TOptions
Bun

type

SQL.Options

type Options = SQLiteOptions | PostgresOrMySQLOptions

Configuration options for SQL client connection and behavior

const config: Bun.SQL.Options = {
  host: 'localhost',
  port: 5432,
  user: 'dbuser',
  password: 'secretpass',
  database: 'myapp',
  idleTimeout: 30,
  max: 20,
  onconnect: (client) => {
    console.log('Connected to database');
  }
};

Referenced types

interface SQLiteOptions

Options for Database

  • adapter?: 'sqlite'
  • create?: boolean

    Allow creating a new database

    Equivalent to constants.SQLITE_OPEN_CREATE

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

    Specify the path to the database file

    Examples:

    • sqlite://:memory:
    • sqlite://./path/to/database.db
    • sqlite:///Users/bun/projects/my-app/database.db
    • ./dev.db
    • :memory:
  • onclose?: (err: null | Error) => void

    Callback executed when a connection is closed (SQLite) Receives the closing Error or null.

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

    Callback executed when a connection attempt completes (SQLite) Receives an Error on failure, or null on success.

  • readonly?: boolean

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

    Equivalent to constants.SQLITE_OPEN_READONLY

  • readwrite?: boolean

    Open the database as read-write

    Equivalent to constants.SQLITE_OPEN_READWRITE

  • safeIntegers?: boolean

    When set to true, integers are returned as bigint types.

    When set to false, integers are returned as number types and truncated to 52 bits.

  • strict?: boolean

    When set to false or undefined:

    • Queries missing bound parameters will NOT throw an error
    • Bound named parameters in JavaScript need to exactly match the SQL query.
    const db = new Database(":memory:", { strict: false });
    db.run("INSERT INTO foo (name) VALUES ($name)", { $name: "foo" });
    

    When set to true:

    • Queries missing bound parameters will throw an error
    • Bound named parameters in JavaScript no longer need to be $, :, or @. The SQL query will remain prefixed.

interface PostgresOrMySQLOptions

  • adapter?: 'postgres' | 'mysql' | 'mariadb'

    Database adapter/driver to use

  • bigint?: boolean

    By default values outside i32 range are returned as strings. If this is true, values outside i32 range are returned as BigInts.

  • connection?: Record<string, string | number | boolean>

    Postgres client runtime configuration options

  • connectionTimeout?: number

    Maximum time in seconds to wait when establishing a connection

  • database?: string

    Name of the database to connect to

  • host?: string

    Database server hostname

  • idleTimeout?: number

    Maximum time in seconds to wait for connection to become available

  • max?: number

    Maximum number of connections in the pool

  • maxLifetime?: number

    Maximum lifetime in seconds of a connection

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

    Callback executed when a connection is closed Receives the closing Error or null.

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

    Callback executed when a connection attempt completes Receives an Error on failure, or null on success.

  • password?: string | () => MaybePromise<string>

    Database password for authentication

  • path?: string

    Unix domain socket path for connection

  • port?: string | number

    Database server port number

  • prepare?: boolean

    Automatic creation of prepared statements

  • ssl?: boolean | TLSOptions

    Whether to use TLS/SSL for the connection (alias for tls)

  • tls?: boolean | TLSOptions

    Whether to use TLS/SSL for the connection

  • url?: string | URL

    Connection URL (can be string or URL object)

  • username?: string

    Database user for authentication