deepbase-json 3.6.5 → 3.6.7

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
@@ -233,6 +233,8 @@ new DeepBase(drivers, options)
233
233
  - `await db.add(...path, value)` - Add item with auto-generated ID
234
234
  - `await db.upd(...path, fn)` - Update value with function
235
235
  - `await db.keys(...path)` - Get keys at path
236
+ - `await db.first(...path)` - Get first key at path (same order as `keys()`)
237
+ - `await db.last(...path)` - Get last key at path (same order as `keys()`)
236
238
  - `await db.values(...path)` - Get values at path
237
239
  - `await db.entries(...path)` - Get entries at path
238
240
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deepbase-json",
3
- "version": "3.6.5",
3
+ "version": "3.6.7",
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.6.5"
21
+ "deepbase": "^3.6.7"
22
22
  },
23
23
  "scripts": {
24
24
  "test": "mocha test/test.js"
package/src/JsonDriver.js CHANGED
@@ -148,6 +148,45 @@ export class JsonDriver extends DeepBaseDriver {
148
148
  return args;
149
149
  });
150
150
  }
151
+
152
+ async first(...args) {
153
+ if (this.multiProcess) {
154
+ this._refreshFromDisk();
155
+ }
156
+
157
+ const value = this._getRecursive(this.obj, args.slice());
158
+ if (value === null || typeof value !== 'object') {
159
+ return undefined;
160
+ }
161
+
162
+ for (const key in value) {
163
+ if (Object.prototype.hasOwnProperty.call(value, key)) {
164
+ return key;
165
+ }
166
+ }
167
+
168
+ return undefined;
169
+ }
170
+
171
+ async last(...args) {
172
+ if (this.multiProcess) {
173
+ this._refreshFromDisk();
174
+ }
175
+
176
+ const value = this._getRecursive(this.obj, args.slice());
177
+ if (value === null || typeof value !== 'object') {
178
+ return undefined;
179
+ }
180
+
181
+ let last;
182
+ for (const key in value) {
183
+ if (Object.prototype.hasOwnProperty.call(value, key)) {
184
+ last = key;
185
+ }
186
+ }
187
+
188
+ return last;
189
+ }
151
190
 
152
191
  // Internal set without queuing (for use within queued operations)
153
192
  async _setInternal(...args) {
package/test/test.js CHANGED
@@ -149,6 +149,16 @@ describe('JsonDriver', function() {
149
149
  assert.deepStrictEqual(user, { name: 'Alice' });
150
150
  });
151
151
 
152
+ it('should delete a deep key from an object value', async function() {
153
+ await db.set('a', {
154
+ b: { c: 1, d: 2 },
155
+ e: 3
156
+ });
157
+ await db.del('a', 'b', 'c');
158
+
159
+ assert.deepStrictEqual(await db.get('a'), { b: { d: 2 }, e: 3 });
160
+ });
161
+
152
162
  it('should clear all data', async function() {
153
163
  await db.set('key1', 'value1');
154
164
  await db.set('key2', 'value2');
@@ -248,6 +258,19 @@ describe('JsonDriver', function() {
248
258
  assert.ok(entries.some(([k, v]) => k === 'alice' && v.age === 30));
249
259
  assert.ok(entries.some(([k, v]) => k === 'bob' && v.age === 25));
250
260
  });
261
+
262
+ it('first/last should match keys() boundaries', async function() {
263
+ const keys = await db.keys('users');
264
+ const driver = db.getDriver(0);
265
+ assert.strictEqual(await driver.first('users'), keys[0]);
266
+ assert.strictEqual(await driver.last('users'), keys[keys.length - 1]);
267
+ });
268
+
269
+ it('first/last should return undefined for missing path', async function() {
270
+ const driver = db.getDriver(0);
271
+ assert.strictEqual(await driver.first('missing'), undefined);
272
+ assert.strictEqual(await driver.last('missing'), undefined);
273
+ });
251
274
  });
252
275
 
253
276
  describe('Persistence', function() {