@objectstack/driver-memory 3.0.9 → 3.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
 
2
- > @objectstack/driver-memory@3.0.9 build /home/runner/work/spec/spec/packages/plugins/driver-memory
2
+ > @objectstack/driver-memory@3.0.11 build /home/runner/work/spec/spec/packages/plugins/driver-memory
3
3
  > tsup --config ../../../tsup.config.ts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -10,13 +10,13 @@
10
10
  CLI Cleaning output folder
11
11
  ESM Build start
12
12
  CJS Build start
13
- CJS dist/index.js 37.79 KB
14
- CJS dist/index.js.map 77.38 KB
15
- CJS ⚡️ Build success in 129ms
16
- ESM dist/index.mjs 36.64 KB
17
- ESM dist/index.mjs.map 77.34 KB
18
- ESM ⚡️ Build success in 130ms
13
+ ESM dist/index.mjs 47.36 KB
14
+ ESM dist/index.mjs.map 94.51 KB
15
+ ESM ⚡️ Build success in 173ms
16
+ CJS dist/index.js 49.04 KB
17
+ CJS dist/index.js.map 94.56 KB
18
+ CJS ⚡️ Build success in 176ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 15217ms
21
- DTS dist/index.d.mts 8.39 KB
22
- DTS dist/index.d.ts 8.39 KB
20
+ DTS ⚡️ Build success in 15528ms
21
+ DTS dist/index.d.mts 12.48 KB
22
+ DTS dist/index.d.ts 12.48 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @objectstack/driver-memory
2
2
 
3
+ ## 3.0.11
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [92d9d99]
8
+ - @objectstack/spec@3.0.11
9
+ - @objectstack/core@3.0.11
10
+
11
+ ## 3.0.10
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [d1e5d31]
16
+ - @objectstack/spec@3.0.10
17
+ - @objectstack/core@3.0.10
18
+
3
19
  ## 3.0.9
4
20
 
5
21
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -2,6 +2,19 @@ import { QueryInput, DriverOptions, Cube, AnalyticsQuery } from '@objectstack/sp
2
2
  import { DriverInterface, Logger } from '@objectstack/core';
3
3
  import { IAnalyticsService, AnalyticsResult, CubeMeta } from '@objectstack/spec/contracts';
4
4
 
5
+ /**
6
+ * Persistence adapter interface.
7
+ * Matches the PersistenceAdapterSchema contract from @objectstack/spec.
8
+ */
9
+ interface PersistenceAdapterInterface {
10
+ load(): Promise<Record<string, any[]> | null>;
11
+ save(db: Record<string, any[]>): Promise<void>;
12
+ flush(): Promise<void>;
13
+ /** Optional: Start periodic auto-save (used by FileSystemPersistenceAdapter). */
14
+ startAutoSave?(): void;
15
+ /** Optional: Stop auto-save timer and flush pending writes. */
16
+ stopAutoSave?(): Promise<void>;
17
+ }
5
18
  /**
6
19
  * Configuration options for the InMemory driver.
7
20
  * Aligned with @objectstack/spec MemoryConfigSchema.
@@ -13,6 +26,24 @@ interface InMemoryDriverConfig {
13
26
  strictMode?: boolean;
14
27
  /** Optional: Logger instance */
15
28
  logger?: Logger;
29
+ /**
30
+ * Persistence configuration. Defaults to `'auto'`.
31
+ * - `'auto'` (default) — Auto-detect environment (browser → localStorage, Node.js → file)
32
+ * - `'file'` — File-system persistence with defaults (Node.js only)
33
+ * - `'local'` — localStorage persistence with defaults (Browser only)
34
+ * - `{ type: 'file', path?: string, autoSaveInterval?: number }` — File-system with options
35
+ * - `{ type: 'local', key?: string }` — localStorage with options
36
+ * - `{ type: 'auto', path?: string, key?: string, autoSaveInterval?: number }` — Auto-detect with options
37
+ * - `{ adapter: PersistenceAdapterInterface }` — Custom adapter
38
+ * - `false` — Disable persistence (pure in-memory)
39
+ */
40
+ persistence?: string | false | {
41
+ type?: 'file' | 'local' | 'auto';
42
+ path?: string;
43
+ key?: string;
44
+ autoSaveInterval?: number;
45
+ adapter?: PersistenceAdapterInterface;
46
+ };
16
47
  }
