Skip to main content
Module

x/sqlite3/mod.ts>Database#transaction

The fastest and correct module for SQLite3 in Deno.
Latest
method Database.prototype.transaction
import { Database } from "https://deno.land/x/sqlite3@0.11.1/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" },
]);

Type Parameters

T extends (this: Transaction<T>, ...args: any[]) => void