close
Skip to main content

@db/sqlite@0.13.0
Built and signed on GitHub Actions

Works with
This package works with Deno
This package works with Deno
JSR Score100%
Downloads112/wk
Published5 months ago (0.13.0)

The fastest and correct module for SQLite3 in Deno

class Database

Represents a SQLite3 database connection.

Example:

// Open a database from file, creates if doesn't exist.
const db = new Database("myfile.db");

// Open an in-memory database.
const db = new Database(":memory:");

// Open a read-only database.
const db = new Database("myfile.db", { readonly: true });

// Or open using File URL
const db = new Database(new URL("./myfile.db", import.meta.url));

Constructors

new Database(
path: string | URL,
)

Properties

Whether autocommit is enabled. Enabled by default, can be disabled using BEGIN statement.

Number of rows changed by the last executed statement.

Whether DB is in mid of a transaction

Whether to support BigInt columns. False by default, integers larger than 32 bit will be inaccurate.

Gets last inserted Row ID

Whether DB connection is open

Whether to parse JSON columns as JS objects. True by default.

Path of the database file.

Number of rows changed since the database connection was opened.

Unsafe Raw (pointer) to the sqlite object

Methods

Creates a new user-defined aggregate function.

backup(
dest: Database,
name?: string,
pages?
): void

Closes the database connection.

Calling this method more than once is no-op.

Simply executes the SQL statement (supports multiple statements separated by semicolon). Returns the number of changes made by last statement.

Example:

// Create table
db.exec("create table users (id integer not null, username varchar(20) not null)");

// Inserts
db.exec("insert into users (id, username) values(?, ?)", id, username);

// Insert with named parameters
db.exec("insert into users (id, username) values(:id, :username)", { id, username });

// Pragma statements
db.exec("pragma journal_mode = WAL");
db.exec("pragma synchronous = normal");
db.exec("pragma temp_store = memory");

Under the hood, it uses sqlite3_exec if no parameters are given to bind with the SQL statement, a prepared statement otherwise.

function(
name: string,
fn: CallableFunction,
options?: FunctionOptions
): void

Creates a new user-defined function.

Example:

db.function("add", (a: number, b: number) => a + b);
db.prepare("select add(1, 2)").value<[number]>()!; // [3]
loadExtension(
file: string,
entryPoint?: string
): void

Loads an SQLite extension library from the named file.

Open a Blob for incremental I/O.

Make sure to close the blob after you are done with it, otherwise you will have memory leaks.

prepare<T extends object = Record<string, any>>(sql: string): Statement<T>

Creates a new Prepared Statement from the given SQL statement.

Example:

const stmt = db.prepare("SELECT * FROM mytable WHERE id = ?");

for (const row of stmt.all(1)) {
  console.log(row);
}

Bind parameters can be either provided as an array of values, or as an object mapping the parameter name to the value.

Example:

const stmt = db.prepare("SELECT * FROM mytable WHERE id = ?");
const row = stmt.get(1);

// or

const stmt = db.prepare("SELECT * FROM mytable WHERE id = :id");
const row = stmt.get({ id: 1 });

Statements are automatically freed once GC catches them, however you can also manually free using finalize method.

Alias for exec.

sql<T extends Record<string, any> = Record<string, any>>(
strings: TemplateStringsArray,
...parameters: RestBindParameters
): T[]

Safely execute SQL with parameters using a tagged template

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

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" },
]);

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@db/sqlite

Import symbol

import { Database } from "@db/sqlite";
or

Import directly with a jsr specifier

import { Database } from "jsr:@db/sqlite";