Skip to content

gajus/slonik-sql-tag-raw

Repository files navigation

slonik-sql-tag-raw

Travis build status Coveralls NPM version Canonical Code Style Twitter Follow

Slonik SQL tag for constructing dynamic queries.

Warning

Deprecated in favor of https://github.com/gajus/slonik monorepo.

Warning

There are no known use cases for generating queries using raw that aren't covered by nesting bound sql expressions or by one of the other existing query building methods. raw exists only as a mechanism to execute externally stored static queries (e.g. queries stored in files).

Usage

import {
  raw,
} from 'slonik-sql-tag-raw';

raw

(
  sql: string,
  values?: $ReadOnlyArray<PrimitiveValueExpressionType>
) => RawSqlTokenType;

Raw/ dynamic SQL can be inlined using raw, e.g.

sql`
  SELECT 1
  FROM ${raw('"bar"')}
`;

Produces:

{
  sql: 'SELECT 1 FROM "bar"',
  values: []
}

The second parameter of the raw can be used to bind positional parameter values, e.g.

sql`
  SELECT ${raw('$1', [1])}
`;

Produces:

{
  sql: 'SELECT $1',
  values: [
    1
  ]
}

Named parameters

raw supports named parameters, e.g.

sql`
  SELECT ${raw(':foo, :bar', {bar: 'BAR', foo: 'FOO'})}
`;

Produces:

{
  sql: 'SELECT $1, $2',
  values: [
    'FOO',
    'BAR'
  ]
}

Named parameters are matched using /[\s,(]:([a-z_]+)/g regex.