Skip to content

Bun SQL for SQLite

This page explains how to use ts-sql-query with the Bun SQL driver configured with the SQLite adapter, allowing ts-sql-query to execute queries on SQLite databases.

Supported databases

Tested with

The built-in Bun SQL driver, on Bun.

This information reflects the runtimes the test suite runs it on against a real database.

Requirements

Bun 1.4.4 or newer.

Do not share connections between requests

A ts-sql-query connection object — along with the query runner instances passed to its constructor — represents a dedicated connection to the database.

Therefore, you must not share the same connection object between concurrent HTTP requests. Instead, create a new connection object for each request, along with its own query runners.

Even if the query runner internally uses a connection pool, the ts-sql-query connection still represents a single active connection, acquired from the pool. It must be treated as such and never reused across requests.

Using a Bun SQL SQLite connection

Executes queries through a Bun SQL connection configured with the SQLite adapter.

import { SQL } from "bun";
import { BunSqlSqliteQueryRunner } from "ts-sql-query/queryRunners/BunSqlSqliteQueryRunner";

const sql = new SQL({
    adapter: 'sqlite',
    filename: './foobar.db',
});

async function main() {
    const connection = new DBConnection(new BunSqlSqliteQueryRunner(sql));
    // Do your queries here
    connection // ...
}

Safe Integers

If your queries may return integers larger than JavaScript's safe integer range, consider enabling safeIntegers in the Bun SQL SQLite configuration.

Inside a transaction you began with Bun SQL

Created with the transaction object of sql.begin(...), the query runner runs its statements in that transaction, which Bun SQL commits or rolls back when your function ends. A transaction can't begin inside it: connection.transaction(...) and connection.beginTransaction() are rejected with NESTED_TRANSACTION_NOT_SUPPORTED.

import { SQL, type TransactionSQL } from "bun";
import { BunSqlSqliteQueryRunner } from "ts-sql-query/queryRunners/BunSqlSqliteQueryRunner";

const sql = new SQL({ adapter: 'sqlite', filename: './foobar.db' });

async function main() {
    await sql.begin(async (transaction: TransactionSQL) => {
        const connection = new DBConnection(new BunSqlSqliteQueryRunner(transaction));
        // Do your queries here, in the transaction
        connection // ...
    });
}