deepbase-sqlite 3.6.6 → 3.6.8

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-sqlite",
3
- "version": "3.6.6",
3
+ "version": "3.6.8",
4
4
  "description": "⚡ DeepBase SQLite - SQLite database driver",
5
5
  "type": "module",
6
6
  "main": "src/index.cjs",
@@ -17,7 +17,7 @@
17
17
  "better-sqlite3": "^11.8.1"
18
18
  },
19
19
  "peerDependencies": {
20
- "deepbase": "^3.6.6"
20
+ "deepbase": "^3.6.8"
21
21
  },
22
22
  "scripts": {
23
23
  "test": "mocha test/test.js"
@@ -49,6 +49,7 @@ export class SqliteDriver extends DeepBaseDriver {
49
49
  }
50
50
 
51
51
  this.db = null;
52
+ this._nextSeq = 1;
52
53
  SqliteDriver._instances[this.fileName] = this;
53
54
  }
54
55
 
@@ -102,9 +103,10 @@ export class SqliteDriver extends DeepBaseDriver {
102
103
  }
103
104
 
104
105
  this.getStmt = this.db.prepare('SELECT value FROM deepbase WHERE key = ?');
106
+ this.getMaxSeqStmt = this.db.prepare('SELECT IFNULL(MAX(seq), 0) AS maxSeq FROM deepbase');
105
107
  this.setStmt = this.db.prepare(`
106
108
  INSERT INTO deepbase (key, value, seq)
107
- VALUES (?, ?, (SELECT IFNULL(MAX(seq), 0) + 1 FROM deepbase))
109
+ VALUES (?, ?, ?)
108
110
  ON CONFLICT(key) DO UPDATE SET value = excluded.value
109
111
  `);
110
112
  this.delStmt = this.db.prepare('DELETE FROM deepbase WHERE key = ?');
@@ -123,10 +125,11 @@ export class SqliteDriver extends DeepBaseDriver {
123
125
 
124
126
  this._setTxn = this.db.transaction((key, jsonValue, keys) => {
125
127
  this._expandParentObjects(keys);
126
- this.setStmt.run(key, jsonValue);
128
+ this.setStmt.run(key, jsonValue, this._consumeSeq());
127
129
  });
128
130
 
129
- this._delTxn = this.db.transaction((key, likePattern) => {
131
+ this._delTxn = this.db.transaction((key, likePattern, keys) => {
132
+ this._expandParentObjects(keys);
130
133
  this.delStmt.run(key);
131
134
  this.delChildrenStmt.run(likePattern);
132
135
  });
@@ -136,17 +139,19 @@ export class SqliteDriver extends DeepBaseDriver {
136
139
  const newValue = func(currentValue);
137
140
  const key = this._pathToKey(keys);
138
141
  this._expandParentObjects(keys);
139
- this.setStmt.run(key, JSON.stringify(newValue));
142
+ this.setStmt.run(key, JSON.stringify(newValue), this._consumeSeq());
140
143
  return keys;
141
144
  });
142
145
 
143
146
  this._setRootTxn = this.db.transaction((entries) => {
144
147
  this.db.exec('DELETE FROM deepbase');
148
+ this._nextSeq = 1;
145
149
  for (const [key, value] of entries) {
146
- this.setStmt.run(key, JSON.stringify(value));
150
+ this.setStmt.run(key, JSON.stringify(value), this._consumeSeq());
147
151
  }
148
152
  });
149
153
 
154
+ this._nextSeq = Number(this.getMaxSeqStmt.get()?.maxSeq || 0) + 1;
150
155
  this._connected = true;
151
156
  }
152
157
 
@@ -218,11 +223,12 @@ export class SqliteDriver extends DeepBaseDriver {
218
223
  async del(...keys) {
219
224
  if (keys.length === 0) {
220
225
  this.db.exec('DELETE FROM deepbase');
226
+ this._nextSeq = 1;
221
227
  return;
222
228
  }
223
229
 
224
230
  const key = this._pathToKey(keys);
225
- this._delTxn(key, this._likePrefix(key));
231
+ this._delTxn(key, this._likePrefix(key), keys);
226
232
  }
227
233
 
228
234
  async inc(...args) {
@@ -411,12 +417,18 @@ export class SqliteDriver extends DeepBaseDriver {
411
417
 
412
418
  const entries = this._flattenObject(parentValue, parentKey);
413
419
  for (const [key, value] of entries) {
414
- this.setStmt.run(key, JSON.stringify(value));
420
+ this.setStmt.run(key, JSON.stringify(value), this._consumeSeq());
415
421
  }
416
422
  }
417
423
  }
418
424
  }
419
425
  }
426
+
427
+ _consumeSeq() {
428
+ const seq = this._nextSeq;
429
+ this._nextSeq += 1;
430
+ return seq;
431
+ }
420
432
  }
421
433
 
422
434
  export default SqliteDriver;
package/test/test.js CHANGED
@@ -159,6 +159,15 @@ for (const pragma of PRAGMA_MODES) {
159
159
  assert.deepStrictEqual(await db.get('user'), { name: 'Alice' });
160
160
  });
161
161
 
162
+ it('should delete a deep key from an object value', async function () {
163
+ await db.set('a', {
164
+ b: { c: 1, d: 2 },
165
+ e: 3,
166
+ });
167
+ await db.del('a', 'b', 'c');
168
+ assert.deepStrictEqual(await db.get('a'), { b: { d: 2 }, e: 3 });
169
+ });
170
+
162
171
  it('should delete parent and all children', async function () {
163
172
  await db.set('parent', 'child1', 'value1');
164
173
  await db.set('parent', 'child2', 'value2');