deepbase-json 3.8.6 → 3.9.0

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.
package/README.md CHANGED
@@ -31,40 +31,47 @@ DeepBase v3.0 is split into modular packages:
31
31
  ### Simple JSON Driver
32
32
 
33
33
  ```bash
34
- npm install deepbase
35
- # deepbase automatically includes deepbase-json
34
+ npm install deepbase deepbase-json
36
35
  ```
37
36
 
38
37
  ```javascript
39
38
  import DeepBase from 'deepbase';
39
+ import { resolvePath } from 'deepbase/path';
40
+
41
+ const dataPath = resolvePath(import.meta.url, './data');
40
42
 
41
43
  // Option 1: Backward-compatible syntax (uses JSON driver by default)
42
- const db = new DeepBase({ path: './data', name: 'mydb' });
44
+ const db = new DeepBase({ path: dataPath, name: 'mydb' });
43
45
  await db.connect();
44
46
 
45
47
  // Option 2: Explicit JSON driver
46
- import { JsonDriver } from 'deepbase';
47
- const db = new DeepBase(new JsonDriver({ path: './data', name: 'mydb' }));
48
- await db.connect();
48
+ import { JsonDriver } from 'deepbase-json';
49
+ const explicitDb = new DeepBase(new JsonDriver({ path: dataPath, name: 'mydb' }));
50
+ await explicitDb.connect();
49
51
 
50
52
  await db.set('users', 'alice', { name: 'Alice', age: 30 });
51
53
  const alice = await db.get('users', 'alice');
52
54
  console.log(alice); // { name: 'Alice', age: 30 }
53
55
  ```
54
56
 
57
+ `path` is required and must be absolute. `resolvePath()` anchors a relative
58
+ path to your application module instead of `process.cwd()` or the installed
59
+ package location.
60
+
55
61
  ### Multi-Driver Setup (MongoDB + JSON Backup)
56
62
 
57
63
  ```bash
58
- npm install deepbase deepbase-mongodb
64
+ npm install deepbase deepbase-json deepbase-mongodb
59
65
  ```
60
66
 
61
67
  ```javascript
62
- import DeepBase, { JsonDriver } from 'deepbase';
68
+ import DeepBase from 'deepbase';
69
+ import { JsonDriver } from 'deepbase-json';
63
70
  import MongoDriver from 'deepbase-mongodb';
64
71
 
65
72
  const db = new DeepBase([
66
73
  new MongoDriver({ url: 'mongodb://localhost:27017' }),
67
- new JsonDriver({ path: './backup' })
74
+ new JsonDriver({ path: '/var/lib/myapp/backup' })
68
75
  ], {
69
76
  writeAll: true, // Write to all drivers
70
77
  readFirst: true, // Read from first available
@@ -140,7 +147,7 @@ import MongoDriver from '@deepbase/mongodb';
140
147
 
141
148
  // Setup with both drivers
142
149
  const db = new DeepBase([
143
- new JsonDriver({ path: './data', name: 'mydb' }), // Source (index 0)
150
+ new JsonDriver({ path: '/var/lib/myapp/data', name: 'mydb' }), // Source (index 0)
144
151
  new MongoDriver({ url: 'mongodb://localhost:27017' }) // Target (index 1)
145
152
  ]);
146
153
 
@@ -177,7 +184,7 @@ import RedisDriver from '@deepbase/redis';
177
184
 
178
185
  const db = new DeepBase([
179
186
  new MongoDriver({ url: 'mongodb://localhost:27017' }), // Primary
180
- new JsonDriver({ path: './persistence' }), // Backup
187
+ new JsonDriver({ path: '/var/lib/myapp/persistence' }), // Backup
181
188
  new RedisDriver({ url: 'redis://localhost:6379' }) // Cache
182
189
  ], {
183
190
  writeAll: true, // Replicate writes to all three
@@ -281,17 +288,18 @@ See [`examples/08-concurrency-safe.js`](./examples/08-concurrency-safe.js) for d
281
288
  Prevent operations from hanging indefinitely with configurable timeouts:
282
289
 
283
290
  ```javascript
