-
Notifications
You must be signed in to change notification settings - Fork 301
Alternative to tagged template function? #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @digitalnature ; I have some knex-based / "make a SQL string with ?s and a params[]" that I wanted to execute through postgres.js and was able to do it like this: function convertToSql(sql: Sql, query: string, bindings: any[]): PendingQuery<any> {
// Split the query string by the placeholder character '?'
const fragments = query.split("?");
if (fragments.length - 1 !== bindings.length) {
throw new Error(`Mismatch between placeholders (${fragments.length - 1}) and bindings (${bindings.length})`);
}
// postgres.js does runtime detection of the `raw` property to determine if it's a template string
const templateStrings = Object.assign(fragments, {
raw: fragments,
}) as unknown as TemplateStringsArray;
return sql(templateStrings, ...bindings);
} This takes a query like await sql`select * from authors where a = ${1} and b= ${2}`; This approach also makes the query "look static" to postgres.js's internals, so it will get prepared statement-ized, which the Hope that helps! |
This is what sql.unsafe does. Just use parameters with it. |
Hi @porsager , I had started out doing that (using I assume this is b/c Do you think this is a fundamental limitation of Thanks! |
You just need to pass { prepare: true } as options to unsafe 😉 If it's not in the docs you're more than welcome to PR it. |
@porsager Ack! I should have seen that. :-) Thank you! Opened #1065 adding a paragraph to the readme. 🙏 With that insight, my guess is that |
Well, hard to see if it's not there 😇 thanks for the PR, and yes, I think it answers this issue too 👍 https://github.com/porsager/postgres?tab=readme-ov-file#await-sqlunsafequery-args-options---result |
the tagged template works well for basic things, but when it comes to building queries with dynamic parameters, the syntax gets crazy really fast. Because arrays that contain sql tag cannot be joined with a string like comma or
and
, we are forced to use ternary operator a lot and this creates code that is very hard to read.Is there an alternate way to do queries, like a simple query function that accepts raw sql with
?
placeholders, and function arguments for parameters?The text was updated successfully, but these errors were encountered: