docs: enterprise refactor plan with ralph specs
This commit is contained in:
43
dist/db/connection.js
vendored
Normal file
43
dist/db/connection.js
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
"use strict";
|
||||
/**
|
||||
* ABE Database Connection
|
||||
* Singleton SQLite connection using better-sqlite3.
|
||||
* Runs migrations on first access.
|
||||
*/
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getDb = getDb;
|
||||
exports.setDb = setDb;
|
||||
exports.closeDb = closeDb;
|
||||
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const migrations_1 = require("./migrations");
|
||||
let _db = null;
|
||||
function getDb() {
|
||||
if (_db)
|
||||
return _db;
|
||||
const dbPath = process.env['ABE_DB_PATH'] ?? path_1.default.join(process.cwd(), 'data', 'abe.db');
|
||||
const dir = path_1.default.dirname(dbPath);
|
||||
if (!fs_1.default.existsSync(dir)) {
|
||||
fs_1.default.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
_db = new better_sqlite3_1.default(dbPath);
|
||||
_db.pragma('journal_mode = WAL');
|
||||
_db.pragma('foreign_keys = ON');
|
||||
(0, migrations_1.runMigrations)(_db);
|
||||
return _db;
|
||||
}
|
||||
/** For testing — inject a custom (in-memory) database instance. */
|
||||
function setDb(db) {
|
||||
_db = db;
|
||||
}
|
||||
/** Close and reset. Used in tests. */
|
||||
function closeDb() {
|
||||
if (_db) {
|
||||
_db.close();
|
||||
_db = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user