dmfay / massive-js

A data mapper for Node.js and PostgreSQL.
2.49k stars 158 forks source link

Question: Named Parameter Syntax #668

Closed bighappyworld closed 5 years ago

bighappyworld commented 5 years ago

Love the library!

I wanted to understand the reason behind the named parameter syntax. Here's an example from the docs:

db.query(
  'select * from tests where id > ${something}',
  {something: 1}
).then(tests => {
  // all tests matching the criteria
});

It is using the same javascript syntax for variable replacement in a back ticked string. So it actually prevents someone from being able to use a back ticked string when building the query sql. And I think normal linter rules look for this and pop a warning that there is a string substitution detected but the wrong type of quotes (possibly even auto-fixing it incorrectly). A poor example would be substituting the table name tests for a variable table name.

I just wondered the reasoning behind using the same syntax and if there was any "advantage" in using it that I wasn't seeing. Thanks!

bighappyworld commented 5 years ago

Nevermind, I found the reason. It comes from pg-promise. But pg-promise supports a variety of different syntaxes for Named Parameter substitution and they work (just tested). Updating the docs on Massive to say it supports the ones from pg-promise would be helpful because the current docs make it look like the above syntax is the only syntax (at least to me).

bighappyworld commented 5 years ago

Added to the docs: https://github.com/dmfay/massive-js/pull/669