deepbase-json 3.4.12 → 3.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deepbase-json",
3
- "version": "3.4.12",
3
+ "version": "3.6.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.4.12"
21
+ "deepbase": "^3.6.0"
22
22
  },
23
23
  "scripts": {
24
24
  "test": "mocha test/test.js"
package/src/JsonDriver.js CHANGED
@@ -35,13 +35,13 @@ export class JsonDriver extends DeepBaseDriver {
35
35
  JsonDriver._instances[this.fileName] = this;
36
36
  }
37
37
 
38
- async connect() {
38
+ _connectSync() {
39
39
  if (this._connected) return;
40
-
40
+
41
41
  if (!fs.existsSync(this.path)) {
42
42
  fs.mkdirSync(this.path, { recursive: true });
43
43
  }
44
-
44
+
45
45
  if (fs.existsSync(this.fileName)) {
46
46
  const fileContent = fs.readFileSync(this.fileName, "utf8");
47
47
  this.obj = fileContent ? this.parse(fileContent) : {};
@@ -49,9 +49,13 @@ export class JsonDriver extends DeepBaseDriver {
49
49
  // Create the file so proper-lockfile can lock it in multiProcess mode
50
50
  fs.writeFileSync(this.fileName, this.stringify(this.obj));
51
51
  }
52
-
52
+
53
53
  this._connected = true;
54
54
  }
55
+
56
+ async connect() {
57
+ this._connectSync();
58
+ }
55
59
 
56
60
  async disconnect() {
57
61
  await this._saveToFile();
@@ -68,6 +72,17 @@ export class JsonDriver extends DeepBaseDriver {
68
72
  ? this.parse(this.stringify(value))
69
73
  : value;
70
74
  }
75
+
76
+ getSync(...args) {
77
+ if (this.multiProcess) {
78
+ throw new Error('JsonDriver: getSync() is not supported in multiProcess mode.');
79
+ }
80
+ this._connectSync();
81
+ const value = this._getRecursive(this.obj, args.slice());
82
+ return typeof value === 'object' && value !== null
83
+ ? this.parse(this.stringify(value))
84
+ : value;
85
+ }
71
86
 
72
87
  async set(...args) {
73
88
  return this._queueOperation(async () => {
package/test/test.js CHANGED
@@ -68,6 +68,21 @@ describe('JsonDriver', function() {
68
68
  const result = await db.get('nonexistent');
69
69
  assert.strictEqual(result, null);
70
70
  });
71
+
72
+ it('getSync returns same value as get (when not multiProcess)', async function() {
73
+ await db.set('syncKey', 'syncValue');
74
+ await db.set('syncNested', 'x', 42);
75
+ const driver = db.getDriver(0);
76
+ assert.strictEqual(driver.getSync('syncKey'), 'syncValue');
77
+ assert.strictEqual(driver.getSync('syncNested', 'x'), 42);
78
+ assert.strictEqual(driver.getSync('nonexistent'), null);
79
+ });
80
+
81
+ it('getSync lazy-connects when not connected', function() {
82
+ const driver = new JsonDriver({ name: 'getSync-lazy', path: testDataPath });
83
+ assert.strictEqual(driver.getSync('key'), null);
84
+ assert.ok(driver._connected);
85
+ });
71
86
  });
72
87
 
73
88
  describe('Keys with Dots', function() {
@@ -516,6 +531,12 @@ describe('JsonDriver', function() {
516
531
  }
517
532
  });
518
533
 
534
+ it('getSync throws in multiProcess mode', async function() {
535
+ const driver = new JsonDriver({ name: 'mp-getSync', path: multiProcessPath, multiProcess: true });
536
+ await driver.connect();
537
+ assert.throws(() => driver.getSync('key'), /getSync\(\) is not supported in multiProcess mode/);
538
+ });
539
+
519
540
  it('should handle concurrent increments from multiple processes', async function() {
520
541
  this.timeout(30000);
521
542