deepbase-redis 3.10.0 → 3.11.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
@@ -118,6 +118,22 @@ const allUsers = await db.get('users');
118
118
  // Scans all keys matching prefix
119
119
  ```
120
120
 
121
+ ### Querying
122
+
123
+ `db.query(...path)` is evaluated in memory: the driver reads the whole
124
+ document already stored in Redis and walks it locally. v1 does not push
125
+ filters to the server and does not use indexes.
126
+
127
+ Every result has the `{ id, value }` shape, where `id` is the property name
128
+ inside the queried object:
129
+
130
+ ```javascript
131
+ await db.set('users', 'alice', { name: 'Alice', age: 30 });
132
+
133
+ const records = await db.query('users').where('age', '>=', 18).toArray();
134
+ // [{ id: 'alice', value: { name: 'Alice', age: 30 } }]
135
+ ```
136
+
121
137
  ## Connection String Formats
122
138
 
123
139
  ```javascript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deepbase-redis",
3
- "version": "3.10.0",
3
+ "version": "3.11.0",
4
4
  "description": "⚡ DeepBase Redis (vanilla) - driver",
5
5
  "type": "module",
6
6
  "main": "src/index.cjs",
@@ -17,10 +17,7 @@
17
17
  "redis": "^4.7.0"
18
18
  },
19
19
  "peerDependencies": {
20
- "deepbase": "^3.10.0"
21
- },
22
- "scripts": {
23
- "test": "mocha test/test.js"
20
+ "deepbase": "^3.11.0"
24
21
  },
25
22
  "devDependencies": {
26
23
  "mocha": "^10.8.2"
@@ -44,5 +41,8 @@
44
41
  "bugs": {
45
42
  "url": "https://github.com/clasen/DeepBase/issues"
46
43
  },
47
- "homepage": "https://github.com/clasen/DeepBase/tree/main/packages/driver-redis"
48
- }
44
+ "homepage": "https://github.com/clasen/DeepBase/tree/main/packages/driver-redis",
45
+ "scripts": {
46
+ "test": "mocha test/test.js"
47
+ }
48
+ }
@@ -1,4 +1,4 @@
1
- import { DeepBaseDriver } from 'deepbase';
1
+ import { DeepBaseDriver, evaluateQuery, removeKey } from 'deepbase';
2
2
  import { createClient } from 'redis';
3
3
 
4
4
  export class RedisDriver extends DeepBaseDriver {
@@ -50,6 +50,18 @@ export class RedisDriver extends DeepBaseDriver {
50
50
  return null;
51
51
  }
52
52
  }
53
+
54
+ /**
55
+ * Run a query descriptor over the object stored at ...path.
56
+ * The value is read from Redis and evaluated in memory; filters are not
57
+ * pushed to the server and no indexes are used.
58
+ * @param {Array<string|number>} path - Path to the queried object
59
+ * @param {object[]} steps - Query descriptor steps
60
+ * @returns {Promise<Array<{id: string, value: any}>>} Matching records
61
+ */
62
+ async query(path, steps) {
63
+ return evaluateQuery(await this.get(...path), steps, { path });
64
+ }
53
65
 
54
66
  async _acquireLock(key) {
55
67
  // Wait for any pending operation on this key to complete
@@ -176,9 +188,11 @@ export class RedisDriver extends DeepBaseDriver {
176
188
  }
177
189
 
178
190
  const lastKey = args[args.length - 1];
179
- delete current[lastKey];
180
-
181
- await this.client.set(redisKey, JSON.stringify(data));
191
+ // Array indices are spliced so the array shrinks instead of keeping a
192
+ // hole that JSON.stringify renders as null.
193
+ if (removeKey(current, lastKey)) {
194
+ await this.client.set(redisKey, JSON.stringify(data));
195
+ }
182
196
  }
183
197
 
184
198
  return [key, ...args];
package/test/test.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import assert from 'assert';
2
2
  import { DeepBase } from '../../core/src/index.js';
3
+ import { arrayScenarios } from '../../core/test/array-scenario.js';
4
+ import { queryScenarios, seedQueryFixture } from '../../core/test/query-scenario.js';
3
5
  import { RedisDriver } from '../src/RedisDriver.js';
4
6
 
5
7
  describe('RedisDriver', function() {
@@ -434,5 +436,24 @@ describe('RedisDriver', function() {
434
436
  assert.strictEqual(keys.length, operations);
435
437
  });
436
438
  });
437
- });
438
439
 
440
+ describe('query()', function() {
441
+ beforeEach(async function() {
442
+ await seedQueryFixture(db);
443
+ });
444
+
445
+ for (const scenario of queryScenarios) {
446
+ it(scenario.title, async function() {
447
+ await scenario.run(db);
448
+ });
449
+ }
450
+ });
451
+
452
+ describe('Array Operations', function() {
453
+ for (const scenario of arrayScenarios) {
454
+ it(scenario.title, async function() {
455
+ await scenario.run(db);
456
+ });
457
+ }
458
+ });
459
+ });