@@ -28,6 +28,27 @@ export class MemoryKvPrimitives implements KvPrimitives {
2828 }
2929 }
3030 }
31+
32+ static fromJSON ( json : Record < string , any > ) : MemoryKvPrimitives {
33+ const result = new MemoryKvPrimitives ( ) ;
34+ for ( const key of Object . keys ( json ) ) {
35+ result . store . set ( key , json [ key ] ) ;
36+ }
37+ return result ;
38+ }
39+
40+ /**
41+ * Create a new MemoryKvPrimitives instance from a file and initialize it
42+ */
43+ static async fromFile (
44+ filePath : string ,
45+ options : { throttleMs ?: number } = { } ,
46+ ) : Promise < MemoryKvPrimitives > {
47+ const instance = new MemoryKvPrimitives ( filePath , options ) ;
48+ await instance . init ( ) ;
49+ return instance ;
50+ }
51+
3152 clear ( ) : Promise < void > {
3253 this . store . clear ( ) ;
3354 return Promise . resolve ( ) ;
@@ -62,23 +83,6 @@ export class MemoryKvPrimitives implements KvPrimitives {
6283 }
6384 }
6485
65- /**
66- * Persist the current state to disk
67- */
68- private async persistToDisk ( ) : Promise < void > {
69- if ( ! this . filePath ) return ;
70-
71- try {
72- const jsonData = this . toJSON ( ) ;
73- await Deno . writeTextFile (
74- this . filePath ,
75- JSON . stringify ( jsonData , null , 2 ) ,
76- ) ;
77- } catch ( error ) {
78- console . error ( `Failed to persist KV store to ${ this . filePath } :` , error ) ;
79- }
80- }
81-
8286 batchGet ( keys : KvKey [ ] ) : Promise < any [ ] > {
8387 return Promise . resolve (
8488 keys . map ( ( key ) => this . store . get ( key . join ( memoryKeySeparator ) ) ) ,
@@ -125,14 +129,6 @@ export class MemoryKvPrimitives implements KvPrimitives {
125129 return result ;
126130 }
127131
128- static fromJSON ( json : Record < string , any > ) : MemoryKvPrimitives {
129- const result = new MemoryKvPrimitives ( ) ;
130- for ( const key of Object . keys ( json ) ) {
131- result . store . set ( key , json [ key ] ) ;
132- }
133- return result ;
134- }
135-
136132 async * query ( options : KvQueryOptions ) : AsyncIterableIterator < KV > {
137133 const prefix = options . prefix ?. join ( memoryKeySeparator ) ;
138134 const sortedKeys = [ ...this . store . keys ( ) ] . sort ( ) ;
@@ -155,14 +151,19 @@ export class MemoryKvPrimitives implements KvPrimitives {
155151 }
156152
157153 /**
158- * Create a new MemoryKvPrimitives instance from a file and initialize it
154+ * Persist the current state to disk
159155 */
160- static async fromFile (
161- filePath : string ,
162- options : { throttleMs ?: number } = { } ,
163- ) : Promise < MemoryKvPrimitives > {
164- const instance = new MemoryKvPrimitives ( filePath , options ) ;
165- await instance . init ( ) ;
166- return instance ;
156+ private async persistToDisk ( ) : Promise < void > {
157+ if ( ! this . filePath ) return ;
158+
159+ try {
160+ const jsonData = this . toJSON ( ) ;
161+ await Deno . writeTextFile (
162+ this . filePath ,
163+ JSON . stringify ( jsonData , null , 2 ) ,
164+ ) ;
165+ } catch ( error ) {
166+ console . error ( `Failed to persist KV store to ${ this . filePath } :` , error ) ;
167+ }
167168 }
168169}
0 commit comments