sql
The sql method is a parameterizing tagged-template form of query.
sql<RowType>(
strings: TemplateStringsArray,
...params: JsonValue[],
): Promise<Results<RowType>>| Type | Description | |
|---|---|---|
strings | TemplateStringsArray | |
...params | JsonValue[] | |
| returns | Promise<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)
`);
const tasks = [
{id: 'a', title: 'Write docs'},
{id: 'b', title: 'Ship release'},
];
await db.transaction(async (tx) => {
for (const {id, title} of tasks) {
await tx.sql`INSERT INTO tasks (id, title) VALUES (${id}, ${title})`;
}
});
const {rows} = await db.query('SELECT id FROM tasks ORDER BY id');
console.log(rows);
// -> [{id: 'a'}, {id: 'b'}]
await db.close();