ts-postgres icon indicating copy to clipboard operation
ts-postgres copied to clipboard

parameters

Open chase-moskal opened this issue 5 years ago • 7 comments

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

chase-moskal avatar Jun 16 '20 04:06 chase-moskal

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.

malthe avatar Jun 16 '20 07:06 malthe

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?

chase-moskal avatar Jun 16 '20 13:06 chase-moskal

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!

chase-moskal avatar Jun 16 '20 14:06 chase-moskal

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.

malthe avatar Jun 16 '20 14:06 malthe

i'm thinking something along these lines ought be possible!

const result = await query`SELECT * FROM table WHERE column = ${value}`

how do you fancy?

chase-moskal avatar Jun 17 '20 00:06 chase-moskal

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.

malthe avatar Jun 17 '20 06:06 malthe

i'll look into putting together a pull request for you, to add a sugar directory, so we can experiment and mull it over :)

chase-moskal avatar Jun 18 '20 03:06 chase-moskal