query

The query method runs one parameterized statement against staged data.

query<RowType>(
  sql: string,
  params?: JsonValue[],
  options?: QueryOptions,
): Promise<Results<RowType>>
TypeDescription
sqlstring
params?JsonValue[]
options?QueryOptions
returnsPromise<Results<RowType>>

A Promise resolving to the statement results.

Example

import {create} from 'tinyjoin';

const db = await create();
await db.exec(`
  CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT NOT NULL)
`);
await db.transaction(async (tx) => {
  await tx.query('INSERT INTO tasks (id, title) VALUES ($1, $2)', [
    'a',
    'Write docs',
  ]);
  // A read inside the transaction sees the rows it has staged.
  const {rows} = await tx.query('SELECT title FROM tasks WHERE id = $1', [
    'a',
  ]);
  console.log(rows);
  // -> [{title: 'Write docs'}]
});
await db.close();