@@ -12,9 +12,11 @@ import type {
1212import { openDatabase } from './lib/db' ;
1313import { runMigrations } from './lib/migrate' ;
1414import { MetaHelper } from './lib/meta' ;
15+ import { num } from './lib/bigint' ;
1516import { v001 } from './migrations/v001' ;
1617import { SqliteTeamStore } from './stores/team' ;
1718import { SqliteProjectsStore } from './stores/projects' ;
19+ import { SqliteProjectScopedStore } from './stores/project-scoped' ;
1820
1921const ALL_MIGRATIONS = [ v001 ] ;
2022
@@ -61,30 +63,66 @@ export class SqliteStore implements Store {
6163
6264 // --- Project scoping ---
6365
64- project ( _projectId : number ) : ProjectScopedStore {
65- throw new Error ( 'Not implemented yet (Phase 6)' ) ;
66+ project ( projectId : number ) : ProjectScopedStore {
67+ this . requireDb ( ) ;
68+ let scoped = this . scopedCache . get ( projectId ) ;
69+ if ( ! scoped ) {
70+ scoped = new SqliteProjectScopedStore ( this . db ! , projectId ) ;
71+ this . scopedCache . set ( projectId , scoped ) ;
72+ }
73+ return scoped ;
6674 }
6775
6876 // --- Edges ---
6977
70- createEdge ( _projectId : number , _edge : Edge ) : void {
71- throw new Error ( 'Not implemented yet (Phase 6)' ) ;
78+ createEdge ( projectId : number , edge : Edge ) : void {
79+ this . requireDb ( ) ;
80+ this . db ! . prepare ( `
81+ INSERT OR IGNORE INTO edges (project_id, from_graph, from_id, to_graph, to_id, kind)
82+ VALUES (?, ?, ?, ?, ?, ?)
83+ ` ) . run ( projectId , edge . fromGraph , edge . fromId , edge . toGraph , edge . toId , edge . kind ) ;
7284 }
7385
74- deleteEdge ( _projectId : number , _edge : Edge ) : void {
75- throw new Error ( 'Not implemented yet (Phase 6)' ) ;
86+ deleteEdge ( projectId : number , edge : Edge ) : void {
87+ this . requireDb ( ) ;
88+ this . db ! . prepare ( `
89+ DELETE FROM edges
90+ WHERE project_id = ? AND from_graph = ? AND from_id = ? AND to_graph = ? AND to_id = ? AND kind = ?
91+ ` ) . run ( projectId , edge . fromGraph , edge . fromId , edge . toGraph , edge . toId , edge . kind ) ;
7692 }
7793
78- listEdges ( _filter : EdgeFilter & { projectId ?: number } ) : Edge [ ] {
79- throw new Error ( 'Not implemented yet (Phase 6)' ) ;
94+ listEdges ( filter : EdgeFilter & { projectId ?: number } ) : Edge [ ] {
95+ this . requireDb ( ) ;
96+ const conditions : string [ ] = [ ] ;
97+ const params : unknown [ ] = [ ] ;
98+
99+ if ( filter . projectId !== undefined ) { conditions . push ( 'project_id = ?' ) ; params . push ( filter . projectId ) ; }
100+ if ( filter . fromGraph ) { conditions . push ( 'from_graph = ?' ) ; params . push ( filter . fromGraph ) ; }
101+ if ( filter . fromId !== undefined ) { conditions . push ( 'from_id = ?' ) ; params . push ( filter . fromId ) ; }
102+ if ( filter . toGraph ) { conditions . push ( 'to_graph = ?' ) ; params . push ( filter . toGraph ) ; }
103+ if ( filter . toId !== undefined ) { conditions . push ( 'to_id = ?' ) ; params . push ( filter . toId ) ; }
104+ if ( filter . kind ) { conditions . push ( 'kind = ?' ) ; params . push ( filter . kind ) ; }
105+
106+ const where = conditions . length > 0 ? `WHERE ${ conditions . join ( ' AND ' ) } ` : '' ;
107+ const rows = this . db ! . prepare (
108+ `SELECT from_graph, from_id, to_graph, to_id, kind FROM edges ${ where } `
109+ ) . all ( ...params ) as Array < Record < string , unknown > > ;
110+
111+ return rows . map ( r => ( {
112+ fromGraph : r . from_graph as GraphName ,
113+ fromId : num ( r . from_id as bigint ) ,
114+ toGraph : r . to_graph as GraphName ,
115+ toId : num ( r . to_id as bigint ) ,
116+ kind : r . kind as string ,
117+ } ) ) ;
80118 }
81119
82- findIncomingEdges ( _targetGraph : GraphName , _targetId : number , _projectId ?: number ) : Edge [ ] {
83- throw new Error ( 'Not implemented yet (Phase 6)' ) ;
120+ findIncomingEdges ( targetGraph : GraphName , targetId : number , projectId ?: number ) : Edge [ ] {
121+ return this . listEdges ( { toGraph : targetGraph , toId : targetId , projectId } ) ;
84122 }
85123
86- findOutgoingEdges ( _fromGraph : GraphName , _fromId : number , _projectId ?: number ) : Edge [ ] {
87- throw new Error ( 'Not implemented yet (Phase 6)' ) ;
124+ findOutgoingEdges ( fromGraph : GraphName , fromId : number , projectId ?: number ) : Edge [ ] {
125+ return this . listEdges ( { fromGraph , fromId , projectId } ) ;
88126 }
89127
90128 // --- Transaction ---
@@ -113,7 +151,7 @@ export class SqliteStore implements Store {
113151
114152 // --- Internal ---
115153
116- /** Get the raw database handle (for sub-stores) */
154+ /** Get the raw database handle (for sub-stores and tests ) */
117155 getDb ( ) : Database . Database {
118156 this . requireDb ( ) ;
119157 return this . db ! ;
0 commit comments