jsql-neo 6.6.0 → 6.6.1

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
@@ -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.0** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.6.1** — 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
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.6.0 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.6.1 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/date-types.js CHANGED
@@ -77,12 +77,20 @@ function toUtcDateString(d) {
77
77
  return d.getUTCFullYear() + '-' + pad2(d.getUTCMonth() + 1) + '-' + pad2(d.getUTCDate());
78
78
  }
79
79
 
80
- /** UTC 墙上时间的 'YYYY-MM-DD HH:MM:SS' */
80
+ /** UTC 墙上时间的 'YYYY-MM-DD HH:MM:SS'(整秒) */
81
81
  function toUtcDateTimeString(d) {
82
82
  return toUtcDateString(d) + ' ' +
83
83
  pad2(d.getUTCHours()) + ':' + pad2(d.getUTCMinutes()) + ':' + pad2(d.getUTCSeconds());
84
84
  }
85
85
 
86
+ /** 保留亚秒精度(#82):仅当存在非零毫秒时追加 '.sss',整秒输入不变。
87
+ * 用于“字符串里显式携带小数秒”的解析路径;Date 对象/NOW() 走整秒 toUtcDateTimeString。 */
88
+ function toUtcDateTimeStringMs(d) {
89
+ const base = toUtcDateTimeString(d);
90
+ const ms = d.getUTCMilliseconds();
91
+ return ms ? base + '.' + String(ms).padStart(3, '0') : base;
92
+ }
93
+
86
94
  /**
87
95
  * 把「日期/时间部件 ± 可选时区标记」解析成绝对时刻(毫秒)。
88
96
  * 无时区标记 → 按 UTC 墙上时间;带 Z / ±HH:MM → 按标记换算。
@@ -192,11 +200,14 @@ function validateDateTime(value, fieldName) {
192
200
  }
193
201
  if (typeof value === 'string') {
194
202
  // ISO format: 2024-01-15T10:30:00.000Z / +08:00(可能带时区标记)
195
- const isoMatch = value.match(/^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})/);
203
+ // #82:捕获可选的亚秒 .sss,避免 .500 被静默截断。
204
+ const isoMatch = value.match(/^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(?:\.(\d{1,3}))?/);
196
205
  if (isoMatch) {
197
206
  // P2-35: 验证日期和时间有效性
198
207
  const datePart = isoMatch[1];
199
208
  const timePart = isoMatch[2];
209
+ const fracPart = isoMatch[3];
210
+ const msPart = fracPart ? parseInt(fracPart.padEnd(3, '0')) : 0;
200
211
  const dm = datePart.match(/^(\d{4})-(\d{2})-(\d{2})$/);
201
212
  const tm = timePart.match(/^(\d{2}):(\d{2}):(\d{2})$/);
202
213
  if (dm && tm) {
@@ -210,10 +221,10 @@ function validateDateTime(value, fieldName) {
210
221
  // 二者因此在 UTC 字面量上完全等价(#75 round-trip)。
211
222
  const tz = parseIsoTimezone(value.slice(isoMatch[0].length));
212
223
  if (tz !== null) {
213
- const ms = Date.UTC(y, mo - 1, d, h, mi, s) - tz * 60000;
214
- return toUtcDateTimeString(new Date(ms));
224
+ const ms = Date.UTC(y, mo - 1, d, h, mi, s) + msPart - tz * 60000;
225
+ return toUtcDateTimeStringMs(new Date(ms));
215
226
  }
216
- return datePart + ' ' + timePart;
227
+ return datePart + ' ' + timePart + (msPart ? '.' + String(msPart).padStart(3, '0') : '');
217
228
  }
218
229
  }
219
230
  }
@@ -237,7 +248,7 @@ function validateDateTime(value, fieldName) {
237
248
  }
238
249
  const ms = parseToUtcMs(value);
239
250
  if (ms !== null) {
240
- return toUtcDateTimeString(new Date(ms));
251
+ return toUtcDateTimeStringMs(new Date(ms));
241
252
  }
242
253
  // 仅带明确时区标记时允许原生解析兜底
243
254
  if (/[Zz]$|[+-]\d{2}:?\d{2}$/.test(value)) {
@@ -110,6 +110,15 @@ function matchFieldValue(value, condition) {
110
110
  return looseEqual(value, condition);
111
111
  }
112
112
 
113
+ // #81:$pull 的“元素是否命中”判定。
114
+ // 数组元素可能是标量('red')或对象({name:'red'});条件 v 可能是
115
+ // 原始值、正则、$op 对象,或普通对象查询(按字段匹配,NeDB 语义)。
116
+ function elementMatch(el, v) {
117
+ const isQueryObject = v && typeof v === 'object' && !Array.isArray(v) &&
118
+ !(v instanceof RegExp) && !Object.keys(v).some(k => k.startsWith('$'));
119
+ return isQueryObject ? match(el, v) : matchFieldValue(el, v);
120
+ }
121
+
113
122
  function match(doc, query) {
114
123
  if (!query) return true;
115
124
  if (typeof query === 'function') return !!query(doc);
@@ -193,7 +202,11 @@ function applyModifier(doc, update) {
193
202
  } else if (op === '$pull') {
194
203
  for (const [k, v] of Object.entries(payload)) {
195
204
  const arr = getPath(doc, k);
196
- if (Array.isArray(arr)) setPath(doc, k, arr.filter(el => !match(el, { k: v })));
205
+ // #81:$pull 之前把条件包成 { k: v }(硬编码键 "k"),导致永远不匹配、
206
+ // 数组原样保留(no-op)。
207
+ // NeDB 语义:对数组每个元素做 match(element, v) —— 标量元素直接与 v 比较,
208
+ // 对象元素按字段匹配。这里用 elementMatch 统一处理,真正把匹配元素移除。
209
+ if (Array.isArray(arr)) setPath(doc, k, arr.filter(el => !elementMatch(el, v)));
197
210
  }
198
211
  }
199
212
  }
package/lib/plugin.js CHANGED
@@ -103,12 +103,13 @@ function definePlugin(name, def = {}) {
103
103
  return new Plugin(name, def).build();
104
104
  }
105
105
 
106
- /** 登记单个钩子;未知钩子名会自动创建,便于插件自定义扩展点 */
107
- function registerHook(engine, hook, fn) {
106
+ /** 登记单个钩子;未知钩子名会自动创建,便于插件自定义扩展点。
107
+ * 同时记录登记方的 priority,供 runHooks 按 priority 排序(#84)。 */
108
+ function registerHook(engine, hook, fn, priority = 0) {
108
109
  if (typeof fn !== 'function') return engine;
109
110
  if (!engine._hooks) engine._hooks = createHookRegistry();
110
111
  if (!engine._hooks[hook]) engine._hooks[hook] = [];
111
- engine._hooks[hook].push(fn);
112
+ engine._hooks[hook].push({ fn, priority: priority || 0 });
112
113
  return engine;
113
114
  }
114
115
 
@@ -117,7 +118,7 @@ function registerHooks(engine, plugin) {
117
118
  if (!plugin || !plugin.hooks) return engine;
118
119
  for (const [hook, val] of Object.entries(plugin.hooks)) {
119
120
  const fns = Array.isArray(val) ? val : [val];
120
- for (const fn of fns) registerHook(engine, hook, fn);
121
+ for (const fn of fns) registerHook(engine, hook, fn, plugin.priority || 0);
121
122
  }
122
123
  return engine;
123
124
  }
@@ -253,8 +254,11 @@ function runHooks(engine, hookName, args) {
253
254
 
254
255
  return (async () => {
255
256
  try {
256
- for (const fn of hooks) {
257
- const result = await fn(...args);
257
+ // #84:按插件 priority 降序执行(数值大的先跑)。此前只对 engine._plugins
258
+ // 排序,钩子数组本身保持注册顺序,导致 priority 被完全忽略。
259
+ const ordered = hooks.slice().sort((a, b) => (b.priority || 0) - (a.priority || 0));
260
+ for (const entry of ordered) {
261
+ const result = await entry.fn(...args);
258
262
  if (result === false) return false;
259
263
  }
260
264
  return true;
package/lib/sql.js CHANGED
@@ -2,6 +2,8 @@
2
2
  const { createError } = require('./errors');
3
3
  // #75:WHERE 里 DATETIME 字面量要与 INSERT 走同一套规范化规则
4
4
  const { validateDateType } = require('./date-types');
5
+ // #78:UNION/INTERSECT/EXCEPT 去重键对 BigInt 安全(JSON.stringify 遇到 BigInt 抛错)
6
+ const bigintJson = require('./bigint_json');
5
7
 
6
8
  const DANGEROUS_SQL = [
7
9
  { re: /^INTO$/, next: /^(OUTFILE|DUMPFILE)$/, name: 'INTO OUTFILE/DUMPFILE' },
@@ -4979,7 +4981,8 @@ function extractEqualPushdown(expr, schema) {
4979
4981
  function dedupeRows(rows) {
4980
4982
  const seen = new Set();
4981
4983
  return rows.filter(r => {
4982
- const k = JSON.stringify(r);
4984
+ // #78:去重键用 BigInt 安全序列化,否则 UNION/INTERSECT/EXCEPT 遇 bigint 列抛错
4985
+ const k = bigintJson.stringify(r);
4983
4986
  if (seen.has(k)) return false;
4984
4987
  seen.add(k);
4985
4988
  return true;
@@ -5006,7 +5009,10 @@ function assertSetOpColumnCount(left, right) {
5006
5009
  // 避免把 true 与字符串 'true' 的比较悄悄改成 1 与 'true' 的比较。
5007
5010
  function normalizeCmpValue(a, other) {
5008
5011
  if (typeof a !== 'boolean') return a;
5009
- const otherIsNumeric = typeof other === 'number' || typeof other === 'boolean';
5012
+ // #83:MySQL BOOL/TINYINT(1) 语义下,布尔与数值字符串 '1'/'0' 直接可比。
5013
+ // 此前只认 number/boolean,导致 WHERE b = '1'(b 为 BOOLEAN)恒为空。
5014
+ const otherIsNumeric = typeof other === 'number' || typeof other === 'boolean' ||
5015
+ (typeof other === 'string' && /^[0-9]+$/.test(other.trim()));
5010
5016
  return otherIsNumeric ? (a ? 1 : 0) : a;
5011
5017
  }
5012
5018
 
@@ -5025,6 +5031,41 @@ function mysqlNumeric(v) {
5025
5031
  return NaN;
5026
5032
  }
5027
5033
 
5034
+ // #77:BIGINT 列比较不能用 String(...) 字典序("50" > "1000" 为真,但 50 < 1000)。
5035
+ // 这里提供 bigint/number 混合的精确数值相等与三路比较。
5036
+ // 仅当两个操作数都是整数可无损转换时才用 BigInt 精确比较;
5037
+ // 遇到小数(BigInt 无法表示)则回退到 Number 比较。
5038
+ function numEq(a, b) {
5039
+ if (typeof a === 'bigint' && typeof b === 'bigint') return a === b;
5040
+ if (typeof a === 'bigint') {
5041
+ if (typeof b !== 'number' || !Number.isInteger(b)) return false;
5042
+ return a === BigInt(b);
5043
+ }
5044
+ if (typeof b === 'bigint') {
5045
+ if (typeof a !== 'number' || !Number.isInteger(a)) return false;
5046
+ return BigInt(a) === b;
5047
+ }
5048
+ return a === b;
5049
+ }
5050
+ function numCmp(a, b) {
5051
+ if (typeof a === 'bigint' && typeof b === 'bigint') return a < b ? -1 : a > b ? 1 : 0;
5052
+ if (typeof a === 'bigint') {
5053
+ if (typeof b === 'number' && !Number.isInteger(b)) {
5054
+ const x = Number(a); return x < b ? -1 : x > b ? 1 : 0;
5055
+ }
5056
+ const bb = BigInt(b);
5057
+ return a < bb ? -1 : a > bb ? 1 : 0;
5058
+ }
5059
+ if (typeof b === 'bigint') {
5060
+ if (typeof a === 'number' && !Number.isInteger(a)) {
5061
+ const y = Number(b); return a < y ? -1 : a > y ? 1 : 0;
5062
+ }
5063
+ const aa = BigInt(a);
5064
+ return aa < b ? -1 : aa > b ? 1 : 0;
5065
+ }
5066
+ return a < b ? -1 : a > b ? 1 : 0;
5067
+ }
5068
+
5028
5069
  /** 严格数值解析:只有真正能解析成有限数值时才返回 number,否则 null。
5029
5070
  * 用来判断字符串边界是否“看起来像数字”('9' / '0.01' / ' 100 ' 是,'abc' 不是)。
5030
5071
  * 与 mysqlNumeric 的区别:非数字字符串返回 null 而不是 0 —— 判断“要不要按数值比较”
@@ -5084,9 +5125,29 @@ function sqlXor(a, b) {
5084
5125
  function evaluateExpr(expr, row, ctx) {
5085
5126
  if (expr === null || expr === undefined) return false;
5086
5127
  switch (expr.type) {
5087
- case 'and': return evaluateExpr(expr.left, row, ctx) && evaluateExpr(expr.right, row, ctx);
5088
- case 'or': return evaluateExpr(expr.left, row, ctx) || evaluateExpr(expr.right, row, ctx);
5089
- case 'xor': return Boolean(evaluateExpr(expr.left, row, ctx)) !== Boolean(evaluateExpr(expr.right, row, ctx));
5128
+ // #80:AND/OR/XOR 必须遵循 SQL 三值逻辑,UNKNOWN(null) 要传播,
5129
+ // 不能把 Boolean(null) 塌缩成 false(否则 TRUE XOR UNKNOWN 被误判为真)。
5130
+ // 这里复用 sqlTruthy(返回 true/false/null)与 3VL 真值表,结果统一为 true/false/null。
5131
+ case 'and': {
5132
+ const a = sqlTruthy(evaluateExpr(expr.left, row, ctx));
5133
+ const b = sqlTruthy(evaluateExpr(expr.right, row, ctx));
5134
+ if (a === false || b === false) return false;
5135
+ if (a === null || b === null) return null;
5136
+ return true;
5137
+ }
5138
+ case 'or': {
5139
+ const a = sqlTruthy(evaluateExpr(expr.left, row, ctx));
5140
+ const b = sqlTruthy(evaluateExpr(expr.right, row, ctx));
5141
+ if (a === true || b === true) return true;
5142
+ if (a === null || b === null) return null;
5143
+ return false;
5144
+ }
5145
+ case 'xor': {
5146
+ const a = sqlTruthy(evaluateExpr(expr.left, row, ctx));
5147
+ const b = sqlTruthy(evaluateExpr(expr.right, row, ctx));
5148
+ if (a === null || b === null) return null;
5149
+ return a !== b;
5150
+ }
5090
5151
  case 'not': {
5091
5152
  // 三值逻辑:NOT UNKNOWN = UNKNOWN(返回 null,在 WHERE/ON/HAVING 中视为不选中)
5092
5153
  // 必须用 resolveOperand 取值:evaluateExpr 只认布尔类节点,
@@ -5116,20 +5177,45 @@ function evaluateExpr(expr, row, ctx) {
5116
5177
  // 覆盖 0 = '' → 0 = 0 → 1、'a' = 0 → 0 = 0 → 1、'5' = 5 → 5 = 5 → 1(#26)。
5117
5178
  // 此前只对「数字 vs 纯数字字符串」生效,'' / 'a' 这类非纯数字字符串
5118
5179
  // 会退化成字符串比较而错判为不等。
5119
- const isNum = (v) => typeof v === 'number';
5180
+ const isNum = (v) => typeof v === 'number' || typeof v === 'bigint';
5120
5181
  // MySQL 类型强转(#26):数字 vs 字符串统一走全局 mysqlNum
5121
5182
  // (按数值前缀解析,非数字/空串视为 0,对齐 MySQL 宽松转换)。
5122
5183
  // 覆盖 '5' = 5、0 = ''、'a' = 0、'09' = 9 等,避免退化成字典序比较。
5184
+ // #77:大整数与纯整数字符串按 BigInt 精确比较,防止丢精度。
5123
5185
  const isStr = (v) => typeof v === 'string';
5124
5186
  const numVsStr = (isNum(nl) && isStr(nr)) || (isStr(nl) && isNum(nr));
5125
5187
  if (numVsStr) {
5126
- const an = mysqlNum(nl), bn = mysqlNum(nr);
5127
- if (expr.op === '=') return an === bn;
5128
- const fn = OPERATORS[expr.op];
5129
- return fn(an, bn);
5188
+ const toNum = (v) => {
5189
+ if (typeof v === 'bigint') return v;
5190
+ if (typeof v === 'string' && /^-?\d+$/.test(v.trim())) { try { return BigInt(v.trim()); } catch (_) {} }
5191
+ return mysqlNum(v);
5192
+ };
5193
+ const an = toNum(nl), bn = toNum(nr);
5194
+ if (expr.op === '=') return numEq(an, bn);
5195
+ if (expr.op === '!=' || expr.op === '<>') return !numEq(an, bn);
5196
+ const nc = numCmp(an, bn);
5197
+ switch (expr.op) {
5198
+ case '<': return nc < 0;
5199
+ case '<=': return nc <= 0;
5200
+ case '>': return nc > 0;
5201
+ case '>=': return nc >= 0;
5202
+ default: return nc === 0;
5203
+ }
5204
+ }
5205
+ // #77:BIGINT 列 vs 数值(bigint 或 number)必须走数值比较,不能退化成字典序
5206
+ if (isNum(nl) && isNum(nr)) {
5207
+ if (expr.op === '=') return numEq(nl, nr);
5208
+ if (expr.op === '!=' || expr.op === '<>') return !numEq(nl, nr);
5209
+ const nc = numCmp(nl, nr);
5210
+ switch (expr.op) {
5211
+ case '<': return nc < 0;
5212
+ case '<=': return nc <= 0;
5213
+ case '>': return nc > 0;
5214
+ case '>=': return nc >= 0;
5215
+ default: return nc === 0;
5216
+ }
5130
5217
  }
5131
5218
  if (expr.op === '=') return ciEq(nl, nr, cs);
5132
- if (typeof nl === 'number' && typeof nr === 'number') { const fn = OPERATORS[expr.op]; return fn(nl, nr); }
5133
5219
  const c = ciCompare(nl, nr, cs);
5134
5220
  switch (expr.op) {
5135
5221
  case '!=': case '<>': return c !== 0;
@@ -7710,8 +7796,8 @@ class SQLExecutor {
7710
7796
  });
7711
7797
  }
7712
7798
  if (!isAll) {
7713
- const seen = new Set(accumulated.map(r => JSON.stringify(r)));
7714
- next = next.filter(r => !seen.has(JSON.stringify(r)));
7799
+ const seen = new Set(accumulated.map(r => bigintJson.stringify(r)));
7800
+ next = next.filter(r => !seen.has(bigintJson.stringify(r)));
7715
7801
  }
7716
7802
  if (next.length === 0) break;
7717
7803
  // 超过行数上限说明递归条件写错(恒真),提前收手而不是撑爆内存
@@ -8058,8 +8144,8 @@ class SQLExecutor {
8058
8144
  const left = await this.executeSelect(leftStatement);
8059
8145
  const right = await this.executeSelect(statement.intersect.select);
8060
8146
  assertSetOpColumnCount(left, right);
8061
- const seen = new Set(right.rows.map(r => JSON.stringify(r)));
8062
- let out = left.rows.filter(r => seen.has(JSON.stringify(r)));
8147
+ const seen = new Set(right.rows.map(r => bigintJson.stringify(r)));
8148
+ let out = left.rows.filter(r => seen.has(bigintJson.stringify(r)));
8063
8149
  if (!statement.intersect.all) out = dedupeRows(out);
8064
8150
  out = this._finalizeSetOp(out, left.columns, statement);
8065
8151
  return { ok: true, type: 'select', columns: left.columns, rows: out, raw: out };
@@ -8072,8 +8158,8 @@ class SQLExecutor {
8072
8158
  const left = await this.executeSelect(leftStatement);
8073
8159
  const right = await this.executeSelect(statement.except.select);
8074
8160
  assertSetOpColumnCount(left, right);
8075
- const seen = new Set(right.rows.map(r => JSON.stringify(r)));
8076
- let out = left.rows.filter(r => !seen.has(JSON.stringify(r)));
8161
+ const seen = new Set(right.rows.map(r => bigintJson.stringify(r)));
8162
+ let out = left.rows.filter(r => !seen.has(bigintJson.stringify(r)));
8077
8163
  if (!statement.except.all) out = dedupeRows(out);
8078
8164
  out = this._finalizeSetOp(out, left.columns, statement);
8079
8165
  return { ok: true, type: 'select', columns: left.columns, rows: out, raw: out };
package/lib/table.js CHANGED
@@ -729,6 +729,12 @@ class Table {
729
729
  if (rowIndex === undefined) return 0;
730
730
  const row = this._rows[rowIndex];
731
731
  if (!row || row._deleted) return 0;
732
+ // #79:按 id 删除也要触发 beforeRemove(与 remove() 一致),返回 false 则中止。
733
+ // 此前 removeById 只触发 afterRemove,beforeRemove 钩子静默失效。
734
+ const removeQuery = { [this._primaryKey]: id };
735
+ for (const hook of this._hooks.beforeRemove) {
736
+ if (hook(removeQuery) === false) return 0;
737
+ }
732
738
  this._cascadeForeignKeys(row);
733
739
  this._removeFromIndexes(rowIndex, row);
734
740
  const lastIdx = this._rows.length - 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.6.0",
3
+ "version": "6.6.1",
4
4
  "description": "JSQL-NEO — Rust-powered embedded database with WASM, REST API, B-Tree indexes, WAL, crash recovery",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -50,12 +50,12 @@
50
50
  "scripts": {
51
51
  "postinstall": "node postinstall.js",
52
52
  "build:native": "node scripts/build-native.js",
53
- "test": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js",
53
+ "test": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js",
54
54
  "test:parser": "node test/sql-parser.test.js",
55
55
  "test:btree": "node test/btree.test.js",
56
56
  "test:native": "node test/native.test.js",
57
57
  "test:regress": "node test/regress-5.1.0.js",
58
- "test:all": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js",
58
+ "test:all": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js",
59
59
  "test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
60
60
  "test:orms": "node examples/orms/run-all.js",
61
61
  "test:dml": "node test/dml-constraints.test.js",
@@ -55,10 +55,10 @@ function storedOf(iso) { return utcStr(Date.parse(iso)); }
55
55
  const expectMinus5 = storedOf('2024-01-15T10:30:00-05:00');
56
56
  ok('#50 ISO -05:00 偏移正确换算', r.rows[0][0] === expectMinus5, { got: r.rows[0][0], want: expectMinus5 });
57
57
 
58
- // 4) 带毫秒 + Z
58
+ // 4) 带毫秒 + Z(#82:保留亚秒精度,不再静默截断为整秒)
59
59
  await q("INSERT INTO t (id, dt) VALUES (4, '2024-01-15T10:30:00.500Z')");
60
60
  r = await q('SELECT dt FROM t WHERE id = 4');
61
- ok('#50 带毫秒的 Z 同样换算(毫秒截断)', r.rows[0][0] === expectZ, r.rows[0][0]);
61
+ ok('#50 带毫秒的 Z 同样换算(保留亚秒 .500)', r.rows[0][0] === '2024-01-15 10:30:00.500', r.rows[0][0]);
62
62
 
63
63
  // 5) 不带时区标记:字面值即 UTC 墙上时间,原样保存(与 Z 等价)
64
64
  await q("INSERT INTO t (id, dt) VALUES (5, '2024-01-15T10:30:00')");
@@ -0,0 +1,137 @@
1
+ // 回归测试:Issue #77-#84
2
+ // #77 WHERE on BIGINT 用字典序字符串比较导致错误结果
3
+ // #78 UNION/INTERSECT/EXCEPT 遇 BIGINT 列 TypeError
4
+ // #79 BEFORE DELETE trigger 在 removeById() 上不触发
5
+ // #80 XOR 违反 SQL 三值逻辑(UNKNOWN 塌缩成 FALSE)
6
+ // #81 NeDB-compat $pull 是 no-op(硬编码键 "k")
7
+ // #82 DATETIME 静默丢弃亚秒精度
8
+ // #83 BOOLEAN 列与数字字符串字面量 '1' 比较返回空
9
+ // #84 插件 priority 被忽略(钩子按注册顺序执行)
10
+ const { executeSQL } = require('../lib/sql');
11
+ const Database = require('../lib/database');
12
+ const { Datastore } = require('../lib/nedb_compat');
13
+ const dateTypes = require('../lib/date-types');
14
+
15
+ let pass = 0, fail = 0;
16
+ function ok(name, cond, extra) {
17
+ if (cond) { pass++; console.log('[OK] ', name); }
18
+ else { fail++; console.log('[FAIL]', name, extra !== undefined ? ' -> ' + JSON.stringify(extra).slice(0, 300) : ''); }
19
+ }
20
+ const engine = (schema, rows) => ({
21
+ hasTable: () => true,
22
+ find: async () => rows,
23
+ getTableSchema: async () => schema,
24
+ truncate: async () => {}, flush: async () => {},
25
+ });
26
+
27
+ (async () => {
28
+ // #77 BIGINT WHERE 比较
29
+ {
30
+ const r = await executeSQL(engine(
31
+ { id: { type: 'integer', primaryKey: true }, big: { type: 'bigint' } },
32
+ [{ id: 1, big: 50n }, { id: 2, big: 2000n }]
33
+ ), 'SELECT id FROM t WHERE big > 1000', { safety: false });
34
+ ok('#77 bigint > 1000 只返回 id=2', r.rows.map(x => x[0]).join(',') === '2', r.rows);
35
+ }
36
+ {
37
+ const r = await executeSQL(engine(
38
+ { id: { type: 'integer', primaryKey: true }, big: { type: 'bigint' } },
39
+ [{ id: 1, big: 50n }, { id: 2, big: 2000n }]
40
+ ), 'SELECT id FROM t WHERE big = 50', { safety: false });
41
+ ok('#77 bigint = 50 数值相等', r.rows.map(x => x[0]).join(',') === '1', r.rows);
42
+ }
43
+
44
+ // #78 UNION / INTERSECT / EXCEPT on bigint
45
+ {
46
+ const r = await executeSQL(engine(
47
+ { big: { type: 'bigint' } },
48
+ [{ big: 50n }, { big: 2000n }]
49
+ ), 'SELECT big FROM t UNION SELECT big FROM t', { safety: false });
50
+ ok('#78 UNION bigint 不抛错且去重', Array.isArray(r.rows) && r.rows.length === 2, r.rows);
51
+ }
52
+ {
53
+ const r = await executeSQL(engine(
54
+ { big: { type: 'bigint' } },
55
+ [{ big: 50n }]
56
+ ), 'SELECT big FROM t INTERSECT SELECT big FROM t', { safety: false });
57
+ ok('#78 INTERSECT bigint 不抛错', Array.isArray(r.rows) && r.rows.length === 1, r.rows);
58
+ }
59
+ {
60
+ const r = await executeSQL(engine(
61
+ { big: { type: 'bigint' } },
62
+ [{ big: 50n }, { big: 2000n }]
63
+ ), 'SELECT big FROM t EXCEPT SELECT big FROM t WHERE big = 50', { safety: false });
64
+ ok('#78 EXCEPT bigint 不抛错', Array.isArray(r.rows), r.rows);
65
+ }
66
+
67
+ // #79 BEFORE DELETE on removeById
68
+ {
69
+ const db = new Database(':memory:');
70
+ await db.createTable('t', { id: { type: 'integer', primaryKey: true } });
71
+ let fired = 0;
72
+ db.createTrigger('tr', { event: 'remove', table: 't', timing: 'before' }, () => { fired++; });
73
+ await db.insert('t', { id: 1 });
74
+ await db.removeById('t', 1);
75
+ ok('#79 removeById 触发 before remove', fired === 1, { fired });
76
+ }
77
+
78
+ // #80 XOR 三值逻辑
79
+ {
80
+ const r = await executeSQL(engine(
81
+ { id: { type: 'integer', primaryKey: true }, a: { type: 'integer' } },
82
+ [{ id: 1, a: null }, { id: 2, a: 1 }, { id: 3, a: 2 }]
83
+ ), 'SELECT id FROM t WHERE TRUE XOR (a = 1)', { safety: false });
84
+ ok('#80 TRUE XOR (a=1) 只返回 id=3(UNKNOWN 不塌缩)', r.rows.map(x => x[0]).join(',') === '3', r.rows);
85
+ }
86
+
87
+ // #81 $pull
88
+ {
89
+ const ds = new Datastore({ inMemoryOnly: true });
90
+ await ds.loadDatabase();
91
+ const doc = await ds.insert({ tags: ['red', 'green', 'blue'] });
92
+ await ds.update({ _id: doc._id }, { $pull: { tags: 'red' } });
93
+ const after = await ds.findOne({ _id: doc._id });
94
+ ok('#81 $pull 移除匹配元素', after.tags.join(',') === 'green,blue', after.tags);
95
+ }
96
+ {
97
+ const ds = new Datastore({ inMemoryOnly: true });
98
+ await ds.loadDatabase();
99
+ const doc = await ds.insert({ items: [{ n: 1 }, { n: 2 }, { n: 3 }] });
100
+ await ds.update({ _id: doc._id }, { $pull: { items: { n: 2 } } });
101
+ const after = await ds.findOne({ _id: doc._id });
102
+ ok('#81 $pull 对象数组按字段匹配', after.items.length === 2 && after.items[1].n === 3, after.items);
103
+ }
104
+
105
+ // #82 DATETIME 亚秒
106
+ {
107
+ const out = dateTypes.validateDateType('2024-01-15T10:30:00.500Z', 'datetime', 'f');
108
+ ok('#82 保留 .500 亚秒', out === '2024-01-15 10:30:00.500', out);
109
+ }
110
+ {
111
+ const out = dateTypes.validateDateType('2024-01-15T10:30:00Z', 'datetime', 'f');
112
+ ok('#82 整秒不带 .000', out === '2024-01-15 10:30:00', out);
113
+ }
114
+
115
+ // #83 BOOLEAN vs '1'
116
+ {
117
+ const r = await executeSQL(engine(
118
+ { id: { type: 'integer', primaryKey: true }, b: { type: 'boolean' } },
119
+ [{ id: 1, b: true }, { id: 2, b: false }]
120
+ ), "SELECT id FROM t WHERE b = '1'", { safety: false });
121
+ ok('#83 b = \'1\' 返回 id=1', r.rows.map(x => x[0]).join(',') === '1', r.rows);
122
+ }
123
+
124
+ // #84 plugin priority
125
+ {
126
+ const db = new Database(':memory:');
127
+ await db.createTable('t', { id: { type: 'integer', primaryKey: true } });
128
+ const order = [];
129
+ await db.use({ name: 'low', priority: 1, hooks: { beforeInsert: () => order.push('low') } });
130
+ await db.use({ name: 'high', priority: 10, hooks: { beforeInsert: () => order.push('high') } });
131
+ await db.insert('t', { id: 1 });
132
+ ok('#84 高 priority 先执行', order.join(',') === 'high,low', order);
133
+ }
134
+
135
+ console.log(`\n${pass} passed, ${fail} failed`);
136
+ process.exit(fail === 0 ? 0 : 1);
137
+ })().catch(e => { console.error('FATAL', e); process.exit(1); });