Skip to content

How can support bun native sqlite drive? #11236

Open

Description

@seepine

Feature Description

I want to use bun sqlite

The Solution

eg

const AppDataSource = new DataSource({
    type: "bun-sqlite",
    database: "sqlite.db",
})

Considered Alternatives

use sqlite or better-sqlite, but build as docker alpine image with bun cannot work.

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

Activity

  1. alumni commented on Jan 13, 2025

    @alumni
    Collaborator

    I remember bun was implementing its SQLite API to be as close as possible to better-sqlite3, so explicitly passing the driver might help. Would this work?

    import sqlite from 'bun:sqlite';
    
    const AppDataSource = new DataSource({
        type: 'better-sqlite3',
        database: 'sqlite.db',
        driver: sqlite,
    });

    There might be some other issues with bun though, since it's still not 100% compatible with node (e.g. bun is not importing typeorm correctly).

  2. seepine commented on Jan 13, 2025

    @seepine
    Author

    @alumni Unfortunately, this doesn't work.

    83 |     async createDatabaseConnection() {
    84 |         // not to create database directory if is in memory
    85 |         if (this.options.database !== ":memory:")
    86 |             await this.createDatabaseDirectory(path_1.default.dirname(this.options.database));
    87 |         const { database, readonly = false, fileMustExist = false, timeout = 5000, verbose = null, nativeBinding = null, prepareDatabase, } = this.options;
    88 |         const databaseConnection = this.sqlite(database, {
                                                 ^
    TypeError: Cannot call a class constructor without |new|
          at Database (bun:sqlite:195:14)
    

    And try

    import { Database } from 'bun:sqlite'
    
    const AppDataSource = new DataSource({
        type: 'better-sqlite3',
        database: 'sqlite.db',
        driver: (database: string, opt: any) => {
                  return new Database(database, {
                    ...opt,
                    readwrite: true,
                    create: true
                  })
                },
    });

    This seems to be initialized successfully, but some errors occurred.

    query: PRAGMA foreign_keys = OFF
    query: BEGIN TRANSACTION
    query: SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" IN ('user','user_passkey')
    query: ROLLBACK
    query: PRAGMA foreign_keys = ON
    889 |                     promises.push(this.query(`SELECT * FROM "sqlite_master" WHERE "type" = '${type}' AND "${type === "table" ? "name" : "tbl_name"}" IN (${tableNamesWithoutDot})`));
    890 |                 }
    891 |                 return promises;
    892 |             };
    893 |             dbTables = (await Promise.all(queryPromises("table")))
    894 |                 .reduce((acc, res) => [...acc, ...res], [])
                                  ^
    TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
          at /Users/seepine/workspace/bun/rayze-template/node_modules/typeorm/driver/sqlite-abstract/AbstractSqliteQueryRunner.js:894:25
          at reduce (1:11)
          at /Users/seepine/workspace/bun/rayze-template/node_modules/typeorm/driver/sqlite-abstract/AbstractSqliteQueryRunner.js:894:18
    
    889 |                     promises.push(this.query(`SELECT * FROM "sqlite_master" WHERE "type" = '${type}' AND "${type === "table" ? "name" : "tbl_name"}" IN (${tableNamesWithoutDot})`));
    890 |                 }
    891 |                 return promises;
    892 |             };
    893 |             dbTables = (await Promise.all(queryPromises("table")))
    894 |                 .reduce((acc, res) => [...acc, ...res], [])
                                  ^
    TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
          at /Users/seepine/workspace/bun/rayze-template/node_modules/typeorm/driver/sqlite-abstract/AbstractSqliteQueryRunner.js:894:25
          at reduce (1:11)
          at /Users/seepine/workspace/bun/rayze-template/node_modules/typeorm/driver/sqlite-abstract/AbstractSqliteQueryRunner.js:894:18
    
  3. alumni commented on Jan 13, 2025

    @alumni
    Collaborator

    @seepine The code that you wrote is correct. However, Bun does not implement the Statement.reader property. I would suggest opening an issue for them.

    TypeORM uses Statement.reader to check if a prepared statement returns something or not, and then call either Statement.all() or Statement.run() to execute that statement and get the results. See node_modules/typeorm/driver/better-sqlite3/BetterSqlite3QueryRunner.js:77-88 or https://github.com/typeorm/typeorm/blob/0.3.20/src/driver/better-sqlite3/BetterSqlite3QueryRunner.ts#L94-L106

    You might be able to work around this issue and patch TypeORM - replace stmt.reader with query.startsWith("SELECT ") - that should cover about 95% of statements that return something (you don't use CTEs, do you? 😁). Also AbstractSqliteDriver.createParameter might need to be adjusted.

    My experience in the past year with Bun was that most likely you will find another issue quite soon. 🤞🏻

  4. seepine commented on Jan 14, 2025

    @seepine
    Author

    My experience in the past year with Bun was that most likely you will find another issue quite soon. 🤞🏻

    😧😧😧

    Maybe using bun/sqlite is not a good idea...

  5. alumni commented on Jan 20, 2025

    @alumni
    Collaborator

    Looking at Bun's API, I think there's an easier way to check if something returns data or not:

    stmt.columnNames.length > 0

    This is similar to what better-sqlite3 does internally.

    You would try patching TypeORM with the two changes (for createParameters and stmt.reader) and see what other issues you run into 🤷🏻. Maybe since SQLite is not that complex, there won't be much to do.

  6. github-actions commented on Sep 17, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

  7. seepine commented on Sep 20, 2025

    @seepine
    Author

    Maybe wait this done.

    oven-sh/bun#2023
    #10684

  8. reopened this on Sep 20, 2025
  9. alumni commented on Dec 1, 2025

    @alumni
    Collaborator

    I think this can be implemented in a simpler way with an additional driver. Bun's sqlite client is indeed inspired by better-sqlite3, so with a few modifications to that driver (which itself is quite small) you can add bun support.

  10. jappyjan commented on Dec 18, 2025

    @jappyjan

    i would like to see a native bun driver too

  11. trashhead commented on Jun 26, 2026

    @trashhead

    Any updates on this?

  12. seepine commented on Oct 17, 2026

    @seepine
    Author

    The bun issue oven-sh/bun#16469 has been resolved;
    I'll give it a try to see whether it is supported now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions