11import { readFileSync , writeFileSync , mkdirSync , existsSync , rmSync } from 'node:fs' ;
22import { join } from 'node:path' ;
3- import { STORE_KEYS , type StorageBackend } from './storage.js' ;
3+ import { STORE_KEYS , type StorageBackend , type StoreName } from './storage.js' ;
44
55export class FileSystemStorage implements StorageBackend {
66 constructor ( private dirPath : string ) {
@@ -9,53 +9,51 @@ export class FileSystemStorage implements StorageBackend {
99
1010 async init ( ) : Promise < void > { }
1111
12- private filePath ( store : string ) : string {
13- if ( ! STORE_KEYS [ store ] ) throw new Error ( `Unknown store: ${ store } ` ) ;
12+ private filePath ( store : StoreName ) : string {
1413 return join ( this . dirPath , `${ store } .json` ) ;
1514 }
1615
17- private readStore ( store : string ) : Record < string , any > {
16+ private readStore ( store : StoreName ) : Record < string , any > {
1817 const path = this . filePath ( store ) ;
1918 if ( ! existsSync ( path ) ) return { } ;
2019 return JSON . parse ( readFileSync ( path , 'utf-8' ) ) ;
2120 }
2221
23- private writeStore ( store : string , data : Record < string , any > ) : void {
22+ private writeStore ( store : StoreName , data : Record < string , any > ) : void {
2423 writeFileSync ( this . filePath ( store ) , JSON . stringify ( data , null , 2 ) ) ;
2524 }
2625
27- private getKey ( store : string , value : any ) : string {
26+ private getKey ( store : StoreName , value : any ) : string {
2827 const keyField = STORE_KEYS [ store ] ;
29- if ( ! keyField ) throw new Error ( `Unknown store: ${ store } ` ) ;
3028 const key = value ?. [ keyField ] ;
3129 if ( key === undefined || key === null ) throw new Error ( `Missing key field '${ keyField } ' in value for store '${ store } '` ) ;
3230 return String ( key ) ;
3331 }
3432
35- async get ( store : string , key : any ) : Promise < any | undefined > {
33+ async get ( store : StoreName , key : any ) : Promise < any | undefined > {
3634 const data = this . readStore ( store ) ;
3735 const k = String ( key ) ;
3836 return data [ k ] ? structuredClone ( data [ k ] ) : undefined ;
3937 }
4038
41- async getAll ( store : string ) : Promise < any [ ] > {
39+ async getAll ( store : StoreName ) : Promise < any [ ] > {
4240 return Object . values ( this . readStore ( store ) ) . map ( v => structuredClone ( v ) ) ;
4341 }
4442
45- async getAllByIndex ( store : string , index : string , value : any ) : Promise < any [ ] > {
43+ async getAllByIndex ( store : StoreName , index : string , value : any ) : Promise < any [ ] > {
4644 return Object . values ( this . readStore ( store ) )
4745 . filter ( record => record [ index ] === value )
4846 . map ( v => structuredClone ( v ) ) ;
4947 }
5048
51- async put ( store : string , value : any ) : Promise < void > {
49+ async put ( store : StoreName , value : any ) : Promise < void > {
5250 const data = this . readStore ( store ) ;
5351 const key = this . getKey ( store , value ) ;
5452 data [ key ] = structuredClone ( value ) ;
5553 this . writeStore ( store , data ) ;
5654 }
5755
58- async putAll ( store : string , values : any [ ] ) : Promise < void > {
56+ async putAll ( store : StoreName , values : any [ ] ) : Promise < void > {
5957 const data = this . readStore ( store ) ;
6058 for ( const value of values ) {
6159 const key = this . getKey ( store , value ) ;
@@ -64,28 +62,28 @@ export class FileSystemStorage implements StorageBackend {
6462 this . writeStore ( store , data ) ;
6563 }
6664
67- async del ( store : string , key : any ) : Promise < void > {
65+ async del ( store : StoreName , key : any ) : Promise < void > {
6866 const data = this . readStore ( store ) ;
6967 delete data [ String ( key ) ] ;
7068 this . writeStore ( store , data ) ;
7169 }
7270
73- async clear ( store : string ) : Promise < void > {
71+ async clear ( store : StoreName ) : Promise < void > {
7472 this . writeStore ( store , { } ) ;
7573 }
7674
7775 async clearAll ( ) : Promise < void > {
78- for ( const storeName of Object . keys ( STORE_KEYS ) ) {
76+ for ( const storeName of Object . keys ( STORE_KEYS ) as StoreName [ ] ) {
7977 const path = this . filePath ( storeName ) ;
8078 if ( existsSync ( path ) ) rmSync ( path ) ;
8179 }
8280 }
8381
84- async count ( store : string ) : Promise < number > {
82+ async count ( store : StoreName ) : Promise < number > {
8583 return Object . keys ( this . readStore ( store ) ) . length ;
8684 }
8785
88- async iterate ( store : string , callback : ( value : any ) => boolean | void ) : Promise < void > {
86+ async iterate ( store : StoreName , callback : ( value : any ) => boolean | void ) : Promise < void > {
8987 for ( const value of Object . values ( this . readStore ( store ) ) ) {
9088 if ( callback ( structuredClone ( value ) ) === false ) break ;
9189 }
0 commit comments