refactor: migrate from better-sqlite3 to node:sqlite

- Replace better-sqlite3 with built-in node:sqlite (DatabaseSync)
- Add transaction() helper for manual BEGIN/COMMIT/ROLLBACK
- Suppress ExperimentalWarning in CLI tests with --no-warnings
- Remove better-sqlite3 from dependencies and onlyBuiltDependencies
- No more native addon compilation issues across Node versions
This commit is contained in:
2026-06-03 22:11:44 +00:00
parent f8103e20ce
commit 00a536631a
31 changed files with 227 additions and 1045 deletions
+2 -1
View File
@@ -1,11 +1,12 @@
#!/usr/bin/env node
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
import type { Hash, ListEntry, ListOptions, Store, TagOp } from "@ocas/core";
import {
applyListOptions,
@@ -373,7 +373,11 @@ exports[`Phase 3: Variable System > 3.10 var delete removes variable 1`] = `
}
`;
exports[`Phase 3: Variable System > 3.11 var get deleted variable returns not found 1`] = `"Error: Variable not found: name=@myapp/config, schema=FRBAB1BF0ZBCS"`;
exports[`Phase 3: Variable System > 3.11 var get deleted variable returns not found 1`] = `
"(node:651685) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
Error: Variable not found: name=@myapp/config, schema=FRBAB1BF0ZBCS"
`;
exports[`Phase 4: Template System > 4.1 template set registers template 1`] = `
{
@@ -413,13 +417,29 @@ exports[`Phase 4: Template System > 4.4 template delete removes template 1`] = `
}
`;
exports[`Phase 4: Template System > 4.5 template get deleted template returns not found 1`] = `"Error: Template not found for schema: FRBAB1BF0ZBCS"`;
exports[`Phase 4: Template System > 4.5 template get deleted template returns not found 1`] = `
"(node:651720) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
Error: Template not found for schema: FRBAB1BF0ZBCS"
`;
exports[`Phase 7: Edge Cases > 7.1 get non-existent hash errors gracefully 1`] = `"Node not found: AAAAAAAAAAAAA"`;
exports[`Phase 7: Edge Cases > 7.1 get non-existent hash errors gracefully 1`] = `
"(node:651545) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
Node not found: AAAAAAAAAAAAA"
`;
exports[`Phase 7: Edge Cases > 7.3 var set empty name errors 1`] = `"Usage: ocas var set <name> <hash> [--tag <tag>...]"`;
exports[`Phase 7: Edge Cases > 7.3 var set empty name errors 1`] = `
"(node:651559) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
Usage: ocas var set <name> <hash> [--tag <tag>...]"
`;
exports[`Phase 7: Edge Cases > 7.4 var set name with invalid chars errors 1`] = `"Error: Invalid variable name "invalid name!": Name must follow @scope/name format (e.g. @myapp/config)"`;
exports[`Phase 7: Edge Cases > 7.4 var set name with invalid chars errors 1`] = `
"(node:651566) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
Error: Invalid variable name "invalid name!": Name must follow @scope/name format (e.g. @myapp/config)"
`;
exports[`Phase 7: Edge Cases > 7.5 no subcommand shows help text 1`] = `
"Usage: ocas [--home <path>] [--json] <command> [args]
@@ -1,3 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Phase 2: Schema Validation > 2.3 put against non-existent schema hash fails 1`] = `"Schema not found: AAAAAAAAAAAAA"`;
exports[`Phase 2: Schema Validation > 2.3 put against non-existent schema hash fails 1`] = `
"(node:651513) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
Schema not found: AAAAAAAAAAAAA"
`;
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
// ---- @ Alias Resolution Tests ----
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue, stripVolatile } from "./helpers";
const entrypoint = resolve(import.meta.dirname, "../dist/index.js");
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue } from "./helpers";
const entrypoint = resolve(import.meta.dirname, "../dist/index.js");
+2 -2
View File
@@ -1,11 +1,11 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Hash } from "@ocas/core";
import { bootstrap, validate } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
let testDir: string;
let storePath: string;
+3 -3
View File
@@ -1,3 +1,4 @@
import { execFileSync } from "node:child_process";
import {
mkdirSync,
mkdtempSync,
@@ -5,7 +6,6 @@ import {
rmSync,
writeFileSync,
} from "node:fs";
import { execFileSync } from "node:child_process";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import type { JSONSchema } from "@ocas/core";
@@ -59,7 +59,7 @@ export function runCli(
? [entrypoint, "--home", storePath, ...args]
: [entrypoint, ...args];
try {
const stdout = execFileSync("node", finalArgs, {
const stdout = execFileSync("node", ["--no-warnings", ...finalArgs], {
encoding: "utf-8",
timeout: 10000,
});
@@ -81,7 +81,7 @@ export function runCliWithStdin(
): { stdout: string; stderr: string; exitCode: number } {
const finalArgs = [entrypoint, "--home", storePath, ...args];
try {
const stdout = execFileSync("node", finalArgs, {
const stdout = execFileSync("node", ["--no-warnings", ...finalArgs], {
input: stdin,
encoding: "utf-8",
timeout: 10000,
+1 -1
View File
@@ -1,9 +1,9 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { BOOTSTRAP_STORE } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { envValue, runCli } from "./helpers.js";
let storePath: string;
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { envValue, runCli } from "./helpers.js";
const HASH_RE = /^[0-9A-HJKMNP-TV-Z]{13}$/;
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
let testDir: string;
let storePath: string;
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue } from "./helpers";
const entrypoint = resolve(import.meta.dirname, "../dist/index.js");
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue, stripVolatile } from "./helpers";
const entrypoint = resolve(import.meta.dirname, "../dist/index.js");
+2 -2
View File
@@ -1,10 +1,10 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { bootstrap } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue, putSchemaFile, runCli, runCliWithStdin } from "./helpers";
const entrypoint = resolve(import.meta.dirname, "../dist/index.js");
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue, putSchemaFile, runCli } from "./helpers";
// ---- Issue #50: Schema Validation in put Command ----
+2 -2
View File
@@ -1,11 +1,11 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Hash } from "@ocas/core";
import { bootstrap } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
let testDir: string;
let storePath: string;
+2 -2
View File
@@ -1,11 +1,11 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Hash, Store } from "@ocas/core";
import { bootstrap } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
// ---- Test helpers ----
+1 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
const usagePath = join(import.meta.dirname, "..", "prompts", "usage.md");
+2 -2
View File
@@ -1,11 +1,11 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Hash, Store } from "@ocas/core";
import { bootstrap } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
let testDir: string;
let storePath: string;
+2 -2
View File
@@ -1,11 +1,11 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { mkdirSync, rmSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Hash, Store } from "@ocas/core";
import { bootstrap, putSchema } from "@ocas/core";
import { openStore as openFsStore } from "@ocas/fs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
// ---- Test helpers ----
+2 -2
View File
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { envValue, stripVolatile } from "./helpers";
const entrypoint = resolve(import.meta.dirname, "../dist/index.js");
+1 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
function* walk(dir: string): Generator<string> {
for (const name of readdirSync(dir)) {
+1 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import type {
CasNode,
CasStore,
-2
View File
@@ -16,7 +16,6 @@
],
"dependencies": {
"@ocas/core": "workspace:*",
"better-sqlite3": "^12.10.0",
"cborg": "^4.2.3"
},
"repository": {
@@ -29,7 +28,6 @@
"url": "https://github.com/shazhou-ww/ocas/issues"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.13",
"@types/node": "^25.9.1"
}
}
+1 -1
View File
@@ -1,2 +1,2 @@
export { createFsStore, openStore, prepareStore } from "./store.js";
export { createSqliteVarStore } from "./sqlite-store.js";
export { createFsStore, openStore, prepareStore } from "./store.js";
+49 -41
View File
@@ -1,6 +1,6 @@
import { existsSync, mkdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import Database from "better-sqlite3";
import { DatabaseSync } from "node:sqlite";
import type {
CasStore,
Hash,
@@ -27,19 +27,31 @@ import {
varKey,
} from "@ocas/core";
function transaction<T>(db: DatabaseSync, fn: () => T): T {
db.exec("BEGIN");
try {
const r = fn();
db.exec("COMMIT");
return r;
} catch (e) {
db.exec("ROLLBACK");
throw e;
}
}
const DB_FILE = "_store.db";
const VARS_FILE = "_vars.jsonl";
const TAGS_FILE = "_tags.jsonl";
function openDb(dir: string): Database.Database {
function openDb(dir: string): DatabaseSync {
mkdirSync(dir, { recursive: true });
const db = new Database(join(dir, DB_FILE));
db.pragma("journal_mode = WAL");
db.pragma("foreign_keys = ON");
const db = new DatabaseSync(join(dir, DB_FILE));
db.exec("PRAGMA journal_mode = WAL");
db.exec("PRAGMA foreign_keys = ON");
return db;
}
function initVarTables(db: Database.Database): void {
function initVarTables(db: DatabaseSync): void {
db.exec(`
CREATE TABLE IF NOT EXISTS vars (
name TEXT NOT NULL,
@@ -67,7 +79,7 @@ function initVarTables(db: Database.Database): void {
`);
}
function initTagTables(db: Database.Database): void {
function initTagTables(db: DatabaseSync): void {
db.exec(`
CREATE TABLE IF NOT EXISTS tags (
target TEXT NOT NULL,
@@ -90,11 +102,7 @@ type StoredTag = {
created: number;
};
function migrateJsonlVars(
db: Database.Database,
dir: string,
_cas: CasStore,
): void {
function migrateJsonlVars(db: DatabaseSync, dir: string, _cas: CasStore): void {
const path = join(dir, VARS_FILE);
if (!existsSync(path)) return;
@@ -131,7 +139,7 @@ function migrateJsonlVars(
VALUES (?, ?, ?, ?, ?)
`);
const migrate = db.transaction(() => {
transaction(db, () => {
for (const rec of records.values()) {
insertVar.run(
rec.name,
@@ -147,10 +155,9 @@ function migrateJsonlVars(
}
}
});
migrate();
}
function migrateJsonlTags(db: Database.Database, dir: string): void {
function migrateJsonlTags(db: DatabaseSync, dir: string): void {
const path = join(dir, TAGS_FILE);
if (!existsSync(path)) return;
@@ -196,14 +203,13 @@ function migrateJsonlTags(db: Database.Database, dir: string): void {
VALUES (?, ?, ?, ?)
`);
const migrate = db.transaction(() => {
transaction(db, () => {
for (const tm of byTarget.values()) {
for (const tag of tm.values()) {
insertTag.run(tag.target, tag.key, tag.value, tag.created);
}
}
});
migrate();
}
// ── Row helpers ──
@@ -304,17 +310,17 @@ export function createSqliteVarStore(
// ── Transactional helpers ──
const txnSetVar = db.transaction(
(
name: string,
schema: Hash,
hash: Hash,
now: number,
tagsJson: string,
labelsJson: string,
isNew: boolean,
valueChanged: boolean,
) => {
function txnSetVar(
name: string,
schema: Hash,
hash: Hash,
now: number,
tagsJson: string,
labelsJson: string,
isNew: boolean,
valueChanged: boolean,
): void {
transaction(db, () => {
if (isNew) {
stmtInsertVar.run(name, schema, hash, now, now, tagsJson, labelsJson);
stmtInsertHistory.run(name, schema, hash, 0, now);
@@ -329,11 +335,11 @@ export function createSqliteVarStore(
} else {
stmtUpdateVar.run(hash, now, tagsJson, labelsJson, name, schema);
}
},
);
});
}
const txnTagOps = db.transaction(
(target: Hash, operations: TagOp[], now: number) => {
function txnTagOps(target: Hash, operations: TagOp[], now: number): void {
transaction(db, () => {
for (const op of operations) {
if (op.op === "set") {
// Use ON CONFLICT to preserve created time — but we need existing created
@@ -346,14 +352,16 @@ export function createSqliteVarStore(
stmtDeleteTag.run(target, op.key);
}
}
},
);
});
}
const txnUntag = db.transaction((target: Hash, keys: string[]) => {
for (const k of keys) {
stmtDeleteTag.run(target, k);
}
});
function txnUntag(target: Hash, keys: string[]): void {
transaction(db, () => {
for (const k of keys) {
stmtDeleteTag.run(target, k);
}
});
}
// ── VarStore implementation ──
const varStore: VarStore = {
@@ -516,7 +524,7 @@ export function createSqliteVarStore(
// Build dynamic query
const conditions: string[] = [];
const params: unknown[] = [];
const params: (string | number | null)[] = [];
if (options?.exactName !== undefined) {
conditions.push("name = ?");
@@ -650,7 +658,7 @@ export function createSqliteVarStore(
const limit = options?.limit;
let sql: string;
const params: unknown[] = [key];
const params: (string | number | null)[] = [key];
if (value !== undefined) {
sql = `SELECT target FROM tags WHERE key = ? AND value = ? ORDER BY ${sortCol} ${sortDir}`;
params.push(value);
+1 -1
View File
@@ -1,4 +1,3 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import {
existsSync,
mkdtempSync,
@@ -17,6 +16,7 @@ import {
computeSelfHash,
verify,
} from "@ocas/core";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { createFsStore, openStore } from "./store.js";
+1 -1
View File
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { existsSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { openStore } from "./store.js";
const T1 = "AAAAAAAAAAAAA";
+1 -1
View File
@@ -1,4 +1,3 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { existsSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -11,6 +10,7 @@ import {
TagLabelConflictError,
VariableNotFoundError,
} from "@ocas/core";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { openStore } from "./store.js";
const META_TYPE_KEY = Symbol.for("@ocas/core/bootstrap-store");