prepare
The prepare method parses and retains one reusable read or row-mutation statement in the Worker.
prepare<RowType>(sql: string): Promise<PreparedStatement<RowType>>| Type | Description | |
|---|---|---|
sql | string | |
| returns | Promise<PreparedStatement<RowType>> | A Promise resolving to a reusable |
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();