sequelts icon indicating copy to clipboard operation
sequelts copied to clipboard

Support ES Tagged Template Function

Open nikeee opened this issue 3 years ago • 0 comments

Basically, we want to create a function that does this: https://www.npmjs.com/package/sql-template-strings

Current API:

const getSingle = createQuerySingleFunction((q, ...p) => db.prepare(q).get(...p), databaseSchema);

const a = getSingle("SELECT name FROM user WHERE id = ?", 1);

What we want to have:

const get = createQuerySingleTagFunction((q, ...p) => db.prepare(q).get(...p), databaseSchema);
// (or just the function without the database wrapping)

const a = get`SELECT name FROM user WHERE id = ${1}`;

If the user names the function SQL instead of get, the keywords are also highlighted, improving the DX. Whether this is a good idea is a different story.

Additional benefits:

  • If the columns in the WHERE clause are also returned in the SELECT, we maybe could use the type of the bound parameter to infer the type of the column.

Solutions:

  • Re-Implement sql-template-strings (it's about 90 lines and uses legacy stuff like arguments (src))
  • Have sql-template-strings as a dependency and provide finer types

nikeee avatar May 02 '22 17:05 nikeee