TinyJoin logoTinyJoin

execute

The execute method runs a prepared statement owned by the same Client.

execute<RowType>(
  statement: PreparedStatement<RowType>,
  params?: JsonValue[],
  options?: QueryOptions,
): Promise<Results<RowType>>
TypeDescription
statementPreparedStatement<RowType>
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)
`);
// Prepare the statement before the transaction, and run it inside.
const insert = await db.prepare(
  'INSERT INTO tasks (id, title) VALUES ($1, $2)',
);
await db.transaction(async (tx) => {
  await tx.execute(insert, ['a', 'Write docs']);
  await tx.execute(insert, ['b', 'Ship release']);
});

const {rows} = await db.query('SELECT id FROM tasks ORDER BY id');
console.log(rows);
// -> [{id: 'a'}, {id: 'b'}]
await insert.close();
await db.close();