How do I execute raw query (with params already separated) with Drizzle?

Okay, so I've made my own query builder beforehand and I'm migrating away my code from raw postgres-js nature to Drizzle ORM (since I don't really want to parse stuff around anymore like what I would need to be doing with just raw postgres-js. On usual postgres-js, what I'd do is just:
await this.database.unsafe(query, params as never[]);
await this.database.unsafe(query, params as never[]);
In which query is a string and params is a unknown[]. I wanted to execute this with Drizzle, but I haven't find the right way to do so. Even using the sql template literals or even building my own implementation for SQLWrapper interface (source) doesn't seem right to me. Is there any other way for me to execute the code directly without drifting away from the drizzle db instance (that's wrapping the original postgres-js instance)?
3 Replies
reinaldyrfl
reinaldyrflOP9mo ago
What I've tried (and of course, this does not work):
// Remember that `query` is a `string` type
// and `params` is `unknown[]` type
const queryChunks: SQLChunk[] = [];
queryChunks.push(sql.raw(query));
for (const param of params) {
queryChunks.push(sql.param(param));
}

const output = await this.database.execute<{ id: bigint; }>(new SQL(queryChunks)));
// Remember that `query` is a `string` type
// and `params` is `unknown[]` type
const queryChunks: SQLChunk[] = [];
queryChunks.push(sql.raw(query));
for (const param of params) {
queryChunks.push(sql.param(param));
}

const output = await this.database.execute<{ id: bigint; }>(new SQL(queryChunks)));
ssd
ssd4mo ago
Hi reinaldyrfl, were you able to find a reasonable workaround? I came across a similar need.
Angelelz
Angelelz4mo ago
Drizzle does not exposes some like what you're looking for. The whole point of Drizzle is that it does this mapping for you
Want results from more Discord servers?
Add your server