284
- import DeepBase, { JsonDriver } from 'deepbase';
291
+ import DeepBase from 'deepbase';
292
+ import { JsonDriver } from 'deepbase-json';
285
293
 
286
294
  // Global timeout for all operations
287
- const db = new DeepBase(new JsonDriver(), {
295
+ const db = new DeepBase(new JsonDriver({ path: '/var/lib/myapp/data' }), {
288
296
  timeout: 5000 // 5 seconds for all operations
289
297
  });
290
298
 
291
299
  // Different timeouts for reads and writes
292
300
  const db2 = new DeepBase([
293
301
  new RedisDriver({ url: 'redis://slow-server:6379' }),
294
- new JsonDriver({ path: './backup' }) // Fallback if Redis times out
302
+ new JsonDriver({ path: '/var/lib/myapp/backup' }) // Fallback if Redis times out
295
303
  ], {
296
304
  readTimeout: 2000, // 2 seconds for reads (get, keys, values, entries)
297
305
  writeTimeout: 5000, // 5 seconds for writes (set, del, inc, dec, add, upd)
@@ -332,7 +340,7 @@ Filesystem-based JSON storage. Perfect for:
332
340
 
333
341
  ```javascript
334
342
  new JsonDriver({
335
- path: './data', // Storage directory
343
+ path: '/var/lib/myapp/data', // Required absolute storage directory
336
344
  name: 'mydb', // Filename (mydb.json)
337
345
  stringify: JSON.stringify, // Custom serializer
338
346
  parse: JSON.parse // Custom parser
@@ -351,7 +359,7 @@ SQLite embedded database. Perfect for:
351
359
 
352
360
  ```javascript
353
361
  new SqliteDriver({
354
- path: './data', // Storage directory
362
+ path: '/var/lib/myapp/data', // Required absolute storage directory
355
363
  name: 'mydb' // Database filename (mydb.db)
356
364
  })
357
365
  ```
@@ -436,10 +444,11 @@ DeepBase supports custom JSON serialization in the JSON driver, allowing for cir
436
444
 
437
445
  ```javascript
438
446
  import { parse, stringify } from 'flatted';
439
- import DeepBase, { JsonDriver } from 'deepbase';
447
+ import DeepBase from 'deepbase';
448
+ import { JsonDriver } from 'deepbase-json';
440
449
 
441
450
  const db = new DeepBase(new JsonDriver({
442
- path: './data',
451
+ path: '/var/lib/myapp/data',
443
452
  name: 'mydb',
444
453
  stringify,
445
454
  parse
@@ -457,10 +466,11 @@ await db.set('circular', obj);
457
466
 
458
467
  ```javascript
459
468
  const CircularJSON = require('circular-json');
460
- import DeepBase, { JsonDriver } from 'deepbase';
469
+ import DeepBase from 'deepbase';
470
+ import { JsonDriver } from 'deepbase-json';
461
471
 
462
472
  const db = new DeepBase(new JsonDriver({
463
- path: './data',
473
+ path: '/var/lib/myapp/data',
464
474
  name: 'mydb',
465
475
  stringify: (obj) => CircularJSON.stringify(obj, null, 4),
466
476
  parse: CircularJSON.parse
@@ -478,7 +488,8 @@ You can create encrypted storage by extending DeepBase with custom serialization
478
488
 
479
489
  ```javascript
480
490
  import CryptoJS from 'crypto-js';
481
- import DeepBase, { JsonDriver } from 'deepbase';
491
+ import DeepBase from 'deepbase';
492
+ import { JsonDriver } from 'deepbase-json';
482
493
 
483
494
  class DeepbaseSecure extends DeepBase {
484
495
  constructor(opts) {
@@ -511,7 +522,7 @@ class DeepbaseSecure extends DeepBase {
511
522
 
512
523
  // Create an encrypted database
513
524
  const secureDB = new DeepbaseSecure({
514
- path: './data',
525
+ path: '/var/lib/myapp/data',
515
526
  name: 'secure_db',
516
527
  encryptionKey: 'your-secret-key-here'
517
528
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deepbase-json",
3
- "version": "3.8.6",
3
+ "version": "3.9.0",
4
4
  "description": "⚡ DeepBase JSON - filesystem driver",
5
5
  "type": "module",
6
6
  "main": "src/index.cjs",
@@ -18,7 +18,7 @@
18
18
  "steno": "^0.4.4"
19
19
  },
20
20
  "peerDependencies": {
21
- "deepbase": "^3.8.6"
21
+ "deepbase": "^3.9.0"
22
22
  },
23
23
  "devDependencies": {
24
24
  "mocha": "^10.8.2"
package/src/JsonDriver.js CHANGED
@@ -18,9 +18,13 @@ export class JsonDriver extends DeepBaseDriver {
18
18
  ...opts
19
19
  } = {}) {
20
20
  super(opts);
21
+
22
+ if (typeof path !== 'string' || path.trim() === '' || !pathModule.isAbsolute(path)) {
23
+ throw new TypeError('JsonDriver requires an absolute "path" option.');
24
+ }
21
25
 
22
26
  this.name = name || "default";
23
- this.path = path || pathModule.join(process.cwd(), 'db');
27
+ this.path = path;
24
28
  this.stringify = stringify || ((obj) => JSON.stringify(obj, null, 4));
25
29
  this.parse = parse || JSON.parse;
26
30
  this.multiProcess = multiProcess || false;
@@ -375,4 +379,3 @@ export class JsonDriver extends DeepBaseDriver {
375
379
  }
376
380
 
377
381
  export default JsonDriver;
378
-
package/src/index.d.ts CHANGED
@@ -4,7 +4,8 @@ export type MemoryTransform = (value: unknown, path: string[]) => unknown;
4
4
 
5
5
  export interface JsonDriverOptions extends DeepBaseDriverOptions {
6
6
  name?: string;
7
- path?: string;
7
+ /** Required absolute storage directory. */
8
+ path: string;
8
9
  stringify?: (obj: any) => string;
9
10
  parse?: (str: string) => any;
10
11
  /** Enable cross-process file locking for safe multi-process access */
@@ -16,7 +17,7 @@ export interface JsonDriverOptions extends DeepBaseDriverOptions {
16
17
  }
17
18
 
18
19
  export class JsonDriver extends DeepBaseDriver {
19
- constructor(options?: JsonDriverOptions);
20
+ constructor(options: JsonDriverOptions);
20
21
 
21
22
  name: string;
22
23
  path: string;
package/test/test.js CHANGED
@@ -38,6 +38,19 @@ describe('JsonDriver', function() {
38
38
  }
39
39
  });
40
40
 
41
+ describe('Configuration', function() {
42
+ it('should require an absolute storage path', function() {
43
+ assert.throws(
44
+ () => new JsonDriver({ name: 'missing-path' }),
45
+ /requires an absolute "path" option/,
46
+ );
47
+ assert.throws(
48
+ () => new JsonDriver({ name: 'relative-path', path: './data' }),
49
+ /requires an absolute "path" option/,
50
+ );
51
+ });
52
+ });
53
+
41
54
  describe('Basic Operations', function() {
42
55
  it('should set and get a simple value', async function() {
43
56
  await db.set('key', 'value');
@@ -1066,4 +1079,3 @@ describe('Multi-Driver: JsonDriver + SqliteDriver (add + pop + shift)', function
1066
1079
  assert.deepStrictEqual(Object.values(sqliteTasks)[0], { name: 'test', prio: 2 });
1067
1080
  });
1068
1081
  });
1069
-