Lines 1-38javascript
2 * db.ts - Cross-runtime SQLite compatibility layer
4 * Provides a unified Database export that works under both Bun (bun:sqlite)
5 * and Node.js (better-sqlite3). The APIs are nearly identical — the main
6 * difference is the import path.
8 * On macOS, Apple's system SQLite is compiled with SQLITE_OMIT_LOAD_EXTENSION,
9 * which prevents loading native extensions like sqlite-vec. When running under
10 * Bun we call Database.setCustomSQLite() to swap in Homebrew's full-featured
11 * SQLite build before creating any database instances.
13export const isBun = typeof globalThis.Bun !== "undefined";
17 // Dynamic string prevents tsc from resolving bun:sqlite on Node.js builds
18 const bunSqlite = "bun:" + "sqlite";
19 const BunDatabase = (await import(/* @vite-ignore */ bunSqlite)).Database;
20 // See: https://bun.com/docs/runtime/sqlite#setcustomsqlite
MediumDynamic Require
Package source references dynamic require/import behavior.
dist/db.jsView on unpkg · L18 21 if (process.platform === "darwin") {
22 const homebrewPaths = [
23 "/opt/homebrew/opt/sqlite/lib/libsqlite3.dylib", // Apple Silicon
24 "/usr/local/opt/sqlite/lib/libsqlite3.dylib", // Intel
26 for (const p of homebrewPaths) {
28 BunDatabase.setCustomSQLite(p);
34 _Database = BunDatabase;
35 // setCustomSQLite may have silently failed — test that extensions actually work.
37 const { getLoadablePath } = await import("sqlite-vec");
38 const vecPath = getLoadablePath();