Storage adapter backed by sql.js (WebAssembly) for environments without native SQLite bindings.

With a filePath, the adapter writes the database image to the file after each write, once SQLite has no transaction open: a statement through run(), a script through exec() (the statements before a failure included), a statement through all() or get() that can write (see queryWrites, which the IndexedDB adapter reads too), and the end of a transaction(). It does not save while SQLite has a transaction open, whatever began it (transaction(), BEGIN or SAVEPOINT through run(), or a script), since sql.js's export() closes and reopens the connection, which would roll the transaction back; the save after its COMMIT or RELEASE writes it. The connection's PRAGMA settings are read before each export and set again after it, and the functions registerFunction() made are registered again. TEMP tables, attached databases and functions registered on the sql.js database itself do not survive a save; the adapter warns once, through the host logger, when a save drops TEMP objects.

Implements

Constructors

Properties

kind: "sqljs" = 'sqljs'

Identifier for logging/diagnostics (e.g., 'better-sqlite3', 'postgres').

capabilities: ReadonlySet<StorageCapability>

Capability flags indicating supported features.

Methods

  • Opens the underlying connection or initializes the backing store.

    Parameters

    Returns Promise<void>

    If connection cannot be established

    await adapter.open({ filePath: '/path/to/db.sqlite3' });
    
  • Runs one statement. With a filePath, the database is saved to the file after it unless SQLite has a transaction open; a transaction's statements are saved by the save after its COMMIT. A statement that fails part way (INSERT OR FAIL, UPDATE OR FAIL) keeps the rows it changed before the failure, as SQLite's FAIL resolution does, and they are saved too.

    Parameters

    Returns Promise<StorageRunResult>

  • Retrieves a single row (or null if none found).

    Type Parameters

    • T

    Parameters

    • statement: string

      SQL SELECT statement

    • Optionalparameters: StorageParameters

      Optional parameters for the statement

    Returns Promise<null | T>

    First row or null if no results

    If query execution fails

    const user = await adapter.get<User>('SELECT * FROM users WHERE id = ?', [123]);
    
  • Runs a statement and returns its rows. A statement that writes (INSERT ... RETURNING, a PRAGMA that sets a header field such as user_version) is saved to the file as a run() is; see queryWrites.

    Type Parameters

    • T

    Parameters

    Returns Promise<T[]>

  • Runs a script of one or more statements. With a filePath, the database is saved after it unless SQLite has a transaction open, which a script can begin (BEGIN; INSERT ...) or end (INSERT ...; COMMIT). A script that fails part way keeps the statements before the failure, and they are saved as a script's that succeeds are; the script's error is the one thrown.

    Parameters

    • script: string

    Returns Promise<void>

  • Runs fn in a transaction: COMMIT when it resolves, ROLLBACK when it throws. Callbacks run one at a time, since they share the connection, and the database is saved to the file once the transaction has ended. A nested transaction() on the handle runs in the open transaction.

    Type Parameters

    • T

    Parameters

    Returns Promise<T>

  • Saves the committed state to the file and closes the database. A transaction still open is rolled back first, as closing a SQLite connection does, so the save holds committed writes only. A call made while a close is running waits for that close. The adapter can be opened again.

    Returns Promise<void>