jsql-neo 6.9.0 → 6.10.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 +4 -2
- package/index.d.ts +2 -0
- package/index.js +1 -1
- package/lib/bigint_json.js +13 -1
- package/lib/btree.js +12 -3
- package/lib/database.js +56 -8
- package/lib/table.js +52 -11
- package/package.json +1 -1
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.
|
|
6
|
+
> **v6.10.0** — 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
|

|
|
@@ -175,7 +175,9 @@ writes on exit. Every `insert` / `insertMany` / `update` / `updateById` / `remov
|
|
|
175
175
|
writes it to the kernel before returning, so a `kill -9` loses nothing that already
|
|
176
176
|
committed. `fsync` is coalesced into a short window (`walSyncInterval`, default 20 ms)
|
|
177
177
|
so bulk single-row writes no longer pay one disk flush per row — set it to `0` for the
|
|
178
|
-
strictest per-write `fsync` behaviour (roughly an order of magnitude slower).
|
|
178
|
+
strictest per-write `fsync` behaviour (roughly an order of magnitude slower). Each insert
|
|
179
|
+
still appends its own record; `db.syncWAL()` forces an immediate lightweight `fsync`
|
|
180
|
+
(no snapshot) when you need a durability barrier at an exact point.
|
|
179
181
|
`save()` / `close()` / `stop()` checkpoint the WAL (write a full snapshot, `fsync`, then
|
|
180
182
|
truncate) and always flush any pending window first.
|
|
181
183
|
WAL is on by default for file mode — pass `wal: false` for the old semantics, or tune
|
package/index.d.ts
CHANGED
|
@@ -361,6 +361,8 @@ declare namespace JSQLNeo {
|
|
|
361
361
|
constructor(filePath?: string, options?: DatabaseOptions);
|
|
362
362
|
createTable(name: string, schema: Schema): Promise<unknown>;
|
|
363
363
|
insert(table: string, data: Record<string, unknown>): Promise<unknown>;
|
|
364
|
+
/** 轻量持久化屏障:只 fsync 已写入的 WAL 记录,不写全量快照。 */
|
|
365
|
+
syncWAL(): this;
|
|
364
366
|
find(table: string, filter?: Record<string, unknown>): Promise<Row[]>;
|
|
365
367
|
exists(table: string, filter?: Record<string, unknown>): boolean;
|
|
366
368
|
prepare(sql: string): PreparedStatement;
|
package/index.js
CHANGED
package/lib/bigint_json.js
CHANGED
|
@@ -41,7 +41,19 @@ function reviver(_key, value) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
function stringify(value, space) {
|
|
44
|
-
|
|
44
|
+
// 快路径:绝大多数行不含 BigInt / 标记对象。replacer 会对每个属性回调一次,
|
|
45
|
+
// 百万行批量写入时是主要 CPU 开销;这里先走原生无 replacer 序列化,
|
|
46
|
+
// 仅在结果里出现标记(需要 #169 转义)或抛 BigInt TypeError 时回退慢路径。
|
|
47
|
+
try {
|
|
48
|
+
const text = JSON.stringify(value, undefined, space);
|
|
49
|
+
if (typeof text === 'string' && text.indexOf(MARKER) !== -1) {
|
|
50
|
+
return JSON.stringify(value, replacer, space);
|
|
51
|
+
}
|
|
52
|
+
return text;
|
|
53
|
+
} catch (e) {
|
|
54
|
+
if (e instanceof TypeError) return JSON.stringify(value, replacer, space);
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
45
57
|
}
|
|
46
58
|
|
|
47
59
|
function parse(text) {
|
package/lib/btree.js
CHANGED
|
@@ -78,9 +78,18 @@ class BTree {
|
|
|
78
78
|
|
|
79
79
|
_insertNonFull(node, key, rowIndex) {
|
|
80
80
|
if (node.leaf) {
|
|
81
|
-
|
|
81
|
+
// 顺序追加快路径(自增主键 / 已排序批量导入):key 大于当前末键时直接 push,
|
|
82
|
+
// 省掉二分查找和 splice 的返回值分配。批量插入 100 万行时是主要热点。
|
|
83
|
+
const keys = node.keys;
|
|
84
|
+
const last = keys.length - 1;
|
|
85
|
+
if (last < 0 || key > keys[last]) {
|
|
86
|
+
keys.push(key);
|
|
87
|
+
node.values.push([rowIndex]);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const i = this._lowerBound(keys, key);
|
|
82
91
|
|
|
83
|
-
if (i <
|
|
92
|
+
if (i < keys.length && keys[i] === key) {
|
|
84
93
|
if (this._unique) {
|
|
85
94
|
this._size--;
|
|
86
95
|
return;
|
|
@@ -92,7 +101,7 @@ class BTree {
|
|
|
92
101
|
return;
|
|
93
102
|
}
|
|
94
103
|
|
|
95
|
-
|
|
104
|
+
keys.splice(i, 0, key);
|
|
96
105
|
node.values.splice(i, 0, [rowIndex]);
|
|
97
106
|
} else {
|
|
98
107
|
let i = this._route(node, key);
|
package/lib/database.js
CHANGED
|
@@ -762,7 +762,9 @@ class Database {
|
|
|
762
762
|
if (!t || !Array.isArray(rows) || rows.length === 0) return;
|
|
763
763
|
if (t._primaryKey) {
|
|
764
764
|
const pk = t._primaryKey;
|
|
765
|
-
|
|
765
|
+
// 不复制行对象:WAL 在本次调用内同步序列化落盘,
|
|
766
|
+
// 批量插入几十万行时浅拷贝本身就是一笔可观开销。
|
|
767
|
+
const out = rows.filter(r => r && r[pk] !== undefined && r[pk] !== null);
|
|
766
768
|
if (out.length === 0) return;
|
|
767
769
|
this._walWrite({ op: 'upsertRows', table: tableName, pk, rows: out });
|
|
768
770
|
} else {
|
|
@@ -791,7 +793,7 @@ class Database {
|
|
|
791
793
|
if (!this._walEnabled || this._walReplaying || this._loading) return;
|
|
792
794
|
const t = this._tables[tableName];
|
|
793
795
|
if (!t) return;
|
|
794
|
-
this._walWrite({ op: 'replaceTable', table: tableName, rows: t._rows
|
|
796
|
+
this._walWrite({ op: 'replaceTable', table: tableName, rows: t._rows });
|
|
795
797
|
}
|
|
796
798
|
|
|
797
799
|
/**
|
|
@@ -1053,6 +1055,27 @@ class Database {
|
|
|
1053
1055
|
// insert 不走 _loadRows()(那是整体替换语义),而是直接 push 到 _rows,
|
|
1054
1056
|
// 再在末尾统一重建索引;否则逐条回放会互相覆盖。
|
|
1055
1057
|
const touched = new Set();
|
|
1058
|
+
// 回放热路径:按表缓存 pk -> 行下标,避免逐行 findIndex 造成 O(n²)。
|
|
1059
|
+
// 一次 20 万行的批量 upsert 回放在旧实现里会退化成 400 亿次比较(几乎等于挂死)。
|
|
1060
|
+
const replayIdx = new Map(); // table -> Map(pkField -> Map(pkKey -> rowIndex))
|
|
1061
|
+
const pkKeyOf = (v) => typeof v + ':' + String(v);
|
|
1062
|
+
const idxMapOf = (t, pkField) => {
|
|
1063
|
+
let byField = replayIdx.get(t);
|
|
1064
|
+
if (!byField) { byField = new Map(); replayIdx.set(t, byField); }
|
|
1065
|
+
let m = byField.get(pkField);
|
|
1066
|
+
if (!m) {
|
|
1067
|
+
m = new Map();
|
|
1068
|
+
for (let i = 0; i < t._rows.length; i++) {
|
|
1069
|
+
const row = t._rows[i];
|
|
1070
|
+
if (row && row[pkField] !== undefined && row[pkField] !== null) {
|
|
1071
|
+
m.set(pkKeyOf(row[pkField]), i);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
byField.set(pkField, m);
|
|
1075
|
+
}
|
|
1076
|
+
return m;
|
|
1077
|
+
};
|
|
1078
|
+
const invalidateIdx = (t) => { replayIdx.delete(t); };
|
|
1056
1079
|
for (const op of ops) {
|
|
1057
1080
|
try {
|
|
1058
1081
|
// 事务标记帧本身不改数据状态,回放时跳过。
|
|
@@ -1072,11 +1095,15 @@ class Database {
|
|
|
1072
1095
|
}
|
|
1073
1096
|
if (!t || !Array.isArray(op.rows) || op.rows.length === 0) { failed++; break; }
|
|
1074
1097
|
const pk = t._primaryKey;
|
|
1098
|
+
const idm = pk ? idxMapOf(t, pk) : null;
|
|
1075
1099
|
for (const __e of op.rows) {
|
|
1100
|
+
if (!__e) continue;
|
|
1076
1101
|
// 幂等:快照已包含该行时覆盖而不是重复插入
|
|
1077
|
-
if (
|
|
1078
|
-
const
|
|
1079
|
-
|
|
1102
|
+
if (idm && __e[pk] !== undefined && __e[pk] !== null) {
|
|
1103
|
+
const k = pkKeyOf(__e[pk]);
|
|
1104
|
+
const ai = idm.get(k);
|
|
1105
|
+
if (ai !== undefined) { t._rows[ai] = __e; continue; }
|
|
1106
|
+
idm.set(k, t._rows.length);
|
|
1080
1107
|
}
|
|
1081
1108
|
t._rows.push(__e);
|
|
1082
1109
|
}
|
|
@@ -1092,11 +1119,17 @@ class Database {
|
|
|
1092
1119
|
t = this._tables[op.table];
|
|
1093
1120
|
}
|
|
1094
1121
|
if (!t || !op.pk || !Array.isArray(op.rows)) { failed++; break; }
|
|
1122
|
+
const idm = idxMapOf(t, op.pk);
|
|
1095
1123
|
for (const __r of op.rows) {
|
|
1096
1124
|
if (!__r || typeof __r !== 'object') continue;
|
|
1097
|
-
const
|
|
1098
|
-
|
|
1099
|
-
|
|
1125
|
+
const k = pkKeyOf(__r[op.pk]);
|
|
1126
|
+
const ai = idm.get(k);
|
|
1127
|
+
if (ai !== undefined) {
|
|
1128
|
+
t._rows[ai] = { ...__r };
|
|
1129
|
+
} else {
|
|
1130
|
+
idm.set(k, t._rows.length);
|
|
1131
|
+
t._rows.push({ ...__r });
|
|
1132
|
+
}
|
|
1100
1133
|
}
|
|
1101
1134
|
touched.add(t);
|
|
1102
1135
|
replayed++;
|
|
@@ -1107,6 +1140,7 @@ class Database {
|
|
|
1107
1140
|
if (!t || !op.pk || !Array.isArray(op.ids)) { failed++; break; }
|
|
1108
1141
|
const gone = new Set(op.ids.map(String));
|
|
1109
1142
|
t._rows = t._rows.filter(r => !r || !gone.has(String(r[op.pk])));
|
|
1143
|
+
invalidateIdx(t);
|
|
1110
1144
|
touched.add(t);
|
|
1111
1145
|
replayed++;
|
|
1112
1146
|
break;
|
|
@@ -1119,6 +1153,7 @@ class Database {
|
|
|
1119
1153
|
}
|
|
1120
1154
|
if (!t || !Array.isArray(op.rows)) { failed++; break; }
|
|
1121
1155
|
t._rows = op.rows.map(r => ({ ...r }));
|
|
1156
|
+
invalidateIdx(t);
|
|
1122
1157
|
touched.add(t);
|
|
1123
1158
|
replayed++;
|
|
1124
1159
|
break;
|
|
@@ -1138,6 +1173,7 @@ class Database {
|
|
|
1138
1173
|
if (!t || !op.pk) { failed++; break; }
|
|
1139
1174
|
const before = t._rows.length;
|
|
1140
1175
|
t._rows = t._rows.filter(r => r[op.pk] !== op.id);
|
|
1176
|
+
invalidateIdx(t);
|
|
1141
1177
|
if (t._rows.length === before) { failed++; break; }
|
|
1142
1178
|
touched.add(t);
|
|
1143
1179
|
replayed++;
|
|
@@ -2668,6 +2704,18 @@ class Database {
|
|
|
2668
2704
|
return this;
|
|
2669
2705
|
}
|
|
2670
2706
|
|
|
2707
|
+
/**
|
|
2708
|
+
* 轻量持久化屏障:只把已写入的 WAL 记录 fsync 到磁盘,不写全量快照。
|
|
2709
|
+
*
|
|
2710
|
+
* 每条记录在写入时已单独 append 并 write() 进内核;默认 fsync 按
|
|
2711
|
+
* `walSyncInterval` 窗口合并。需要「此刻就必须抗掉电」时调用本方法,
|
|
2712
|
+
* 比 flush()/save()(全量快照)轻得多。
|
|
2713
|
+
*/
|
|
2714
|
+
syncWAL() {
|
|
2715
|
+
if (this._walEnabled) this._syncWAL();
|
|
2716
|
+
return this;
|
|
2717
|
+
}
|
|
2718
|
+
|
|
2671
2719
|
_resolveId(table, id) {
|
|
2672
2720
|
const pk = this._pkOf(table);
|
|
2673
2721
|
if (pk) {
|
package/lib/table.js
CHANGED
|
@@ -647,6 +647,18 @@ class Table {
|
|
|
647
647
|
const schema = this._schema;
|
|
648
648
|
const checkConstraints = this._checkConstraints;
|
|
649
649
|
const foreignKeys = this._foreignKeys;
|
|
650
|
+
// 批量热路径:把与行无关的反射结果提到循环外,避免每行都 Object.entries/keys 分配。
|
|
651
|
+
const defaultEntries = Object.entries(defaults);
|
|
652
|
+
const computedKeys = Object.keys(computedFields);
|
|
653
|
+
const btreeEntries = Object.entries(this._btrees);
|
|
654
|
+
const schemaFields = this._cachedSchemaFields;
|
|
655
|
+
const dateFieldEntries = Object.entries(this._dateFields);
|
|
656
|
+
// 类型归一计划:按列预计算 [field, lowerType, def],避免每行都 Object.entries + toLowerCase。
|
|
657
|
+
const typePlan = { inPlace: true, dateEntries: dateFieldEntries, schemaEntries: [] };
|
|
658
|
+
for (const [field, def] of Object.entries(schema)) {
|
|
659
|
+
if (field === '_softDelete') continue;
|
|
660
|
+
typePlan.schemaEntries.push([field, String(def.type || '').toLowerCase(), def]);
|
|
661
|
+
}
|
|
650
662
|
// 批量预分配自增 ID 范围:先扫描显式提供的最大值,一次性推进计数器,
|
|
651
663
|
// 再在循环内用本地序号递增,避免批量内重复读取/写入同一计数器字段。
|
|
652
664
|
const baseAutoInc = this._autoIncrement;
|
|
@@ -674,13 +686,17 @@ class Table {
|
|
|
674
686
|
try {
|
|
675
687
|
// 在副本上做默认值/计算列/类型归一,避免污染调用方传入的原始对象
|
|
676
688
|
let data = { ...items[i] };
|
|
677
|
-
for (
|
|
689
|
+
for (let di = 0; di < defaultEntries.length; di++) {
|
|
690
|
+
const f = defaultEntries[di][0], dv = defaultEntries[di][1];
|
|
678
691
|
if (data[f] === undefined) data[f] = resolveDefaultValue(dv, f);
|
|
679
692
|
}
|
|
680
|
-
for (
|
|
693
|
+
for (let ci = 0; ci < computedKeys.length; ci++) {
|
|
694
|
+
const f = computedKeys[ci];
|
|
695
|
+
data[f] = computedFields[f](data);
|
|
696
|
+
}
|
|
681
697
|
// 与单条 insert 保持一致:归一时间/数值/布尔/字符串类型(此前 insertMany 跳过了这一步)
|
|
682
|
-
data = this._validateDataTypes(data);
|
|
683
|
-
for (const field of
|
|
698
|
+
data = this._validateDataTypes(data, false, typePlan);
|
|
699
|
+
for (const field of schemaFields) {
|
|
684
700
|
const def = schema[field];
|
|
685
701
|
if (def.required && (data[field] === undefined || data[field] === null)
|
|
686
702
|
&& !(def.autoIncrement && data[field] === undefined)) throw createError('ER_BAD_NULL_ERROR', field);
|
|
@@ -709,7 +725,10 @@ class Table {
|
|
|
709
725
|
this._enforceChecks(data);
|
|
710
726
|
this._checkCompositeUnique(data, -1, compositeUniqueSets);
|
|
711
727
|
// 主键唯一性检查(主键可能不在 _cachedUniqueFields 中)
|
|
712
|
-
|
|
728
|
+
// 自增主键由引擎在下方统一分配(baseAutoInc + 序号),天然唯一,跳过两次哈希查找。
|
|
729
|
+
const pkAutoAssigned = autoIncField && autoIncField === this._primaryKey
|
|
730
|
+
&& (data[autoIncField] === undefined || data[autoIncField] === null || data[autoIncField] === 0);
|
|
731
|
+
if (!pkAutoAssigned && this._primaryKey && data[this._primaryKey] !== undefined && data[this._primaryKey] !== null) {
|
|
713
732
|
const pkVal = data[this._primaryKey];
|
|
714
733
|
if (this._pkIndex && this._pkIndex.has(pkVal)) {
|
|
715
734
|
throw createError('ER_DUP_ENTRY', String(pkVal), this._primaryKey);
|
|
@@ -768,7 +787,8 @@ class Table {
|
|
|
768
787
|
this._pkIndex.set(pkVal, rowIdx);
|
|
769
788
|
}
|
|
770
789
|
}
|
|
771
|
-
for (
|
|
790
|
+
for (let bi = 0; bi < btreeEntries.length; bi++) {
|
|
791
|
+
const field = btreeEntries[bi][0], tree = btreeEntries[bi][1];
|
|
772
792
|
const val = data[field];
|
|
773
793
|
if (val !== undefined && val !== null) tree.insert(val, rowIdx);
|
|
774
794
|
}
|
|
@@ -2335,20 +2355,41 @@ class Table {
|
|
|
2335
2355
|
}
|
|
2336
2356
|
}
|
|
2337
2357
|
|
|
2338
|
-
_validateDataTypes(data, partial = false) {
|
|
2339
|
-
|
|
2340
|
-
|
|
2358
|
+
_validateDataTypes(data, partial = false, plan = null) {
|
|
2359
|
+
// 批量路径传入 plan:复用预计算的列清单,且允许就地归一(调用方已持有独立副本),
|
|
2360
|
+
// 避免每行复制对象 + 每行 Object.entries/toLowerCase。
|
|
2361
|
+
const result = plan && plan.inPlace ? data : { ...data };
|
|
2362
|
+
const dateFields = plan ? plan.dateEntries : Object.entries(this._dateFields);
|
|
2363
|
+
for (let i = 0; i < dateFields.length; i++) {
|
|
2364
|
+
const field = dateFields[i][0], type = dateFields[i][1];
|
|
2341
2365
|
if (partial && data[field] === undefined) continue;
|
|
2342
2366
|
if (data[field] !== undefined && data[field] !== null) {
|
|
2343
2367
|
result[field] = validateDateType(data[field], type, field);
|
|
2344
2368
|
}
|
|
2345
2369
|
}
|
|
2370
|
+
const schemaEntries = plan ? plan.schemaEntries : null;
|
|
2371
|
+
const total = schemaEntries ? schemaEntries.length : 0;
|
|
2372
|
+
if (schemaEntries) {
|
|
2373
|
+
for (let i = 0; i < total; i++) {
|
|
2374
|
+
const field = schemaEntries[i][0], t = schemaEntries[i][1], def = schemaEntries[i][2];
|
|
2375
|
+
if (partial && data[field] === undefined) continue;
|
|
2376
|
+
this._coerceField(result, field, t, def, data);
|
|
2377
|
+
}
|
|
2378
|
+
return result;
|
|
2379
|
+
}
|
|
2346
2380
|
for (const [field, def] of Object.entries(this._schema)) {
|
|
2347
2381
|
if (field === '_softDelete') continue;
|
|
2348
2382
|
if (partial && data[field] === undefined) continue;
|
|
2383
|
+
this._coerceField(result, field, String(def.type || '').toLowerCase(), def, data);
|
|
2384
|
+
}
|
|
2385
|
+
return result;
|
|
2386
|
+
}
|
|
2387
|
+
|
|
2388
|
+
/** 单列类型归一(insert/update 共用),结果写入 result[field]。 */
|
|
2389
|
+
_coerceField(result, field, t, def, data) {
|
|
2390
|
+
{
|
|
2349
2391
|
const v = data[field];
|
|
2350
|
-
if (v === undefined || v === null)
|
|
2351
|
-
const t = String(def.type || '').toLowerCase();
|
|
2392
|
+
if (v === undefined || v === null) return;
|
|
2352
2393
|
if (t === 'integer' || t === 'int' || t === 'bigint') {
|
|
2353
2394
|
if (typeof v === 'boolean') throw integerError(t, v);
|
|
2354
2395
|
if (typeof v === 'bigint') {
|