44
55import { DevlogEntry , DevlogFilter , DevlogStats , DevlogStatus , DevlogType , DevlogPriority , DevlogId } from "@devlog/types" ;
66import { StorageProvider } from "./storage-provider.js" ;
7- import { IdManager } from "../utils/id-manager.js" ;
87
98export class SQLiteStorageProvider implements StorageProvider {
109 private db : any = null ;
@@ -79,7 +78,8 @@ export class SQLiteStorageProvider implements StorageProvider {
7978 try {
8079 this . db . exec ( `
8180 CREATE TABLE IF NOT EXISTS devlog_entries (
82- id TEXT PRIMARY KEY,
81+ id INTEGER PRIMARY KEY AUTOINCREMENT,
82+ key_field TEXT UNIQUE NOT NULL,
8383 title TEXT NOT NULL,
8484 type TEXT NOT NULL,
8585 description TEXT NOT NULL,
@@ -99,6 +99,7 @@ export class SQLiteStorageProvider implements StorageProvider {
9999 notes TEXT -- JSON array
100100 );
101101
102+ CREATE INDEX IF NOT EXISTS idx_devlog_key ON devlog_entries(key_field);
102103 CREATE INDEX IF NOT EXISTS idx_devlog_status ON devlog_entries(status);
103104 CREATE INDEX IF NOT EXISTS idx_devlog_type ON devlog_entries(type);
104105 CREATE INDEX IF NOT EXISTS idx_devlog_priority ON devlog_entries(priority);
@@ -139,17 +140,16 @@ export class SQLiteStorageProvider implements StorageProvider {
139140 }
140141
141142 async exists ( id : DevlogId ) : Promise < boolean > {
142- const idStr = IdManager . idToString ( id ) ;
143- console . log ( `[SQLiteStorage] Checking if entry exists: ${ idStr } ` ) ;
143+ console . log ( `[SQLiteStorage] Checking if entry exists: ${ id } ` ) ;
144144 if ( ! this . db ) {
145- console . error ( `[SQLiteStorage] Database not initialized when checking exists for: ${ idStr } ` ) ;
145+ console . error ( `[SQLiteStorage] Database not initialized when checking exists for: ${ id } ` ) ;
146146 throw new Error ( "Database not initialized" ) ;
147147 }
148148
149149 try {
150150 const stmt = this . db . prepare ( "SELECT 1 FROM devlog_entries WHERE id = ?" ) ;
151- const result = stmt . get ( idStr ) !== undefined ;
152- console . log ( `[SQLiteStorage] Entry exists result for ${ idStr } : ${ result } ` ) ;
151+ const result = stmt . get ( id ) !== undefined ;
152+ console . log ( `[SQLiteStorage] Entry exists result for ${ id } : ${ result } ` ) ;
153153 return result ;
154154 } catch ( error : any ) {
155155 console . error ( `[SQLiteStorage] Error checking if entry exists:` , error ) ;
@@ -158,23 +158,22 @@ export class SQLiteStorageProvider implements StorageProvider {
158158 }
159159
160160 async get ( id : DevlogId ) : Promise < DevlogEntry | null > {
161- const idStr = IdManager . idToString ( id ) ;
162- console . log ( `[SQLiteStorage] Getting entry: ${ idStr } ` ) ;
161+ console . log ( `[SQLiteStorage] Getting entry: ${ id } ` ) ;
163162 if ( ! this . db ) {
164- console . error ( `[SQLiteStorage] Database not initialized when getting: ${ idStr } ` ) ;
163+ console . error ( `[SQLiteStorage] Database not initialized when getting: ${ id } ` ) ;
165164 throw new Error ( "Database not initialized" ) ;
166165 }
167166
168167 try {
169168 const stmt = this . db . prepare ( "SELECT * FROM devlog_entries WHERE id = ?" ) ;
170- const row = stmt . get ( idStr ) as any ;
169+ const row = stmt . get ( id ) as any ;
171170
172171 if ( ! row ) {
173- console . log ( `[SQLiteStorage] Entry not found: ${ idStr } ` ) ;
172+ console . log ( `[SQLiteStorage] Entry not found: ${ id } ` ) ;
174173 return null ;
175174 }
176175
177- console . log ( `[SQLiteStorage] Found entry: ${ idStr } ` ) ;
176+ console . log ( `[SQLiteStorage] Found entry: ${ id } ` ) ;
178177 return this . rowToDevlogEntry ( row ) ;
179178 } catch ( error : any ) {
180179 console . error ( `[SQLiteStorage] Error getting entry:` , error ) ;
@@ -183,24 +182,24 @@ export class SQLiteStorageProvider implements StorageProvider {
183182 }
184183
185184 async save ( entry : DevlogEntry ) : Promise < void > {
186- const idStr = IdManager . idToString ( entry . id ) ;
187- console . log ( `[SQLiteStorage] Saving entry: ${ idStr } - ${ entry . title } ` ) ;
185+ console . log ( `[SQLiteStorage] Saving entry: ${ entry . id } - ${ entry . title } ` ) ;
188186 if ( ! this . db ) {
189- console . error ( `[SQLiteStorage] Database not initialized when saving: ${ idStr } ` ) ;
187+ console . error ( `[SQLiteStorage] Database not initialized when saving: ${ entry . id } ` ) ;
190188 throw new Error ( "Database not initialized" ) ;
191189 }
192190
193191 try {
194192 const stmt = this . db . prepare ( `
195193 INSERT OR REPLACE INTO devlog_entries (
196- id, title, type, description, status, priority, created_at, updated_at,
194+ id, key_field, title, type, description, status, priority, created_at, updated_at,
197195 estimated_hours, actual_hours, assignee, tags, files, related_devlogs,
198196 context, ai_context, external_references, notes
199- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
197+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
200198 ` ) ;
201199
202200 stmt . run (
203- idStr ,
201+ entry . id ,
202+ entry . key ,
204203 entry . title ,
205204 entry . type ,
206205 entry . description ,
@@ -220,19 +219,18 @@ export class SQLiteStorageProvider implements StorageProvider {
220219 JSON . stringify ( entry . notes )
221220 ) ;
222221
223- console . log ( `[SQLiteStorage] Successfully saved entry: ${ idStr } ` ) ;
222+ console . log ( `[SQLiteStorage] Successfully saved entry: ${ entry . id } ` ) ;
224223 } catch ( error : any ) {
225224 console . error ( `[SQLiteStorage] Error saving entry:` , error ) ;
226225 throw error ;
227226 }
228227 }
229228
230229 async delete ( id : DevlogId ) : Promise < void > {
231- const idStr = IdManager . idToString ( id ) ;
232230 if ( ! this . db ) throw new Error ( "Database not initialized" ) ;
233231
234232 const stmt = this . db . prepare ( "DELETE FROM devlog_entries WHERE id = ?" ) ;
235- stmt . run ( idStr ) ;
233+ stmt . run ( id ) ;
236234 }
237235
238236 async list ( filter ?: DevlogFilter ) : Promise < DevlogEntry [ ] > {
@@ -364,7 +362,8 @@ export class SQLiteStorageProvider implements StorageProvider {
364362
365363 private rowToDevlogEntry ( row : any ) : DevlogEntry {
366364 return {
367- id : IdManager . stringToId ( row . id ) ,
365+ id : row . id ,
366+ key : row . key_field ,
368367 title : row . title ,
369368 type : row . type ,
370369 description : row . description ,
0 commit comments