interface

SQL.SQLiteOptions

interface SQLiteOptions

Options for Database

  • adapter?: 'sqlite'

    Selects the SQLite adapter. Optional when filename is set, because a filename selects SQLite too.

  • bigint?: boolean

    Return every integer as a BigInt. The same as safeIntegers, under the name that the other adapters use. safeIntegers takes priority.

  • create?: boolean

    Allow creating a new database

    Equivalent to constants.SQLITE_OPEN_CREATE

    When false, opening a file that does not exist throws an error with the code SQLITE_CANTOPEN.

  • filename?: URL | string & {} | ':memory:'

    Path to the database file

    Examples:

    • sqlite://:memory:
    • sqlite://./path/to/database.db
    • sqlite:///Users/bun/projects/my-app/database.db
    • ./dev.db
    • :memory:

    Everything from the first ? is a query string, which can have mode=: ro (read-only), rw (read-write, the file must exist) or rwc (read-write and create, the default). memory has no effect, and any other mode throws. The readonly and create options take priority over it, and a read-only database ignores create and readwrite.

    Without a filename, Bun reads a DATABASE_URL that is not a PostgreSQL, MySQL or MariaDB URL, then SQLITE_URL.

  • onclose?: (err: null | Error) => void

    SQLite: called when SQL.close closes the database. Receives an Error with the message "Connection closed", not null.

  • onconnect?: (err: null | Error) => void

    SQLite: called once, when Bun has tried to open the database. Receives null, or the Error that kept it from opening (for example SQLITE_CANTOPEN).

  • readonly?: boolean

    Open the database as read-only (no write operations, no create).

    Equivalent to constants.SQLITE_OPEN_READONLY

    Together with create: true or readwrite: true, the constructor throws a TypeError.

  • readwrite?: boolean

    Open the database as read-write

    Equivalent to constants.SQLITE_OPEN_READWRITE

    readwrite: true without create: true does not create the file. readwrite: false is the same as readonly: true.

  • safeIntegers?: boolean

    When true, integers are returned as bigint.

    When false, integers are returned as number, and integers beyond 53 bits are rounded to the nearest number.

    When true, a bigint parameter that does not fit in a signed 64-bit integer throws a RangeError. When false, it keeps its low 64 bits: 2n ** 64n - 1n is stored as -1.

  • strict?: boolean

    When set to true, a query with a missing bound parameter throws an error. When set to false or undefined, the parameter is NULL.

    In both modes, the key of a named parameter can include the $, :, or @ prefix or leave it out. If an object has both keys, the key without the prefix wins in strict mode, and the key with the prefix otherwise.

    const db = new Database(":memory:", { strict: true });
    db.run("INSERT INTO foo (name) VALUES ($name)", { name: "foo" });
    db.run("INSERT INTO foo (name) VALUES ($name)", { nmae: "foo" }); // throws
  • transform?: Transform | NameTransform

    Renames columns between the database and JavaScript, for example SQL.camel

  • url?: string | URL

    The same as filename, which takes priority. Needs adapter: "sqlite" unless the value is a sqlite: or file: URL.