import { Database } from "https://deno.land/x/sqlite3@0.5.3/mod.ts";
Wraps a callback function in a transaction.
- When function is called, the transaction is started.
- When function returns, the transaction is committed.
- When function throws an error, the transaction is rolled back.
Example:
const stmt = db.prepare("insert into users (id, username) values(?, ?)");
interface User {
id: number;
username: string;
}
const insertUsers = db.transaction((data: User[]) => {
for (const user of data) {
stmt.run(user);
}
});
insertUsers([
{ id: 1, username: "alice" },
{ id: 2, username: "bob" },
]);
// May also use `insertUsers.deferred`, `immediate`, or `exclusive`.
// They corresspond to using `BEGIN DEFERRED`, `BEGIN IMMEDIATE`, and `BEGIN EXCLUSIVE`.
// For eg.
insertUsers.deferred([
{ id: 1, username: "alice" },
{ id: 2, username: "bob" },
]);
Parameters
fn: (this: Transaction<T>, _: T) => unknown