diff options
Diffstat (limited to 'src/main/db/index.ts')
| -rw-r--r-- | src/main/db/index.ts | 31 |
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 @@ | |||
| 1 | import Database from "better-sqlite3"; | ||
| 2 | import { app } from "electron"; | ||
| 3 | import path from "node:path"; | ||
| 4 | import fs from "node:fs"; | ||
| 5 | import { initSchema } from "./schema"; | ||
| 6 | |||
| 7 | let db: Database.Database | null = null; | ||
| 8 | |||
| 9 | export 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 | |||
| 26 | export function closeDb() { | ||
| 27 | if (db) { | ||
| 28 | db.close(); | ||
| 29 | db = null; | ||
| 30 | } | ||
| 31 | } | ||
