deepbase-sqlite 3.6.7 → 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.7",
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.7"
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,7 +125,7 @@ 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
131
  this._delTxn = this.db.transaction((key, likePattern, keys) => {
@@ -137,17 +139,19 @@ export class SqliteDriver extends DeepBaseDriver {
137
139
  const newValue = func(currentValue);
138
140
  const key = this._pathToKey(keys);
139
141
  this._expandParentObjects(keys);
140
- this.setStmt.run(key, JSON.stringify(newValue));
142
+ this.setStmt.run(key, JSON.stringify(newValue), this._consumeSeq());
141
143
  return keys;
142
144
  });
143
145
 
144
146
  this._setRootTxn = this.db.transaction((entries) => {
145
147
  this.db.exec('DELETE FROM deepbase');
148
+ this._nextSeq = 1;
146
149
  for (const [key, value] of entries) {
147
- this.setStmt.run(key, JSON.stringify(value));
150
+ this.setStmt.run(key, JSON.stringify(value), this._consumeSeq());
148
151
  }
149
152
  });
150
153
 
154
+ this._nextSeq = Number(this.getMaxSeqStmt.get()?.maxSeq || 0) + 1;
151
155
  this._connected = true;
152
156
  }
153
157
 
@@ -219,6 +223,7 @@ export class SqliteDriver extends DeepBaseDriver {
219
223
  async del(...keys) {
220
224
  if (keys.length === 0) {
221
225
  this.db.exec('DELETE FROM deepbase');
226
+ this._nextSeq = 1;
222
227
  return;
223
228
  }
224
229
 
@@ -412,12 +417,18 @@ export class SqliteDriver extends DeepBaseDriver {
412
417
 
413
418
  const entries = this._flattenObject(parentValue, parentKey);
414
419
  for (const [key, value] of entries) {
415
- this.setStmt.run(key, JSON.stringify(value));
420
+ this.setStmt.run(key, JSON.stringify(value), this._consumeSeq());
416
421
  }
417
422
  }
418
423
  }
419
424
  }
420
425
  }
426
+
427
+ _consumeSeq() {
428
+ const seq = this._nextSeq;
429
+ this._nextSeq += 1;
430
+ return seq;
431
+ }
421
432
  }
422
433
 
423
434
  export default SqliteDriver;