jsql-neo 6.7.2 → 6.7.5
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 +24 -2
- package/index.js +1 -1
- package/lib/database.js +154 -46
- package/lib/sql.js +195 -40
- package/lib/table.js +8 -0
- package/package.json +1 -1
- package/postinstall.js +16 -15
- package/test/bug-regression-2.test.js +2 -1
- package/test/extra-coverage.test.js +3 -2
- package/test/issue-111.test.js +112 -0
- package/test/issue-114.test.js +102 -0
- package/test/mysql-utility-functions.test.js +48 -0
- package/test/network-policy.test.js +19 -0
- package/test/wal-recovery.test.js +22 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
|
|
4
4
|
> MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
|
|
5
5
|
|
|
6
|
-
> **v6.7.
|
|
6
|
+
> **v6.7.5** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|

|
|
@@ -169,6 +169,17 @@ await db.start();
|
|
|
169
169
|
| Disk | `new Database('./data/app')` | Production persistence |
|
|
170
170
|
| Hybrid | `new Database('./data', { autoSave: true, saveInterval: 3000 })` | Performance + durability |
|
|
171
171
|
|
|
172
|
+
File mode (`new Database('./data/db.json')`) is **not** an in-memory store that only
|
|
173
|
+
writes on exit. Every `insert` / `insertMany` / `update` / `updateById` / `remove` /
|
|
174
|
+
`removeById` / `softDelete` / `truncate` appends one JSONL record to `<file>.wal` and
|
|
175
|
+
`fsync`s it before returning, so a `kill -9` loses nothing that already committed.
|
|
176
|
+
`save()` / `close()` / `stop()` only checkpoint (write a full snapshot, then truncate
|
|
177
|
+
the WAL); they are no longer the moment data first reaches disk.
|
|
178
|
+
WAL is on by default for file mode — pass `wal: false` for the old semantics, or tune
|
|
179
|
+
auto-checkpointing with `walCheckpointOps` (default 1000, `0` disables). Directory
|
|
180
|
+
(`mode: 'disk' | 'hybrid'`) and encrypted (`encryptKey`) databases keep it off unless
|
|
181
|
+
you opt in with `wal: true`, because the WAL is plaintext.
|
|
182
|
+
|
|
172
183
|
---
|
|
173
184
|
|
|
174
185
|
## Query results
|
|
@@ -266,7 +277,18 @@ source stating why it cannot be done locally:
|
|
|
266
277
|
| `jsql star --token-check` | `api.github.com` | star status only exists on the GitHub account | `JSQL_OFFLINE=1` |
|
|
267
278
|
| `jsql update` | `registry.npmjs.org` | "is there a newer version" only exists in the registry | `JSQL_OFFLINE=1` |
|
|
268
279
|
| `jsql plugin list\|search\|info\|install` | `registry-beta.vexify.top` | the plugin catalogue only exists remotely | `JSQL_OFFLINE=1` |
|
|
269
|
-
| `npm install` (postinstall) | GitHub releases | the precompiled Rust `.node` is not in the npm tarball | `JSQL_NO_NATIVE_DOWNLOAD=1` or `JSQL_OFFLINE=1` |
|
|
280
|
+
| `npm install` (postinstall) | GitHub releases (`github.com`) | the precompiled Rust `.node` is not in the npm tarball | `JSQL_NO_NATIVE_DOWNLOAD=1` or `JSQL_OFFLINE=1` |
|
|
281
|
+
|
|
282
|
+
`npm install` downloads the prebuilt addon from the official GitHub release host
|
|
283
|
+
and verifies it against the published `SHA256SUMS`; a mismatch (or a missing
|
|
284
|
+
manifest) aborts the download. **No third-party mirror is used by default.** If
|
|
285
|
+
`github.com` is unreachable you can opt into a proxy explicitly:
|
|
286
|
+
|
|
287
|
+
| Variable | Meaning |
|
|
288
|
+
| --- | --- |
|
|
289
|
+
| `JSQL_GHPROXY` | Comma-separated proxy prefixes, tried in order (e.g. `https://gh-proxy.com/`). Empty/unset = direct only. |
|
|
290
|
+
| `JSQL_GHPROXY_TOKEN` | Optional bearer token, sent as `Authorization: Bearer …` **only** to the proxy origins listed in `JSQL_GHPROXY` (never to `github.com`, never on cross-origin redirects). |
|
|
291
|
+
| `JSQL_NATIVE_SHA256` | Pin the expected digest instead of reading `SHA256SUMS`. |
|
|
270
292
|
|
|
271
293
|
`JSQL_OFFLINE=1` disables every external request at once; affected commands
|
|
272
294
|
report the reason instead of failing silently. Loopback traffic (the
|
package/index.js
CHANGED
package/lib/database.js
CHANGED
|
@@ -103,7 +103,9 @@ class Database {
|
|
|
103
103
|
* @param {boolean} options.pretty - JSON 是否格式化(默认 true,仅旧文件模式)
|
|
104
104
|
* @param {string} options.encryptKey - 加密密钥(16/24/32 字节),启用 AES-256-CBC
|
|
105
105
|
* @param {boolean} options.versioning - 是否启用版本历史(默认 false)
|
|
106
|
-
* @param {boolean} options.wal - 是否启用 WAL
|
|
106
|
+
* @param {boolean} options.wal - 是否启用 WAL(默认 true:文件模式每次写入立即 append+fsync;
|
|
107
|
+
* 内存模式自动禁用;传 false 关闭;目录 hybrid/disk 模式与加密库默认关闭)
|
|
108
|
+
* @param {number} options.walCheckpointOps - WAL 累积多少条后自动做一次快照检查点(默认 1000,0 禁用)
|
|
107
109
|
* @param {string} options.isolationLevel - 事务隔离级别 'READ_COMMITTED' | 'REPEATABLE_READ'(默认 'READ_COMMITTED')
|
|
108
110
|
* @param {number} options.slowQueryThreshold - 慢查询阈值 ms(默认 100,0 记录全部,<0 禁用)
|
|
109
111
|
* @param {boolean} options.fileLock - 是否启用文件锁(默认 false)
|
|
@@ -157,13 +159,20 @@ class Database {
|
|
|
157
159
|
this._attached = {}; // 附加数据库 { name: db }
|
|
158
160
|
this._changeStream = null; // 变更流
|
|
159
161
|
|
|
160
|
-
// WAL(Write-Ahead Logging
|
|
161
|
-
|
|
162
|
+
// WAL(Write-Ahead Logging):文件模式下默认开启,每次写入立即追加并 fsync,
|
|
163
|
+
// 不再依赖「内存中改完、退出时 save()」的旧语义。
|
|
164
|
+
if (this._memoryMode) this._walEnabled = false;
|
|
165
|
+
else if (this._dirMode) this._walEnabled = options.wal === true;
|
|
166
|
+
else if (this._encryptKey) this._walEnabled = options.wal === true; // WAL 为明文,加密库需显式开启
|
|
167
|
+
else this._walEnabled = options.wal !== false;
|
|
162
168
|
this._walPath = this._filePath ? this._filePath + '.wal' : null;
|
|
163
169
|
this._walOps = []; // WAL 操作日志(内存镜像)
|
|
164
170
|
this._walFlushedCount = 0; // 已落盘记录数(append-only 增量追加用)
|
|
165
171
|
this._walReplaying = false; // 回放中:抑制再次写日志
|
|
166
172
|
this._walReplaySummary = null;
|
|
173
|
+
this._walOpCount = 0; // 自上次检查点以来的操作数
|
|
174
|
+
this._walCheckpointOps = options.walCheckpointOps !== undefined ? options.walCheckpointOps : 1000;
|
|
175
|
+
this._walCheckpointScheduled = false;
|
|
167
176
|
|
|
168
177
|
// 事务隔离:默认 REPEATABLE_READ(begin 时保存行快照,rollback 可回滚)
|
|
169
178
|
this._isolationLevel = options.isolationLevel || 'REPEATABLE_READ';
|
|
@@ -441,23 +450,89 @@ class Database {
|
|
|
441
450
|
* append-only 下崩溃最多丢失最后一行(已 fsync 过的行不会丢)。
|
|
442
451
|
*/
|
|
443
452
|
_walWrite(op) {
|
|
444
|
-
if (!this._walEnabled) return;
|
|
453
|
+
if (!this._walEnabled || this._loading) return;
|
|
445
454
|
const entry = {
|
|
446
455
|
...op,
|
|
447
456
|
timestamp: Date.now(),
|
|
448
457
|
sequence: this._walOps.length + 1
|
|
449
458
|
};
|
|
450
459
|
this._walOps.push(entry);
|
|
460
|
+
this._walOpCount++;
|
|
451
461
|
// 恢复过程中重放操作不能再次写 WAL,否则会无限增长
|
|
452
462
|
if (this._walReplaying) return;
|
|
453
463
|
this._flushWAL();
|
|
464
|
+
this._maybeCheckpointWAL();
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* 记录「写入后的行」:有主键时按主键幂等 upsert,无主键时退化为整表镜像。
|
|
469
|
+
* 这样任何写入都立刻落到 WAL,崩溃后按序回放即可恢复。
|
|
470
|
+
*/
|
|
471
|
+
_walLogUpsert(tableName, rows) {
|
|
472
|
+
if (!this._walEnabled || this._walReplaying || this._loading) return;
|
|
473
|
+
const t = this._tables[tableName];
|
|
474
|
+
if (!t || !Array.isArray(rows) || rows.length === 0) return;
|
|
475
|
+
if (t._primaryKey) {
|
|
476
|
+
const pk = t._primaryKey;
|
|
477
|
+
const out = rows.filter(r => r && r[pk] !== undefined && r[pk] !== null).map(r => ({ ...r }));
|
|
478
|
+
if (out.length === 0) return;
|
|
479
|
+
this._walWrite({ op: 'upsertRows', table: tableName, pk, rows: out });
|
|
480
|
+
} else {
|
|
481
|
+
this._walLogReplaceTable(tableName);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** 记录删除:有主键时只记主键列表,无主键时记录整表镜像。 */
|
|
486
|
+
_walLogRemove(tableName, removedRows) {
|
|
487
|
+
if (!this._walEnabled || this._walReplaying || this._loading) return;
|
|
488
|
+
const t = this._tables[tableName];
|
|
489
|
+
if (!t) return;
|
|
490
|
+
if (t._primaryKey && Array.isArray(removedRows) && removedRows.length) {
|
|
491
|
+
const pk = t._primaryKey;
|
|
492
|
+
const ids = removedRows.map(r => r && r[pk]).filter(v => v !== undefined && v !== null);
|
|
493
|
+
if (ids.length) {
|
|
494
|
+
this._walWrite({ op: 'removeRows', table: tableName, pk, ids });
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
this._walLogReplaceTable(tableName);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** 无主键表 / truncate / 结构变化:写入该表的完整行镜像。 */
|
|
502
|
+
_walLogReplaceTable(tableName) {
|
|
503
|
+
if (!this._walEnabled || this._walReplaying || this._loading) return;
|
|
504
|
+
const t = this._tables[tableName];
|
|
505
|
+
if (!t) return;
|
|
506
|
+
this._walWrite({ op: 'replaceTable', table: tableName, rows: t._rows.map(r => ({ ...r })) });
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* WAL 增长到阈值后自动做一次快照检查点,避免日志无限膨胀。
|
|
511
|
+
* 事务进行中不检查点,否则未提交的数据会进入快照。
|
|
512
|
+
*/
|
|
513
|
+
_maybeCheckpointWAL() {
|
|
514
|
+
if (!this._walEnabled || this._walCheckpointScheduled) return;
|
|
515
|
+
if (this._walCheckpointOps <= 0 || this._walOpCount < this._walCheckpointOps) return;
|
|
516
|
+
if (this._loading || this._walReplaying || this._transaction) return;
|
|
517
|
+
this._walCheckpointScheduled = true;
|
|
518
|
+
const run = () => {
|
|
519
|
+
this._walCheckpointScheduled = false;
|
|
520
|
+
if (this._loading || this._walReplaying || this._transaction) return;
|
|
521
|
+
try {
|
|
522
|
+
this.save();
|
|
523
|
+
} catch (e) {
|
|
524
|
+
// 快照失败时保留 WAL,数据仍可恢复
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
if (typeof setImmediate === 'function') setImmediate(run);
|
|
528
|
+
else setTimeout(run, 0);
|
|
454
529
|
}
|
|
455
530
|
|
|
456
531
|
/**
|
|
457
532
|
* 将新增的 WAL 记录追加到磁盘(append-only)
|
|
458
533
|
*/
|
|
459
534
|
_flushWAL() {
|
|
460
|
-
if (!this._walEnabled || this._walOps.length === 0) return;
|
|
535
|
+
if (!this._walEnabled || !this._walPath || this._walOps.length === 0) return;
|
|
461
536
|
try {
|
|
462
537
|
const dir = path.dirname(this._walPath);
|
|
463
538
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
@@ -489,6 +564,7 @@ class Database {
|
|
|
489
564
|
if (!this._walEnabled) return;
|
|
490
565
|
this._walOps = [];
|
|
491
566
|
this._walFlushedCount = 0;
|
|
567
|
+
this._walOpCount = 0;
|
|
492
568
|
try {
|
|
493
569
|
if (this._walPath && fs.existsSync(this._walPath)) {
|
|
494
570
|
fs.unlinkSync(this._walPath);
|
|
@@ -567,7 +643,45 @@ class Database {
|
|
|
567
643
|
case 'insert': {
|
|
568
644
|
const t = op.table && this._tables[op.table];
|
|
569
645
|
if (!t || !Array.isArray(op.rows) || op.rows.length === 0) { failed++; break; }
|
|
570
|
-
|
|
646
|
+
const pk = t._primaryKey;
|
|
647
|
+
for (const __e of op.rows) {
|
|
648
|
+
// 幂等:快照已包含该行时覆盖而不是重复插入
|
|
649
|
+
if (pk && __e && __e[pk] !== undefined) {
|
|
650
|
+
const ai = t._rows.findIndex(r => r && r[pk] === __e[pk]);
|
|
651
|
+
if (ai >= 0) { t._rows[ai] = __e; continue; }
|
|
652
|
+
}
|
|
653
|
+
t._rows.push(__e);
|
|
654
|
+
}
|
|
655
|
+
touched.add(t);
|
|
656
|
+
replayed++;
|
|
657
|
+
break;
|
|
658
|
+
}
|
|
659
|
+
case 'upsertRows': {
|
|
660
|
+
const t = op.table && this._tables[op.table];
|
|
661
|
+
if (!t || !op.pk || !Array.isArray(op.rows)) { failed++; break; }
|
|
662
|
+
for (const __r of op.rows) {
|
|
663
|
+
if (!__r || typeof __r !== 'object') continue;
|
|
664
|
+
const ai = t._rows.findIndex(r => r && String(r[op.pk]) === String(__r[op.pk]));
|
|
665
|
+
if (ai >= 0) t._rows[ai] = { ...__r };
|
|
666
|
+
else t._rows.push({ ...__r });
|
|
667
|
+
}
|
|
668
|
+
touched.add(t);
|
|
669
|
+
replayed++;
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
case 'removeRows': {
|
|
673
|
+
const t = op.table && this._tables[op.table];
|
|
674
|
+
if (!t || !op.pk || !Array.isArray(op.ids)) { failed++; break; }
|
|
675
|
+
const gone = new Set(op.ids.map(String));
|
|
676
|
+
t._rows = t._rows.filter(r => !r || !gone.has(String(r[op.pk])));
|
|
677
|
+
touched.add(t);
|
|
678
|
+
replayed++;
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
case 'replaceTable': {
|
|
682
|
+
const t = op.table && this._tables[op.table];
|
|
683
|
+
if (!t || !Array.isArray(op.rows)) { failed++; break; }
|
|
684
|
+
t._rows = op.rows.map(r => ({ ...r }));
|
|
571
685
|
touched.add(t);
|
|
572
686
|
replayed++;
|
|
573
687
|
break;
|
|
@@ -679,6 +793,9 @@ class Database {
|
|
|
679
793
|
case 'insert':
|
|
680
794
|
case 'update':
|
|
681
795
|
case 'remove':
|
|
796
|
+
case 'upsertRows':
|
|
797
|
+
case 'removeRows':
|
|
798
|
+
case 'replaceTable':
|
|
682
799
|
// 数据类操作由 _recoverFromWAL 按序直接处理(需维护表内行序),
|
|
683
800
|
// 不走这里。
|
|
684
801
|
return false;
|
|
@@ -688,6 +805,8 @@ class Database {
|
|
|
688
805
|
case 'begin':
|
|
689
806
|
case 'commit':
|
|
690
807
|
case 'rollback':
|
|
808
|
+
case 'rollbackTo':
|
|
809
|
+
case 'savepoint':
|
|
691
810
|
case 'undo':
|
|
692
811
|
case 'restore':
|
|
693
812
|
case 'import':
|
|
@@ -1375,6 +1494,11 @@ class Database {
|
|
|
1375
1494
|
|
|
1376
1495
|
if (this._transaction.snapshot) {
|
|
1377
1496
|
this._restoreTxSnapshot(this._transaction.snapshot);
|
|
1497
|
+
// 回滚后的状态必须重新写入 WAL:否则 WAL 里已记录的写入
|
|
1498
|
+
// 会在崩溃重放时被重新应用,回滚等于没做。
|
|
1499
|
+
for (const name of Object.keys(this._transaction.snapshot)) {
|
|
1500
|
+
if (this._tables[name]) this._walLogReplaceTable(name);
|
|
1501
|
+
}
|
|
1378
1502
|
} else {
|
|
1379
1503
|
// READ_COMMITTED:没有行快照,只能退回自增值
|
|
1380
1504
|
for (const [name, table] of Object.entries(this._tables)) {
|
|
@@ -1429,6 +1553,9 @@ class Database {
|
|
|
1429
1553
|
const list = this._requireSavepoints(name, 'ROLLBACK TO SAVEPOINT');
|
|
1430
1554
|
const idx = this._findSavepoint(list, name);
|
|
1431
1555
|
this._restoreTxSnapshot(list[idx].snap);
|
|
1556
|
+
for (const tableName of Object.keys(list[idx].snap)) {
|
|
1557
|
+
if (this._tables[tableName]) this._walLogReplaceTable(tableName);
|
|
1558
|
+
}
|
|
1432
1559
|
list.length = idx + 1;
|
|
1433
1560
|
this._walWrite({ op: 'rollbackTo', name });
|
|
1434
1561
|
this._markDirtyLegacy();
|
|
@@ -1806,17 +1933,11 @@ class Database {
|
|
|
1806
1933
|
if (!(await this._runHooks('beforeInsert', [tableName, arr]))) return [];
|
|
1807
1934
|
const pk = this._pkOf(table);
|
|
1808
1935
|
const ids = [];
|
|
1809
|
-
const logged = [];
|
|
1810
1936
|
for (const row of arr) {
|
|
1811
1937
|
const r = table.insert(row);
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
if (pk) logged.push({ ...r });
|
|
1816
|
-
else logged.push(r);
|
|
1817
|
-
}
|
|
1818
|
-
// 有主键才可幂等回放;无主键表回放会重复插行,故不记录
|
|
1819
|
-
if (pk) this._walWrite({ op: 'insert', table: tableName, rows: logged });
|
|
1938
|
+
ids.push(pk ? r[pk] : table._rows.length);
|
|
1939
|
+
}
|
|
1940
|
+
// WAL 已由 Table.insert() 在写入瞬间逐条落盘(含无主键表的整表镜像)
|
|
1820
1941
|
this._emit('insert', { table: tableName, count: arr.length, ids });
|
|
1821
1942
|
await this._runHooks('afterInsert', [tableName, arr, ids]);
|
|
1822
1943
|
this._markDirty(tableName);
|
|
@@ -1828,6 +1949,7 @@ class Database {
|
|
|
1828
1949
|
if (this._dirMode) {
|
|
1829
1950
|
this._flushDirty();
|
|
1830
1951
|
this._saveMeta();
|
|
1952
|
+
this._checkpoint();
|
|
1831
1953
|
} else if (!this._memoryMode) {
|
|
1832
1954
|
this.save();
|
|
1833
1955
|
}
|
|
@@ -1996,7 +2118,7 @@ class Database {
|
|
|
1996
2118
|
Object.assign(row, validated);
|
|
1997
2119
|
if (rowIndex !== -1) table._addToIndexes(rowIndex, row);
|
|
1998
2120
|
}
|
|
1999
|
-
|
|
2121
|
+
this._walLogUpsert(tableName, [row]);
|
|
2000
2122
|
this._emit('update', { table: tableName, id, data: validated });
|
|
2001
2123
|
await this._runHooks('afterUpdate', [tableName, id, validated, { ok: true }]);
|
|
2002
2124
|
this._markDirty(tableName);
|
|
@@ -2010,6 +2132,7 @@ class Database {
|
|
|
2010
2132
|
if (!(await this._runHooks('beforeUpdate', [tableName, pairs]))) return;
|
|
2011
2133
|
const pk = this._pkOf(table);
|
|
2012
2134
|
let count = 0;
|
|
2135
|
+
const changed = [];
|
|
2013
2136
|
for (const [id, data] of pairs) {
|
|
2014
2137
|
const row = this._resolveId(table, id);
|
|
2015
2138
|
if (row) {
|
|
@@ -2017,9 +2140,10 @@ class Database {
|
|
|
2017
2140
|
if (typeof table._enforceChecks === 'function') table._enforceChecks({ ...row, ...data });
|
|
2018
2141
|
Object.assign(row, data);
|
|
2019
2142
|
count++;
|
|
2020
|
-
|
|
2143
|
+
changed.push(row);
|
|
2021
2144
|
}
|
|
2022
2145
|
}
|
|
2146
|
+
if (changed.length) this._walLogUpsert(tableName, changed);
|
|
2023
2147
|
this._emit('update', { table: tableName, entries: pairs, result: { ok: true, count } });
|
|
2024
2148
|
await this._runHooks('afterUpdate', [tableName, pairs, { ok: true, count }]);
|
|
2025
2149
|
this._markDirty(tableName);
|
|
@@ -2033,7 +2157,7 @@ class Database {
|
|
|
2033
2157
|
if (table._primaryKey) {
|
|
2034
2158
|
const removed = table.removeById(id);
|
|
2035
2159
|
if (removed === 0) return { ok: false, error: 'not found' };
|
|
2036
|
-
|
|
2160
|
+
// WAL 由 Table.removeById() 记录
|
|
2037
2161
|
this._emit('delete', { table: tableName, id });
|
|
2038
2162
|
await this._runHooks('afterDelete', [tableName, id, { ok: true }]);
|
|
2039
2163
|
this._markDirty(tableName);
|
|
@@ -2045,6 +2169,7 @@ class Database {
|
|
|
2045
2169
|
table._rebuildPKIndex();
|
|
2046
2170
|
table._rebuildAllBTrees();
|
|
2047
2171
|
for (const field of Object.keys(table._indexes)) table.createIndex(field);
|
|
2172
|
+
this._walLogReplaceTable(tableName);
|
|
2048
2173
|
this._emit('delete', { table: tableName, id });
|
|
2049
2174
|
await this._runHooks('afterDelete', [tableName, id, { ok: true }]);
|
|
2050
2175
|
this._markDirty(tableName);
|
|
@@ -2087,6 +2212,7 @@ class Database {
|
|
|
2087
2212
|
table._rebuildPKIndex();
|
|
2088
2213
|
table._rebuildAllBTrees();
|
|
2089
2214
|
for (const field of Object.keys(table._indexes)) table.createIndex(field);
|
|
2215
|
+
if (removed > 0) this._walLogReplaceTable(tableName);
|
|
2090
2216
|
}
|
|
2091
2217
|
this._emit('delete', { table: tableName, ids, result: { ok: true, count: removed } });
|
|
2092
2218
|
await this._runHooks('afterDelete', [tableName, ids, { ok: true, count: removed }]);
|
|
@@ -2107,6 +2233,7 @@ class Database {
|
|
|
2107
2233
|
table._rebuildPKIndex();
|
|
2108
2234
|
table._rebuildAllBTrees();
|
|
2109
2235
|
for (const field of Object.keys(table._indexes)) table.createIndex(field);
|
|
2236
|
+
if (removed > 0) this._walLogRemove(tableName, matched);
|
|
2110
2237
|
this._emit('delete', { table: tableName, ids: [], result: { ok: true, count: removed } });
|
|
2111
2238
|
await this._runHooks('afterDelete', [tableName, [filter], { ok: true, count: removed }]);
|
|
2112
2239
|
this._markDirty(tableName);
|
|
@@ -2119,10 +2246,7 @@ class Database {
|
|
|
2119
2246
|
const arr = Array.isArray(items) ? items : [items];
|
|
2120
2247
|
if (!(await this._runHooks('beforeInsert', [tableName, arr]))) return [];
|
|
2121
2248
|
const inserted = table.insertMany(arr);
|
|
2122
|
-
//
|
|
2123
|
-
if (table._primaryKey && inserted.length > 0) {
|
|
2124
|
-
this._walWrite({ op: 'insert', table: tableName, rows: inserted });
|
|
2125
|
-
}
|
|
2249
|
+
// WAL 由 Table.insertMany() 记录
|
|
2126
2250
|
this._emit('insert', { table: tableName, count: arr.length });
|
|
2127
2251
|
await this._runHooks('afterInsert', [tableName, arr, inserted]);
|
|
2128
2252
|
this._markDirty(tableName);
|
|
@@ -2134,14 +2258,7 @@ class Database {
|
|
|
2134
2258
|
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
2135
2259
|
if (!(await this._runHooks('beforeUpdate', [tableName, query, updates]))) return 0;
|
|
2136
2260
|
const count = table.update(query, updates);
|
|
2137
|
-
//
|
|
2138
|
-
if (table._primaryKey && count > 0) {
|
|
2139
|
-
const updatedRows = table.find(query);
|
|
2140
|
-
for (const row of updatedRows) {
|
|
2141
|
-
const id = row[table._primaryKey];
|
|
2142
|
-
if (id !== undefined) this._walWrite({ op: 'update', table: tableName, pk: table._primaryKey, id, row: { ...row } });
|
|
2143
|
-
}
|
|
2144
|
-
}
|
|
2261
|
+
// WAL 由 Table.update() 记录
|
|
2145
2262
|
this._emit('update', { table: tableName, query, count });
|
|
2146
2263
|
await this._runHooks('afterUpdate', [tableName, query, updates, count]);
|
|
2147
2264
|
this._markDirty(tableName);
|
|
@@ -2152,14 +2269,7 @@ class Database {
|
|
|
2152
2269
|
const table = this._ensureTable(tableName);
|
|
2153
2270
|
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
2154
2271
|
if (!(await this._runHooks('beforeDelete', [tableName, query]))) return 0;
|
|
2155
|
-
//
|
|
2156
|
-
if (table._primaryKey) {
|
|
2157
|
-
const toDelete = table.find(query);
|
|
2158
|
-
for (const row of toDelete) {
|
|
2159
|
-
const id = row[table._primaryKey];
|
|
2160
|
-
if (id !== undefined) this._walWrite({ op: 'remove', table: tableName, pk: table._primaryKey, id });
|
|
2161
|
-
}
|
|
2162
|
-
}
|
|
2272
|
+
// WAL 由 Table.remove() 记录
|
|
2163
2273
|
const count = table.remove(query);
|
|
2164
2274
|
this._emit('delete', { table: tableName, query, count });
|
|
2165
2275
|
await this._runHooks('afterDelete', [tableName, query, count]);
|
|
@@ -2170,14 +2280,7 @@ class Database {
|
|
|
2170
2280
|
truncate(tableName) {
|
|
2171
2281
|
const table = this._ensureTable(tableName);
|
|
2172
2282
|
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
2173
|
-
//
|
|
2174
|
-
if (table._primaryKey) {
|
|
2175
|
-
const toDelete = table.find({});
|
|
2176
|
-
for (const row of toDelete) {
|
|
2177
|
-
const id = row[table._primaryKey];
|
|
2178
|
-
if (id !== undefined) this._walWrite({ op: 'remove', table: tableName, pk: table._primaryKey, id });
|
|
2179
|
-
}
|
|
2180
|
-
}
|
|
2283
|
+
// WAL 由 Table.truncate() 记录整表镜像
|
|
2181
2284
|
const count = table.truncate();
|
|
2182
2285
|
this._emit('delete', { table: tableName, count });
|
|
2183
2286
|
this._markDirty(tableName);
|
|
@@ -2194,7 +2297,11 @@ class Database {
|
|
|
2194
2297
|
try {
|
|
2195
2298
|
this._flushDirty();
|
|
2196
2299
|
this._saveMeta();
|
|
2300
|
+
this._checkpoint();
|
|
2197
2301
|
} catch (e) {}
|
|
2302
|
+
} else if (!this._memoryMode) {
|
|
2303
|
+
// 正常关闭时做一次快照检查点,缩短下次启动的回放时间
|
|
2304
|
+
try { this.save(); } catch (e) {}
|
|
2198
2305
|
}
|
|
2199
2306
|
if (this._sigInstalled) {
|
|
2200
2307
|
this._sigInstalled = false;
|
|
@@ -2304,6 +2411,7 @@ class Database {
|
|
|
2304
2411
|
if (this._dirMode) {
|
|
2305
2412
|
this._flushDirty();
|
|
2306
2413
|
this._saveMeta();
|
|
2414
|
+
this._checkpoint();
|
|
2307
2415
|
return;
|
|
2308
2416
|
}
|
|
2309
2417
|
|