17
48
  /**
18
49
  * In-Memory Driver for ObjectStack
@@ -38,6 +69,7 @@ declare class InMemoryDriver implements DriverInterface {
38
69
  private logger;
39
70
  private idCounters;
40
71
  private transactions;
72
+ private persistenceAdapter;
41
73
  constructor(config?: InMemoryDriverConfig);
42
74
  install(ctx: any): void;
43
75
  supports: {
@@ -164,6 +196,99 @@ declare class InMemoryDriver implements DriverInterface {
164
196
  private projectFields;
165
197
  private getTable;
166
198
  private generateId;
199
+ /**
200
+ * Mark the database as dirty, triggering persistence save.
201
+ */
202
+ private markDirty;
203
+ /**
204
+ * Flush pending persistence writes to ensure data is safely stored.
205
+ */
206
+ flush(): Promise<void>;
207
+ /**
208
+ * Detect whether the current runtime is a browser environment.
209
+ */
210
+ private isBrowserEnvironment;
211
+ /**
212
+ * Initialize the persistence adapter based on configuration.
213
+ * Defaults to 'auto' when persistence is not specified.
214
+ * Use `persistence: false` to explicitly disable persistence.
215
+ */
216
+ private initPersistence;
217
+ }
218
+
219
+ /**
220
+ * FileSystemPersistenceAdapter
221
+ *
222
+ * Persists the in-memory database to a JSON file on disk.
223
+ * Supports atomic writes (write to temp file then rename) and auto-save with dirty tracking.
224
+ *
225
+ * Node.js only — will throw if used in non-Node.js environments.
226
+ */
227
+ declare class FileSystemPersistenceAdapter {
228
+ private readonly filePath;
229
+ private readonly autoSaveInterval;
230
+ private dirty;
231
+ private timer;
232
+ private currentDb;
233
+ constructor(options?: {
234
+ path?: string;
235
+ autoSaveInterval?: number;
236
+ });
237
+ /**
238
+ * Load persisted data from disk.
239
+ * Returns null if no file exists.
240
+ */
241
+ load(): Promise<Record<string, any[]> | null>;
242
+ /**
243
+ * Save data to disk using atomic write (temp file + rename).
244
+ */
245
+ save(db: Record<string, any[]>): Promise<void>;
246
+ /**
247
+ * Flush pending writes to disk immediately.
248
+ */
249
+ flush(): Promise<void>;
250
+ /**
251
+ * Start the auto-save timer.
252
+ */
253
+ startAutoSave(): void;
254
+ /**
255
+ * Stop the auto-save timer and flush pending writes.
256
+ */
257
+ stopAutoSave(): Promise<void>;
258
+ /**
259
+ * Atomic write: write to temp file, then rename.
260
+ */
261
+ private writeToDisk;
262
+ }
263
+
264
+ /**
265
+ * LocalStoragePersistenceAdapter
266
+ *
267
+ * Persists the in-memory database to browser localStorage.
268
+ * Synchronous storage with a ~5MB size limit warning.
269
+ *
270
+ * Browser only — will throw if used in non-browser environments.
271
+ */
272
+ declare class LocalStoragePersistenceAdapter {
273
+ private readonly storageKey;
274
+ private static readonly SIZE_WARNING_BYTES;
275
+ constructor(options?: {
276
+ key?: string;
277
+ });
278
+ /**
279
+ * Load persisted data from localStorage.
280
+ * Returns null if no data exists.
281
+ */
282
+ load(): Promise<Record<string, any[]> | null>;
283
+ /**
284
+ * Save data to localStorage.
285
+ * Warns if data size approaches the ~5MB localStorage limit.
286
+ */
287
+ save(db: Record<string, any[]>): Promise<void>;
288
+ /**
289
+ * Flush is a no-op for localStorage (writes are synchronous).
290
+ */
291
+ flush(): Promise<void>;
167
292
  }
