$ open source · GPL-3.0 — need a commercial license? imqueue.com →

query.sql() function

Normalises a SQL string: one-lined, whitespace collapsed, ending in a single semicolon.

Signature:

export function sql(sqlQuery: string | TemplateStringsArray, ...values: any[]): string;

Parameters

Parameter

Type

Description

sqlQuery

string | TemplateStringsArray

A complete statement, or a template with no substitutions.

values

any[]

Template substitutions, which are not supported.

Returns:

string

The statement on one line, terminated with exactly one ;.

Exceptions

TypeError when used as a template tag with substitutions.

Remarks

Usable as a plain function or as a template tag, the tag form being there so an editor highlights the SQL. It does NOT interpolate: a tag carrying substitutions throws, because the substituted values cannot be reached from here and the statement would come out mangled: tagging SELECT ... WHERE id = ${id} used to yield ... id = ,. Bind parameters are the answer, and they are also the only safe one.

Whitespace inside single-quoted literals is preserved; only whitespace outside them is collapsed.

Example

const statement = sql`
    SELECT id, name
      FROM "Lead"
     WHERE status = $1
`;
// SELECT id, name FROM "Lead" WHERE status = $1;
await database().query(statement, { bind: [status] });