11import { STORE_KEYS , type StorageBackend } from './storage.js' ;
22
33export class MemoryStorage implements StorageBackend {
4- private stores = new Map < string , Map < string , any > > ( ) ;
4+ private stores = new Map < string , Map < any , any > > ( ) ;
55
6- private getStore ( name : string ) : Map < string , any > {
6+ async init ( ) : Promise < void > { }
7+
8+ private getStore ( name : string ) : Map < any , any > {
9+ if ( ! STORE_KEYS [ name ] ) throw new Error ( `Unknown store: ${ name } ` ) ;
710 let store = this . stores . get ( name ) ;
811 if ( ! store ) {
912 store = new Map ( ) ;
@@ -12,16 +15,16 @@ export class MemoryStorage implements StorageBackend {
1215 return store ;
1316 }
1417
15- private getKey ( storeName : string , value : any ) : string {
18+ private getKey ( storeName : string , value : any ) : any {
1619 const keyField = STORE_KEYS [ storeName ] ;
17- if ( keyField && value && typeof value === 'object' ) {
18- return String ( value [ keyField ] ) ;
19- }
20- return String ( value ?. id ?? value ?. key ?? '' ) ;
20+ if ( ! keyField ) throw new Error ( `Unknown store: ${ storeName } ` ) ;
21+ const key = value ?. [ keyField ] ;
22+ if ( key === undefined || key === null ) throw new Error ( `Missing key field ' ${ keyField } ' in value for store ' ${ storeName } '` ) ;
23+ return key ;
2124 }
2225
23- async get ( store : string , key : string ) : Promise < any | undefined > {
24- return structuredClone ( this . getStore ( store ) . get ( String ( key ) ) ) ;
26+ async get ( store : string , key : any ) : Promise < any | undefined > {
27+ return structuredClone ( this . getStore ( store ) . get ( key ) ) ;
2528 }
2629
2730 async getAll ( store : string ) : Promise < any [ ] > {
@@ -49,8 +52,8 @@ export class MemoryStorage implements StorageBackend {
4952 }
5053 }
5154
52- async del ( store : string , key : string ) : Promise < void > {
53- this . getStore ( store ) . delete ( String ( key ) ) ;
55+ async del ( store : string , key : any ) : Promise < void > {
56+ this . getStore ( store ) . delete ( key ) ;
5457 }
5558
5659 async clear ( store : string ) : Promise < void > {
0 commit comments