168
293
 
169
294
  /**
@@ -237,4 +362,4 @@ declare const _default: {
237
362
  onEnable: (context: any) => Promise<void>;
238
363
  };
239
364
 
240
- export { InMemoryDriver, type InMemoryDriverConfig, type MemoryAnalyticsConfig, MemoryAnalyticsService, _default as default };
365
+ export { FileSystemPersistenceAdapter, InMemoryDriver, type InMemoryDriverConfig, LocalStoragePersistenceAdapter, type MemoryAnalyticsConfig, MemoryAnalyticsService, type PersistenceAdapterInterface, _default as default };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,19 @@ import { QueryInput, DriverOptions, Cube, AnalyticsQuery } from '@objectstack/sp
2
2
  import { DriverInterface, Logger } from '@objectstack/core';
3
3
  import { IAnalyticsService, AnalyticsResult, CubeMeta } from '@objectstack/spec/contracts';
4
4
 
5
+ /**
6
+ * Persistence adapter interface.
7
+ * Matches the PersistenceAdapterSchema contract from @objectstack/spec.
8
+ */
9
+ interface PersistenceAdapterInterface {
10
+ load(): Promise<Record<string, any[]> | null>;
11
+ save(db: Record<string, any[]>): Promise<void>;
12
+ flush(): Promise<void>;
13
+ /** Optional: Start periodic auto-save (used by FileSystemPersistenceAdapter). */
14
+ startAutoSave?(): void;
15
+ /** Optional: Stop auto-save timer and flush pending writes. */
16
+ stopAutoSave?(): Promise<void>;
17
+ }
5
18
  /**
6
19
  * Configuration options for the InMemory driver.
7
20
  * Aligned with @objectstack/spec MemoryConfigSchema.
@@ -13,6 +26,24 @@ interface InMemoryDriverConfig {
13
26
  strictMode?: boolean;
14
27
  /** Optional: Logger instance */
15
28
  logger?: Logger;
29
+ /**
30
+ * Persistence configuration. Defaults to `'auto'`.
31
+ * - `'auto'` (default) — Auto-detect environment (browser → localStorage, Node.js → file)
32
+ * - `'file'` — File-system persistence with defaults (Node.js only)
33
+ * - `'local'` — localStorage persistence with defaults (Browser only)
34
+ * - `{ type: 'file', path?: string, autoSaveInterval?: number }` — File-system with options
35
+ * - `{ type: 'local', key?: string }` — localStorage with options
36
+ * - `{ type: 'auto', path?: string, key?: string, autoSaveInterval?: number }` — Auto-detect with options
37
+ * - `{ adapter: PersistenceAdapterInterface }` — Custom adapter
38
+ * - `false` — Disable persistence (pure in-memory)
39
+ */
40
+ persistence?: string | false | {
41
+ type?: 'file' | 'local' | 'auto';
42
+ path?: string;
43
+ key?: string;
44
+ autoSaveInterval?: number;
45
+ adapter?: PersistenceAdapterInterface;
46
+ };
16
47
  }
