@getodk/slonik-sql-tag-raw

1.0.3-1 • Public • Published

slonik-sql-tag-raw

Travis build status Coveralls NPM version Canonical Code Style Twitter Follow GitSpo Mentions

Slonik SQL tag for constructing dynamic queries.

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.

Package Sidebar

Install

npm i @getodk/slonik-sql-tag-raw

Weekly Downloads

116

Version

1.0.3-1

License

BSD-3-Clause

Unpacked Size

32.4 kB

Total Files

36

Last publish

Collaborators

  • alxndrsn3
  • eyelidlessness
  • skhoja
  • lognaturel
  • yanokwa