TinyJoin logoTinyJoin

prepare

The prepare method parses and retains one reusable read or row-mutation statement in the Worker.

prepare<RowType>(sql: string): Promise<PreparedStatement<RowType>>
TypeDescription
sqlstring
returnsPromise<PreparedStatement<RowType>>

A Promise resolving to a reusable PreparedStatement handle.

Example

import {create} from 'tinyjoin';

const db = await create();
await db.exec(`
  CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT NOT NULL);
  INSERT INTO tasks (id, title) VALUES ('a', 'Write docs');
  INSERT INTO tasks (id, title) VALUES ('b', 'Ship release');
`);
const titleOf = await db.prepare<{title: string}>(
  'SELECT title FROM tasks WHERE id = $1',
);
for (const id of ['a', 'b']) {
  const {rows} = await titleOf.execute([id]);
  console.log(rows[0].title);
}
// -> 'Write docs'
// -> 'Ship release'
await titleOf.close();
await db.close();