|
| 1 | +import Database from 'better-sqlite3'; |
| 2 | +import type { |
| 3 | + ProjectsStore, |
| 4 | + ProjectCreate, |
| 5 | + ProjectPatch, |
| 6 | + ProjectRecord, |
| 7 | + PaginationOptions, |
| 8 | +} from '../../types'; |
| 9 | +import { MetaHelper } from '../lib/meta'; |
| 10 | +import { num, now } from '../lib/bigint'; |
| 11 | + |
| 12 | +export class SqliteProjectsStore implements ProjectsStore { |
| 13 | + private meta: MetaHelper; |
| 14 | + private stmts: ReturnType<SqliteProjectsStore['prepareStatements']>; |
| 15 | + |
| 16 | + constructor(private db: Database.Database) { |
| 17 | + this.meta = new MetaHelper(db, 'projects'); |
| 18 | + this.stmts = this.prepareStatements(); |
| 19 | + } |
| 20 | + |
| 21 | + private prepareStatements() { |
| 22 | + return { |
| 23 | + insert: this.db.prepare(` |
| 24 | + INSERT INTO projects (slug, name, directory, created_at, updated_at) |
| 25 | + VALUES (?, ?, ?, ?, ?) |
| 26 | + `), |
| 27 | + update: this.db.prepare(` |
| 28 | + UPDATE projects SET name = ?, directory = ?, updated_at = ? |
| 29 | + WHERE id = ? |
| 30 | + `), |
| 31 | + delete: this.db.prepare('DELETE FROM projects WHERE id = ?'), |
| 32 | + getById: this.db.prepare('SELECT * FROM projects WHERE id = ?'), |
| 33 | + getBySlug: this.db.prepare('SELECT * FROM projects WHERE slug = ?'), |
| 34 | + list: this.db.prepare('SELECT * FROM projects ORDER BY name LIMIT ? OFFSET ?'), |
| 35 | + count: this.db.prepare('SELECT COUNT(*) AS c FROM projects'), |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + private toRecord(row: Record<string, unknown>): ProjectRecord { |
| 40 | + return { |
| 41 | + id: num(row.id as bigint), |
| 42 | + slug: row.slug as string, |
| 43 | + name: row.name as string, |
| 44 | + directory: row.directory as string, |
| 45 | + createdAt: num(row.created_at as bigint), |
| 46 | + updatedAt: num(row.updated_at as bigint), |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + create(data: ProjectCreate): ProjectRecord { |
| 51 | + const ts = now(); |
| 52 | + const result = this.stmts.insert.run(data.slug, data.name, data.directory, ts, ts); |
| 53 | + return this.get(num(result.lastInsertRowid))!; |
| 54 | + } |
| 55 | + |
| 56 | + update(projectId: number, patch: ProjectPatch): ProjectRecord { |
| 57 | + const existing = this.get(projectId); |
| 58 | + if (!existing) throw new Error(`Project ${projectId} not found`); |
| 59 | + |
| 60 | + this.stmts.update.run( |
| 61 | + patch.name ?? existing.name, |
| 62 | + patch.directory ?? existing.directory, |
| 63 | + now(), |
| 64 | + projectId, |
| 65 | + ); |
| 66 | + return this.get(projectId)!; |
| 67 | + } |
| 68 | + |
| 69 | + delete(projectId: number): void { |
| 70 | + // CASCADE on FK handles: knowledge, tasks, epics, skills, code, docs, files, edges, tags |
| 71 | + // Cleanup triggers on each entity table handle: vec0, edges, attachments |
| 72 | + // But we need to clean vec0 for entities BEFORE cascade deletes them, |
| 73 | + // because triggers fire per-row — which is fine, SQLite does this automatically. |
| 74 | + this.stmts.delete.run(projectId); |
| 75 | + } |
| 76 | + |
| 77 | + get(projectId: number): ProjectRecord | null { |
| 78 | + const row = this.stmts.getById.get(projectId) as Record<string, unknown> | undefined; |
| 79 | + return row ? this.toRecord(row) : null; |
| 80 | + } |
| 81 | + |
| 82 | + getBySlug(slug: string): ProjectRecord | null { |
| 83 | + const row = this.stmts.getBySlug.get(slug) as Record<string, unknown> | undefined; |
| 84 | + return row ? this.toRecord(row) : null; |
| 85 | + } |
| 86 | + |
| 87 | + list(pagination?: PaginationOptions): { results: ProjectRecord[]; total: number } { |
| 88 | + const limit = pagination?.limit ?? 50; |
| 89 | + const offset = pagination?.offset ?? 0; |
| 90 | + const rows = this.stmts.list.all(limit, offset) as Array<Record<string, unknown>>; |
| 91 | + const total = num((this.stmts.count.get() as { c: bigint }).c); |
| 92 | + return { results: rows.map(r => this.toRecord(r)), total }; |
| 93 | + } |
| 94 | + |
| 95 | + getMeta(key: string): string | null { return this.meta.getMeta(key); } |
| 96 | + setMeta(key: string, value: string): void { this.meta.setMeta(key, value); } |
| 97 | + deleteMeta(key: string): void { this.meta.deleteMeta(key); } |
| 98 | +} |
0 commit comments