@@ -74,16 +74,7 @@ export class Database {
7474 const db = await this . dbPromise ;
7575
7676 // Ensure the value is a valid IndexedDB key (string, number, Date, or array of those)
77- let entityKey = renderer . value ;
78- if ( typeof entityKey !== 'string' && typeof entityKey !== 'number' ) {
79- // If not valid, convert to string (e.g., JSON.stringify)
80- try {
81- entityKey = JSON . stringify ( entityKey ) ;
82- } catch {
83- entityKey = String ( entityKey ) ;
84- }
85- console . warn ( 'Converted entity value to string for IndexedDB key:' , entityKey ) ;
86- }
77+ const entityKey = this . normalizeKey ( renderer . value ) ;
8778
8879 // Add the entity to the entities object store
8980 await db
@@ -148,15 +139,7 @@ export class Database {
148139 } [ ] ,
149140 ) : Promise < GenericIdentifierType > {
150141 // Ensure the value is a valid IndexedDB key (string, number, Date, or array of those)
151- let entityKey = value ;
152- if ( typeof entityKey !== 'string' && typeof entityKey !== 'number' ) {
153- try {
154- entityKey = JSON . stringify ( entityKey ) ;
155- } catch {
156- entityKey = String ( entityKey ) ;
157- }
158- console . warn ( 'Converted entity value to string for IndexedDB key (get):' , entityKey ) ;
159- }
142+ const entityKey = this . normalizeKey ( value ) ;
160143 // Try to get the entity from the database
161144 try {
162145 const db = await this . dbPromise ;
@@ -211,15 +194,7 @@ export class Database {
211194 const db = await this . dbPromise ;
212195
213196 // Ensure the value is a valid IndexedDB key (string, number, Date, or array of those)
214- let entityKey = value ;
215- if ( typeof entityKey !== 'string' && typeof entityKey !== 'number' ) {
216- try {
217- entityKey = JSON . stringify ( entityKey ) ;
218- } catch {
219- entityKey = String ( entityKey ) ;
220- }
221- console . warn ( 'Converted entity value to string for IndexedDB key (delete):' , entityKey ) ;
222- }
197+ const entityKey = this . normalizeKey ( value ) ;
223198
224199 // Delete the entity
225200 await db . delete ( 'entities' , entityKey ) ;
@@ -238,6 +213,19 @@ export class Database {
238213 await tx . done ;
239214 }
240215
216+ private normalizeKey ( value : string ) {
217+ let entityKey = value ;
218+ if ( typeof entityKey !== 'string' && typeof entityKey !== 'number' ) {
219+ try {
220+ entityKey = JSON . stringify ( entityKey ) ;
221+ } catch {
222+ entityKey = String ( entityKey ) ;
223+ }
224+ console . warn ( 'Converted entity value to string for IndexedDB key (delete):' , entityKey ) ;
225+ }
226+ return entityKey ;
227+ }
228+
241229 /**
242230 * Clears all entities from the database.
243231 * @returns {Promise<void> } A promise that resolves when all entities have been deleted.
0 commit comments