trips) migrate sqlite and runtime to bun
Some checks failed
PR Checks / lint-test-and-docker-build (pull_request) Has been cancelled
Some checks failed
PR Checks / lint-test-and-docker-build (pull_request) Has been cancelled
This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
import BetterSqlite3 from 'better-sqlite3';
|
||||
import { Database as BunSqliteDatabase } from 'bun:sqlite';
|
||||
import type { Database } from './types.js';
|
||||
|
||||
export function createSqliteDb(url: string): Database {
|
||||
// Strip the "file:" prefix if present
|
||||
const path = url.startsWith('file:') ? url.slice(5) : url;
|
||||
const db = new BetterSqlite3(path);
|
||||
const db = new BunSqliteDatabase(path);
|
||||
|
||||
// Enable WAL mode for better concurrent read performance
|
||||
db.pragma('journal_mode = WAL');
|
||||
try {
|
||||
db.exec('PRAGMA journal_mode = WAL');
|
||||
} catch {
|
||||
// This can fail when multiple processes initialize the same DB concurrently.
|
||||
}
|
||||
|
||||
return {
|
||||
run(sql, params = []) {
|
||||
db.prepare(sql).run(params);
|
||||
db.query(sql).run(...params);
|
||||
},
|
||||
get<T = Record<string, unknown>>(sql: string, params: unknown[] = []) {
|
||||
return db.prepare(sql).get(params) as T | undefined;
|
||||
const row = db.query(sql).get(...params);
|
||||
return (row === null ? undefined : row) as T | undefined;
|
||||
},
|
||||
all<T = Record<string, unknown>>(sql: string, params: unknown[] = []) {
|
||||
return db.prepare(sql).all(params) as T[];
|
||||
return db.query(sql).all(...params) as T[];
|
||||
},
|
||||
close() {
|
||||
db.close();
|
||||
|
||||
Reference in New Issue
Block a user