Skip to content

Commit acfa15d

Browse files
committed
fix: replace better-sqlite3 with bun:sqlite for schema cache and SQLite driver
`better-sqlite3` is a native addon that doesn't work on Bun ("not yet supported"). This broke `schema_index`, `schema_search`, `schema_cache_status`, and the SQLite driver for all users on the released CLI binary. Switch to `bun:sqlite` which is built into the Bun runtime — zero-install, no native compilation, same synchronous API. The storage layer (`db.ts`) already uses this pattern successfully. Changes: - `schema/cache.ts`: direct `bun:sqlite` import, sync `create()`/`createInMemory()` - `drivers/sqlite.ts`: `bun:sqlite` import, fix PRAGMA+LIMIT syntax error - Remove `better-sqlite3` from optional deps, peer deps, build externals, types - Update driver docs and E2E tests (SQLite tests no longer need skip guards) Closes #314
1 parent 7a16e73 commit acfa15d

8 files changed

Lines changed: 44 additions & 82 deletions

File tree

‎docs/docs/drivers.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Altimate Code connects to 10 databases natively via TypeScript drivers. No Pytho
1010
|----------|---------|-------------|------------|-------|
1111
| PostgreSQL | `pg` | Password, Connection String, SSL | ✅ Docker | Stable, fully parameterized queries |
1212
| DuckDB | `duckdb` | File/Memory (no auth) | ✅ In-memory | Default local database |
13-
| SQLite | `better-sqlite3` | File (no auth) | ✅ File-based | Sync API wrapped async |
13+
| SQLite | `bun:sqlite` (built-in) | File (no auth) | ✅ File-based | Zero-install, built into runtime |
1414
| MySQL | `mysql2` | Password | ✅ Docker | Parameterized introspection |
1515
| SQL Server | `mssql` | Password, Azure AD | ✅ Docker | Uses `tedious` TDS protocol |
1616
| Redshift | `pg` (wire-compat) | Password | ✅ Docker (PG wire) | Uses SVV system views |
@@ -26,7 +26,7 @@ Drivers are `optionalDependencies`, so install only what you need:
2626
```bash
2727
# Embedded databases (no external service needed)
2828
bun add duckdb
29-
bun add better-sqlite3
29+
# SQLite uses bun:sqlite (built-in, no install needed)
3030

3131
# Standard databases
3232
bun add pg # PostgreSQL + Redshift

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
},
7474
"devDependencies": {
7575
"@tsconfig/bun": "catalog:",
76-
"@types/better-sqlite3": "7.6.13",
7776
"@types/pg": "8.18.0",
7877
"@typescript/native-preview": "catalog:",
7978
"husky": "9.1.7",

‎packages/drivers/package.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"mysql2": "^3.0.0",
1717
"mssql": "^11.0.0",
1818
"oracledb": "^6.0.0",
19-
"duckdb": "^1.0.0",
20-
"better-sqlite3": "^11.0.0"
19+
"duckdb": "^1.0.0"
2120
}
2221
}

‎packages/drivers/src/sqlite.ts‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
/**
2-
* SQLite driver using the `better-sqlite3` package.
2+
* SQLite driver using Bun's built-in `bun:sqlite`.
33
* Synchronous API wrapped in async interface.
44
*/
55

6+
import { Database } from "bun:sqlite"
67
import type { ConnectionConfig, Connector, ConnectorResult, SchemaColumn } from "./types"
78

89
export async function connect(config: ConnectionConfig): Promise<Connector> {
9-
let Database: any
10-
try {
11-
const mod = await import("better-sqlite3")
12-
Database = mod.default || mod
13-
} catch {
14-
throw new Error(
15-
"SQLite driver not installed. Run: npm install better-sqlite3",
16-
)
17-
}
18-
1910
const dbPath = (config.path as string) ?? ":memory:"
20-
let db: any
11+
let db: Database | null = null
2112

2213
return {
2314
async connect() {
2415
db = new Database(dbPath, {
2516
readonly: config.readonly === true,
17+
create: true,
2618
})
27-
db.pragma("journal_mode = WAL")
19+
db.exec("PRAGMA journal_mode = WAL")
2820
},
2921

3022
async execute(sql: string, limit?: number, _binds?: any[]): Promise<ConnectorResult> {
23+
if (!db) throw new Error("SQLite connection not open")
3124
const effectiveLimit = limit ?? 1000
3225

3326
// Determine if this is a SELECT-like statement
3427
const trimmed = sql.trim().toLowerCase()
28+
const isPragma = trimmed.startsWith("pragma")
3529
const isSelect =
3630
trimmed.startsWith("select") ||
37-
trimmed.startsWith("pragma") ||
31+
isPragma ||
3832
trimmed.startsWith("with") ||
3933
trimmed.startsWith("explain")
4034

35+
// PRAGMA statements don't support LIMIT clause
4136
let query = sql
4237
if (
4338
isSelect &&
39+
!isPragma &&
4440
effectiveLimit &&
4541
!/\bLIMIT\b/i.test(sql)
4642
) {
@@ -59,7 +55,7 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
5955
}
6056

6157
const stmt = db.prepare(query)
62-
const rows = stmt.all()
58+
const rows = stmt.all() as any[]
6359
const columns = rows.length > 0 ? Object.keys(rows[0]) : []
6460
const truncated = rows.length > effectiveLimit
6561
const limitedRows = truncated ? rows.slice(0, effectiveLimit) : rows
@@ -82,11 +78,12 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
8278
async listTables(
8379
_schema: string,
8480
): Promise<Array<{ name: string; type: string }>> {
81+
if (!db) throw new Error("SQLite connection not open")
8582
const rows = db
8683
.prepare(
8784
"SELECT name, type FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
8885
)
89-
.all()
86+
.all() as any[]
9087
return rows.map((r: any) => ({
9188
name: r.name as string,
9289
type: r.type as string,
@@ -97,7 +94,8 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
9794
_schema: string,
9895
table: string,
9996
): Promise<SchemaColumn[]> {
100-
const rows = db.prepare('SELECT * FROM pragma_table_info(?) ORDER BY cid').all(table)
97+
if (!db) throw new Error("SQLite connection not open")
98+
const rows = db.prepare('SELECT * FROM pragma_table_info(?) ORDER BY cid').all(table) as any[]
10199
return rows.map((r: any) => ({
102100
name: r.name as string,
103101
data_type: r.type as string,

‎packages/opencode/script/build.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ for (const item of targets) {
242242
"@altimateai/altimate-core",
243243
// Database drivers — native addons, users install on demand per warehouse
244244
"pg", "snowflake-sdk", "@google-cloud/bigquery", "@databricks/sql",
245-
"mysql2", "mssql", "oracledb", "duckdb", "better-sqlite3",
245+
"mysql2", "mssql", "oracledb", "duckdb",
246246
// Optional infra packages — native addons or heavy optional deps
247247
"keytar", "ssh2", "dockerode",
248248
],

‎packages/opencode/script/publish.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const driverPeerDependencies: Record<string, string> = {
2929
"mssql": ">=11",
3030
"oracledb": ">=6",
3131
"duckdb": ">=1",
32-
"better-sqlite3": ">=11",
3332
}
3433

3534
const driverPeerDependenciesMeta: Record<string, { optional: true }> = Object.fromEntries(

‎packages/opencode/src/altimate/native/schema/cache.ts‎

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* Schema cache — indexes warehouse metadata into SQLite for fast search.
33
*
4-
* Uses better-sqlite3 (optional dependency, dynamically imported) to build
5-
* a local FTS-ready cache of warehouse schemas, tables, and columns.
4+
* Uses bun:sqlite (built into the Bun runtime) to build a local FTS-ready
5+
* cache of warehouse schemas, tables, and columns.
66
* Cache location: ~/.altimate-code/schema-cache.db
77
*/
88

9+
import { Database } from "bun:sqlite"
910
import * as path from "path"
1011
import * as os from "os"
1112
import * as fs from "fs"
@@ -115,45 +116,24 @@ function tokenizeQuery(query: string): string[] {
115116

116117
/** SQLite-backed schema metadata cache for fast warehouse search. */
117118
export class SchemaCache {
118-
private db: any // better-sqlite3 Database instance
119+
private db: Database
119120
private dbPath: string
120121

121-
private constructor(db: any, dbPath: string) {
122+
private constructor(db: Database, dbPath: string) {
122123
this.db = db
123124
this.dbPath = dbPath
124125
}
125126

126-
/**
127-
* Create a SchemaCache instance.
128-
* Uses dynamic import for better-sqlite3 (optional dependency).
129-
*/
130-
static async create(dbPath?: string): Promise<SchemaCache> {
127+
/** Create a SchemaCache instance backed by a file on disk. */
128+
static create(dbPath?: string): SchemaCache {
131129
const resolvedPath = dbPath || defaultCachePath()
132-
let Database: any
133-
try {
134-
const mod = await import("better-sqlite3")
135-
Database = mod.default || mod
136-
} catch {
137-
throw new Error(
138-
"better-sqlite3 not installed. Install with: npm install better-sqlite3",
139-
)
140-
}
141-
const db = new Database(resolvedPath)
130+
const db = new Database(resolvedPath, { create: true })
142131
db.exec(CREATE_TABLES_SQL)
143132
return new SchemaCache(db, resolvedPath)
144133
}
145134

146-
/**
147-
* Create a SchemaCache with an in-memory database (for testing).
148-
*/
149-
static async createInMemory(): Promise<SchemaCache> {
150-
let Database: any
151-
try {
152-
const mod = await import("better-sqlite3")
153-
Database = mod.default || mod
154-
} catch {
155-
throw new Error("better-sqlite3 not installed.")
156-
}
135+
/** Create a SchemaCache with an in-memory database (for testing). */
136+
static createInMemory(): SchemaCache {
157137
const db = new Database(":memory:")
158138
db.exec(CREATE_TABLES_SQL)
159139
return new SchemaCache(db, ":memory:")
@@ -399,7 +379,7 @@ let _cache: SchemaCache | null = null
399379

400380
export async function getCache(): Promise<SchemaCache> {
401381
if (!_cache) {
402-
_cache = await SchemaCache.create()
382+
_cache = SchemaCache.create()
403383
}
404384
return _cache
405385
}

‎packages/opencode/test/altimate/drivers-e2e.test.ts‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ function isDuckDBAvailable(): boolean {
1919
}
2020
}
2121

22-
function isBetterSqlite3Available(): boolean {
23-
try {
24-
const Database = require("better-sqlite3")
25-
// Verify it actually works (bun throws "not yet supported")
26-
const db = new Database(":memory:")
27-
db.prepare("SELECT 1").get()
28-
db.close()
29-
return true
30-
} catch {
31-
return false
32-
}
33-
}
34-
3522
function isDockerAvailable(): boolean {
3623
if (process.env.TEST_PG_HOST) return true // CI services replace Docker
3724
if (!process.env.DRIVER_E2E_DOCKER) return false // Skip unless opted in
@@ -70,7 +57,6 @@ async function waitForPort(
7057
}
7158

7259
const duckdbAvailable = isDuckDBAvailable()
73-
const sqliteAvailable = isBetterSqlite3Available()
7460
const dockerAvailable = isDockerAvailable()
7561

7662
// ---------------------------------------------------------------------------
@@ -418,7 +404,7 @@ describe("SQLite Driver E2E", () => {
418404
let tmpDir: string
419405

420406
beforeAll(async () => {
421-
if (!sqliteAvailable) return
407+
// bun:sqlite is always available — no runtime check needed
422408
tmpDir = mkdtempSync(join(tmpdir(), "sqlite-test-"))
423409
const dbFile = join(tmpDir, "test.sqlite")
424410
const mod = await import("@altimateai/drivers/sqlite")
@@ -431,19 +417,19 @@ describe("SQLite Driver E2E", () => {
431417
if (tmpDir) rmSync(tmpDir, { recursive: true, force: true })
432418
})
433419

434-
test.skipIf(!sqliteAvailable)("connect to file database", () => {
420+
test("connect to file database", () => {
435421
expect(connector).toBeDefined()
436422
})
437423

438-
test.skipIf(!sqliteAvailable)("execute SELECT query", async () => {
424+
test("execute SELECT query", async () => {
439425
const result = await connector.execute("SELECT 1 AS num, 'hello' AS msg")
440426
expect(result.columns).toEqual(["num", "msg"])
441427
expect(result.rows).toEqual([[1, "hello"]])
442428
expect(result.row_count).toBe(1)
443429
expect(result.truncated).toBe(false)
444430
})
445431

446-
test.skipIf(!sqliteAvailable)(
432+
test(
447433
"execute DDL + DML queries",
448434
async () => {
449435
// CREATE
@@ -493,32 +479,33 @@ describe("SQLite Driver E2E", () => {
493479
},
494480
)
495481

496-
test.skipIf(!sqliteAvailable)(
482+
test(
497483
"listSchemas (SQLite has only 'main')",
498484
async () => {
499485
const schemas = await connector.listSchemas()
500486
expect(schemas).toEqual(["main"])
501487
},
502488
)
503489

504-
test.skipIf(!sqliteAvailable)("listTables", async () => {
490+
test("listTables", async () => {
505491
const tables = await connector.listTables("main")
506492
const names = tables.map((t) => t.name)
507493
expect(names).toContain("test_sqlite")
508494
const entry = tables.find((t) => t.name === "test_sqlite")
509495
expect(entry?.type).toBe("table")
510496
})
511497

512-
test.skipIf(!sqliteAvailable)("describeTable", async () => {
498+
test("describeTable", async () => {
513499
const columns = await connector.describeTable("main", "test_sqlite")
514500
expect(columns).toEqual([
515-
{ name: "id", data_type: "INTEGER", nullable: false },
501+
// INTEGER PRIMARY KEY is a rowid alias — SQLite reports notnull=0 for it
502+
{ name: "id", data_type: "INTEGER", nullable: true },
516503
{ name: "name", data_type: "TEXT", nullable: true },
517504
{ name: "score", data_type: "REAL", nullable: true },
518505
])
519506
})
520507

521-
test.skipIf(!sqliteAvailable)(
508+
test(
522509
"handles read vs write query detection",
523510
async () => {
524511
// SELECT-like returns data rows
@@ -541,7 +528,7 @@ describe("SQLite Driver E2E", () => {
541528
},
542529
)
543530

544-
test.skipIf(!sqliteAvailable)(
531+
test(
545532
"LIMIT truncation works",
546533
async () => {
547534
// Insert enough rows
@@ -560,14 +547,14 @@ describe("SQLite Driver E2E", () => {
560547
},
561548
)
562549

563-
test.skipIf(!sqliteAvailable)(
550+
test(
564551
"handles invalid SQL gracefully",
565552
async () => {
566553
expect(() => connector.execute("INVALID SQL STATEMENT")).toThrow()
567554
},
568555
)
569556

570-
test.skipIf(!sqliteAvailable)(
557+
test(
571558
"close and cleanup",
572559
async () => {
573560
const tmpDir2 = mkdtempSync(join(tmpdir(), "sqlite-close-test-"))
@@ -586,7 +573,7 @@ describe("SQLite Driver E2E", () => {
586573
},
587574
)
588575

589-
test.skipIf(!sqliteAvailable)(
576+
test(
590577
"view is listed with correct type",
591578
async () => {
592579
await connector.execute(

0 commit comments

Comments
 (0)