17
48
  /**
18
49
  * In-Memory Driver for ObjectStack
@@ -38,6 +69,7 @@ declare class InMemoryDriver implements DriverInterface {
38
69
  private logger;
39
70
  private idCounters;
40
71
  private transactions;
72
+ private persistenceAdapter;
41
73
  constructor(config?: InMemoryDriverConfig);
42
74
  install(ctx: any): void;
43
75
  supports: {
@@ -164,6 +196,99 @@ declare class InMemoryDriver implements DriverInterface {
164
196
  private projectFields;
165
197
  private getTable;
166
198
  private generateId;
199
+ /**
200
+ * Mark the database as dirty, triggering persistence save.
201
+ */
202
+ private markDirty;
203
+ /**
204
+ * Flush pending persistence writes to ensure data is safely stored.
205
+ */
206
+ flush(): Promise<void>;
207
+ /**
208
+ * Detect whether the current runtime is a browser environment.
209
+ */
210
+ private isBrowserEnvironment;
211
+ /**
212
+ * Initialize the persistence adapter based on configuration.
213
+ * Defaults to 'auto' when persistence is not specified.
214
+ * Use `persistence: false` to explicitly disable persistence.
215
+ */
216
+ private initPersistence;
217
+ }
218
+
219
+ /**
220
+ * FileSystemPersistenceAdapter
221
+ *
222
+ * Persists the in-memory database to a JSON file on disk.
223
+ * Supports atomic writes (write to temp file then rename) and auto-save with dirty tracking.
224
+ *
225
+ * Node.js only — will throw if used in non-Node.js environments.
226
+ */
227
+ declare class FileSystemPersistenceAdapter {
228
+ private readonly filePath;
229
+ private readonly autoSaveInterval;
230
+ private dirty;
231
+ private timer;
232
+ private currentDb;
233
+ constructor(options?: {
234
+ path?: string;
235
+ autoSaveInterval?: number;
236
+ });
237
+ /**
238
+ * Load persisted data from disk.
239
+ * Returns null if no file exists.
240
+ */
241
+ load(): Promise<Record<string, any[]> | null>;
242
+ /**
243
+ * Save data to disk using atomic write (temp file + rename).
244
+ */
245
+ save(db: Record<string, any[]>): Promise<void>;
246
+ /**
247
+ * Flush pending writes to disk immediately.
248
+ */
249
+ flush(): Promise<void>;
250
+ /**
251
+ * Start the auto-save timer.
252
+ */
253
+ startAutoSave(): void;
254
+ /**
255
+ * Stop the auto-save timer and flush pending writes.
256
+ */
257
+ stopAutoSave(): Promise<void>;
258
+ /**
259
+ * Atomic write: write to temp file, then rename.
260
+ */
261
+ private writeToDisk;
262
+ }
263
+
264
+ /**
265
+ * LocalStoragePersistenceAdapter
266
+ *
267
+ * Persists the in-memory database to browser localStorage.
268
+ * Synchronous storage with a ~5MB size limit warning.
269
+ *
270
+ * Browser only — will throw if used in non-browser environments.
271
+ */
272
+ declare class LocalStoragePersistenceAdapter {
273
+ private readonly storageKey;
274
+ private static readonly SIZE_WARNING_BYTES;
275
+ constructor(options?: {
276
+ key?: string;
277
+ });
278
+ /**
279
+ * Load persisted data from localStorage.
280
+ * Returns null if no data exists.
281
+ */
282
+ load(): Promise<Record<string, any[]> | null>;
283
+ /**
284
+ * Save data to localStorage.
285
+ * Warns if data size approaches the ~5MB localStorage limit.
286
+ */
287
+ save(db: Record<string, any[]>): Promise<void>;
288
+ /**
289
+ * Flush is a no-op for localStorage (writes are synchronous).
290
+ */
291
+ flush(): Promise<void>;
167
292
  }
168
293
 
169
294
  /**
@@ -237,4 +362,4 @@ declare const _default: {
237
362
  onEnable: (context: any) => Promise<void>;
238
363
  };
239
364
 
240
- export { InMemoryDriver, type InMemoryDriverConfig, type MemoryAnalyticsConfig, MemoryAnalyticsService, _default as default };
365
+ export { FileSystemPersistenceAdapter, InMemoryDriver, type InMemoryDriverConfig, LocalStoragePersistenceAdapter, type MemoryAnalyticsConfig, MemoryAnalyticsService, type PersistenceAdapterInterface, _default as default };