Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/sqlite3/mod.ts>Database#transaction

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