TinyJoin logoTinyJoin

sql

Essential

The sql method is a parameterizing tagged-template form of query.

sql<RowType>(
  strings: TemplateStringsArray,
  ...params: JsonValue[],
): Promise<Results<RowType>>
TypeDescription
stringsTemplateStringsArray
...paramsJsonValue[]
returnsPromise<Results<RowType>>

A Promise resolving to the statement results.

Interpolated values become $n parameters. It does not interpolate raw identifiers or SQL fragments.

Example

import {create} from 'tinyjoin';

const db = await create();
await db.sql`
  CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT NOT NULL)
`;

const id = 'a';
const title = 'Write docs';
await db.sql`INSERT INTO tasks (id, title) VALUES (${id}, ${title})`;

const {rows} = await db.sql<{title: string}>`
  SELECT title FROM tasks WHERE id = ${id}
`;
console.log(rows);
// -> [{title: 'Write docs'}]
await db.close();