Closed Azarattum closed 1 year ago
Actually I used prepared statements in the original implementation. The problem I've encountered is that we (as the library) cannot tell what the user intention is. If we eagerly try to prepare everything passed into the template function, we often end up trying to prepare incomplete sql snippets.
Kysely
does and provide query id to the executor function. This would be an object, so the user can use WeakMap to cache prepared statements.So the question comes down to what do we value more flexibility or ease of use? Is there any value in giving user a choice to cache or not to cache prepared statements or is it just objectively better to always cache them? I'll take a look at the article and think a bit more about it. This probably should be a separate PR anyway.
Actually I used prepared statements in the original implementation. The problem I've encountered is that we (as the library) cannot tell what the user intention is. If we eagerly try to prepare everything passed into the template function, we often end up trying to prepare incomplete sql snippets.
We could also let the user decide when to prepare:
E.g.,
const query = sql`...`;
const prepared = query.prepare();
// or
const prepared = db.prepare(query);
const results = prepared.bind(values).then();
This probably should be a separate PR anyway.
Agreed
@tantaman, is this ready to be merged? I've added all the tests here.
lgtm.
I'm also getting pretty close to having something workable for SELECT
statements.
This PR introduces 3 new functions that are now available on
sql
template literal:sql.table
- is used to dynamically pass table identifierssql.column
- is used to dynamically pass column identifierssql.values
- is used to dynamically pass values (as objects or arrays)For example:
table
andcolumn
are the samequote
function under the hood. The difference is only in typing.table
function will accept any valid table name from the schema as its argument. Whilecolumn
function accepts any column name from any table by default and can be narrowed using a generic.values
can accepts arrays or objects as row values. It has variadic parameters to allow for multiple rows.We might generate these generics automatically in the future if this would be feasible to do. For now it is up to the user to specify those.