-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat: support Bun bundled SQLite runtime #7878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1d3d050
eebb968
0b59076
4e26c67
844933e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import fs from "node:fs"; | ||
| import type { PreparedStatement, RunResult, SqliteAdapter } from "./types"; | ||
|
|
||
| /** | ||
| * The Bun runtime already ships a SQLite driver. Keep this adapter deliberately | ||
| * small so the rest of the application can use the same driver contract as | ||
| * better-sqlite3, node:sqlite, and sql.js. | ||
| */ | ||
| export interface BunSqliteDatabaseLike { | ||
| query(sql: string): { | ||
| run(...params: unknown[]): { changes: number | bigint; lastInsertRowid: number | bigint }; | ||
| get(...params: unknown[]): unknown; | ||
| all(...params: unknown[]): unknown[]; | ||
| }; | ||
| exec(sql: string): void; | ||
| serialize?(): Uint8Array; | ||
| transaction<T>(fn: (...args: unknown[]) => T): { | ||
| (...args: unknown[]): T; | ||
| immediate?: (...args: unknown[]) => T; | ||
| }; | ||
| close(): void; | ||
| } | ||
|
|
||
| function normalizeRunResult(result: { | ||
| changes?: number | bigint; | ||
| lastInsertRowid?: number | bigint; | ||
| }): RunResult { | ||
| return { | ||
| changes: Number(result.changes ?? 0), | ||
| lastInsertRowid: Number(result.lastInsertRowid ?? 0), | ||
| }; | ||
| } | ||
|
|
||
| function normalizeParams(params: unknown[]): unknown[] { | ||
| if (params.length !== 1) return params; | ||
| const [first] = params; | ||
| if ( | ||
| first === null || | ||
| typeof first !== "object" || | ||
| Array.isArray(first) || | ||
| first instanceof Uint8Array || | ||
| (typeof Buffer !== "undefined" && Buffer.isBuffer(first)) | ||
| ) { | ||
| return params; | ||
| } | ||
|
|
||
| // better-sqlite3 callers use bare object keys for @name, :name, and $name. | ||
| // Bun requires the sigil to match the SQL placeholder, so provide all three | ||
| // aliases just as the sql.js adapter does. | ||
| const expanded: Record<string, unknown> = {}; | ||
| for (const [key, value] of Object.entries(first as Record<string, unknown>)) { | ||
| if (/^[:@$]/.test(key)) { | ||
| expanded[key] = value; | ||
| } else { | ||
| expanded[`@${key}`] = value; | ||
| expanded[`:${key}`] = value; | ||
| expanded[`$${key}`] = value; | ||
| } | ||
| } | ||
| return [expanded]; | ||
| } | ||
|
|
||
| export function createBunSqliteAdapter(db: BunSqliteDatabaseLike, filePath: string): SqliteAdapter { | ||
| let isOpen = true; | ||
|
|
||
| return { | ||
| driver: "bun:sqlite", | ||
|
|
||
| get open() { | ||
| return isOpen; | ||
| }, | ||
|
|
||
| get name() { | ||
| return filePath; | ||
| }, | ||
|
|
||
| prepare(sql: string): PreparedStatement { | ||
| const statement = db.query(sql); | ||
| return { | ||
| run(...params: unknown[]): RunResult { | ||
| return normalizeRunResult(statement.run(...normalizeParams(params))); | ||
| }, | ||
| get(...params: unknown[]): unknown { | ||
| return statement.get(...normalizeParams(params)); | ||
| }, | ||
| all(...params: unknown[]): unknown[] { | ||
| return statement.all(...normalizeParams(params)); | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| exec(sql: string): void { | ||
| db.exec(sql); | ||
| }, | ||
|
|
||
| pragma(pragmaStr: string, options?: { simple?: boolean }): unknown { | ||
| const statement = db.query(`PRAGMA ${pragmaStr}`); | ||
| if (options?.simple) { | ||
| const row = statement.get() as Record<string, unknown> | undefined; | ||
| return row ? (Object.values(row)[0] ?? null) : null; | ||
| } | ||
| return statement.all(); | ||
| }, | ||
|
|
||
| transaction<T>(fn: (...args: unknown[]) => T): (...args: unknown[]) => T { | ||
| return db.transaction(fn); | ||
| }, | ||
|
|
||
| immediate(fn: () => void): void { | ||
| const transaction = db.transaction(fn); | ||
| if (typeof transaction.immediate === "function") { | ||
| transaction.immediate(); | ||
| return; | ||
| } | ||
| db.exec("BEGIN IMMEDIATE"); | ||
| try { | ||
| fn(); | ||
| db.exec("COMMIT"); | ||
| } catch (error) { | ||
| try { | ||
| db.exec("ROLLBACK"); | ||
| } catch {} | ||
| throw error; | ||
| } | ||
| }, | ||
|
|
||
| async backup(destination: string): Promise<void> { | ||
| if (filePath === ":memory:") return; | ||
| try { | ||
| db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); | ||
| } catch {} | ||
| fs.copyFileSync(filePath, destination); | ||
| }, | ||
|
Comment on lines
+127
to
+133
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using async backup(destination: string): Promise<void> {
if (filePath === ":memory:") return;
try {
db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
} catch {}
fs.copyFileSync(filePath, destination);
}, |
||
|
|
||
| checkpoint(mode = "TRUNCATE"): void { | ||
| try { | ||
| db.exec(`PRAGMA wal_checkpoint(${mode})`); | ||
| } catch {} | ||
| }, | ||
|
|
||
| close(): void { | ||
| if (!isOpen) return; | ||
| db.close(); | ||
| isOpen = false; | ||
| }, | ||
|
|
||
| get raw() { | ||
| return db; | ||
| }, | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
db.serialize()for physical file-backed databases is highly inefficient and poses a significant Out Of Memory (OOM) risk for large databases because it loads the entire database into memory as aUint8Array. SincesourcePathis a physical file, we should perform a standard checkpoint and usefs.copyFileSyncdirectly.