aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/db/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/db/index.ts')
-rw-r--r--src/main/db/index.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main/db/index.ts b/src/main/db/index.ts
new file mode 100644
index 0000000..a77cdd4
--- /dev/null
+++ b/src/main/db/index.ts
@@ -0,0 +1,31 @@
1import Database from "better-sqlite3";
2import { app } from "electron";
3import path from "node:path";
4import fs from "node:fs";
5import { initSchema } from "./schema";
6
7let db: Database.Database | null = null;
8
9export function getDb(): Database.Database {
10 if (db) return db;
11
12 const dbDir = app.getPath("userData");
13 if (!fs.existsSync(dbDir)) {
14 fs.mkdirSync(dbDir, { recursive: true });
15 }
16
17 const dbPath = path.join(dbDir, "claude-flow.db");
18 db = new Database(dbPath);
19 db.pragma("journal_mode = WAL");
20 db.pragma("foreign_keys = ON");
21
22 initSchema(db);
23 return db;
24}
25
26export function closeDb() {
27 if (db) {
28 db.close();
29 db = null;
30 }
31}