ts-postgres
ts-postgres copied to clipboard
parameters
from the readme,
Query parameters use the format $1, $2 etc.
do the parameters have to be numbered? i'd much prefer to use names, as i'm familiar with using parameter placeholders like SELECT * FROM whatever WHERE :lol = 5 or something like that, right?
i'm just really hoping i don't have to worry about how things are ordered, i'm an options-object kind of guy
Yes, it is probably possible to have a preprocessor that scans through the statement, replacing :<param-name> with a position-based expression and then map it correctly on query execute.
But if you look at https://github.com/brianc/node-postgres/issues/268, there are plenty of reasons not to want something like that – opting instead for doing this on a higher level; for example, a query builder could provide such an interface.
oh. well that makes sense. thanks for sharing that link!
maybe we could whip up some sugar for named params?
would you rather that be in a separate repo/package, or would you be interested for some kind of sugar/ directory here for experimenting with these kinds of extras?
preprocessor that scans through the statement
i just read that, and thought about tagged template literals at the same time — and got a nerd-boner!
It might be possible to implement this sugar using a tokenizer only and not have to actually parse the query. I wouldn't mind having a simple desugaring function in the code – the functionality is nice to have for sure.
i'm thinking something along these lines ought be possible!
const result = await query`SELECT * FROM table WHERE column = ${value}`
how do you fancy?
That looks good. People understand the escaping of those literals because of the tagged literal syntax.
And it's far better than :param = ... which would be hard to implement correctly and is as foreign to JS/TypeScript as it is to PostgreSQL.
i'll look into putting together a pull request for you, to add a sugar directory, so we can experiment and mull it over :)