query.L() function
Builds a Sequelize literal from a string or a template — the escape hatch for SQL that no query option can express.
Signature:
export function L(str: TemplateStringsArray | string, ...values: any[]): Literal;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
str |
TemplateStringsArray | string |
The SQL text, or the literal parts when used as a template tag. |
|
values |
any[] |
The substitutions, when used as a template tag. |
Returns:
Literal
The text as a Sequelize literal, ready to use as a query option value.
Remarks
A literal is spliced into the statement exactly as given, so nothing about it is parsed, checked or escaped. That is the whole point of it, and the reason to keep each one as small as the job allows: a correlated subquery in a where, a window function in an order, an operator Sequelize has no name for. Runtime values belong in query.E() rather than in the text.
Used as a template tag, the substitutions used to be dropped and the literal parts joined with commas, so the example below produced (SELECT COUNT(*) FROM "SomeTable" WHERE owner = ,) = 0 — accepted by the template, rejected by Postgres. They are now interpolated in order.
query.sql() refuses substitutions rather than interpolating them, and the difference is deliberate: a complete statement can carry bind parameters, so interpolating into one is a choice to avoid. A fragment handed to Sequelize as a literal has no bind channel, which leaves escaping as the only option.
Example
const owner = 3;
const query = {
where: L`(SELECT COUNT(*) FROM "SomeTable" WHERE owner = ${E(owner)}) = 0`,
};