94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
// PEP-36 prototype: drizzle on Node's built-in node:sqlite. drizzle-orm 0.45 has no
|
|
// node:sqlite driver (it exists only in 1.0 beta/rc), but node:sqlite's API is close to
|
|
// better-sqlite3's, so a thin client shim lets drizzle's better-sqlite3 *session* run on it:
|
|
// prepare() → run/all/get/raw(), transaction(fn) → .deferred/.immediate/.exclusive.
|
|
// drizzle-orm/better-sqlite3 itself is not imported: its driver.js imports the native
|
|
// better-sqlite3 package at load time. construct() below mirrors that driver.
|
|
import { DatabaseSync, type StatementSync } from "node:sqlite";
|
|
import { BetterSQLiteSession } from "drizzle-orm/better-sqlite3/session";
|
|
import { createTableRelationsHelpers, extractTablesRelationalConfig } from "drizzle-orm/relations";
|
|
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core/db";
|
|
import { SQLiteSyncDialect } from "drizzle-orm/sqlite-core/dialect";
|
|
|
|
type Params = unknown[];
|
|
|
|
class Statement {
|
|
stmt: StatementSync;
|
|
rawStmt: StatementSync | null = null;
|
|
db: DatabaseSync;
|
|
sql: string;
|
|
constructor(db: DatabaseSync, sql: string) {
|
|
this.db = db;
|
|
this.sql = sql;
|
|
this.stmt = db.prepare(sql);
|
|
}
|
|
run(...p: Params) {
|
|
return this.stmt.run(...(p as any[]));
|
|
}
|
|
all(...p: Params) {
|
|
return this.stmt.all(...(p as any[]));
|
|
}
|
|
get(...p: Params) {
|
|
return this.stmt.get(...(p as any[]));
|
|
}
|
|
/** Rows as arrays (drizzle maps columns by position). */
|
|
raw() {
|
|
if (!this.rawStmt) {
|
|
this.rawStmt = this.db.prepare(this.sql);
|
|
this.rawStmt.setReturnArrays(true);
|
|
}
|
|
return this.rawStmt;
|
|
}
|
|
}
|
|
|
|
export class NodeSqliteClient {
|
|
db: DatabaseSync;
|
|
cache = new Map<string, Statement>();
|
|
constructor(path: string, opts: { busyTimeoutMs?: number } = {}) {
|
|
this.db = new DatabaseSync(path);
|
|
// busy_timeout first: any statement before it can fail at once with SQLITE_BUSY_RECOVERY
|
|
// when many processes open the file together (see bench/sqlite/README.md).
|
|
this.db.exec(`PRAGMA busy_timeout = ${opts.busyTimeoutMs ?? 5000}`);
|
|
this.db.exec("PRAGMA journal_mode = WAL");
|
|
this.db.exec("PRAGMA synchronous = NORMAL");
|
|
this.db.exec("PRAGMA foreign_keys = ON");
|
|
}
|
|
prepare(sql: string) {
|
|
let s = this.cache.get(sql);
|
|
if (!s) this.cache.set(sql, (s = new Statement(this.db, sql)));
|
|
return s;
|
|
}
|
|
exec(sql: string) {
|
|
this.db.exec(sql);
|
|
}
|
|
transaction<T>(fn: (...a: any[]) => T) {
|
|
const run = (begin: string) => (...a: any[]) => {
|
|
this.db.exec(begin);
|
|
try {
|
|
const r = fn(...a);
|
|
if (r && typeof (r as any).then === "function") throw new Error("SQLite transactions must be synchronous (no async callback)");
|
|
this.db.exec("COMMIT");
|
|
return r;
|
|
} catch (e) {
|
|
if (this.db.isTransaction) this.db.exec("ROLLBACK");
|
|
throw e;
|
|
}
|
|
};
|
|
return { deferred: run("BEGIN"), immediate: run("BEGIN IMMEDIATE"), exclusive: run("BEGIN EXCLUSIVE") };
|
|
}
|
|
close() {
|
|
this.db.close();
|
|
}
|
|
}
|
|
|
|
export function drizzleNodeSqlite<S extends Record<string, unknown>>(path: string, fullSchema: S) {
|
|
const client = new NodeSqliteClient(path);
|
|
const dialect = new SQLiteSyncDialect();
|
|
const tables = extractTablesRelationalConfig(fullSchema, createTableRelationsHelpers);
|
|
const schema = { fullSchema, schema: tables.tables, tableNamesMap: tables.tableNamesMap };
|
|
const session = new BetterSQLiteSession(client as any, dialect, schema as any);
|
|
const db = new BaseSQLiteDatabase("sync", dialect, session as any, schema as any) as any;
|
|
db.$client = client;
|
|
return db as BaseSQLiteDatabase<"sync", unknown, S>;
|
|
}
|