porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.04k stars 257 forks source link

[question] How to get the query string before execution? #818

Open IAkumaI opened 4 months ago

IAkumaI commented 4 months ago
let where = sql`col = 1`;
let query = sql`SELECT * FROM WHERE ${where}`;

let sql_as_string = query.(what?);

Hello. Is there any way to get the actual sql query string before/without the exucution? I know we have sql.statement but it is not available before the query exucution and if the query has errors (while building it dynamically) I don't know how to debug it properly.

I found query.strings but it's...well not what i need.

masfahru commented 4 months ago

https://github.com/porsager/postgres?tab=readme-ov-file#query-descriptions

IAkumaI commented 4 months ago

Describe executes the query. Without correct query and correct connection it will not give me the query string:

let where = sql`WHERE a = 1`;
console.log(await sql`SELECT ERROR ${where}`.describe()); // column "error" does not exist

This shows only error, but not the actual query string. What do I do wrong?

IAkumaI commented 4 months ago

Found a "tricky" way: https://github.com/porsager/postgres/discussions/760 But maybe there is another more "right" way? There is nothing about this in the docs, btw.