naisys
Version:
Node.js Autonomous Intelligence System
39 lines • 1.09 kB
JavaScript
import * as fs from "fs";
import { open } from "sqlite";
import sqlite3 from "sqlite3";
import * as pathService from "./pathService.js";
export async function initDatabase(filepath) {
const hostPath = filepath.toHostPath();
if (fs.existsSync(hostPath)) {
return false;
}
pathService.ensureFileDirExists(filepath);
const db = await open({
filename: hostPath,
driver: sqlite3.Database,
mode: sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
});
await db.close();
return true;
}
export async function openDatabase(filepath) {
const hostPath = filepath.toHostPath();
const db = await open({
filename: hostPath,
driver: sqlite3.Database,
mode: sqlite3.OPEN_READWRITE,
});
// Turn foreign key constraints on
await db.exec("PRAGMA foreign_keys = ON");
return db;
}
export async function usingDatabase(filepath, run) {
const db = await openDatabase(filepath);
try {
return await run(db);
}
finally {
await db.close();
}
}
//# sourceMappingURL=dbUtils.js.map