jsql-neo 6.5.22 → 6.5.24

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/lib/table.js CHANGED
@@ -9,6 +9,45 @@ const BTree = require('./btree');
9
9
  const { validateDateType, now } = require('./date-types');
10
10
  const { createError } = require('./errors');
11
11
 
12
+ // ---------------------------------------------------------------------------
13
+ // 64 位整数(Issue #38)
14
+ //
15
+ // JS 的 number 只能精确表示 |v| <= 2^53-1。integer/int/bigint 列原先用
16
+ // Number(v) 归一,>2^53 的输入(无论 number 还是十进制字符串)都会被静默
17
+ // 截断成 53 位浮点,SQL 路径则因为字面量被解析成 BigInt 而报 1292 —— 两条
18
+ // 路径行为不一致且都丢精度。
19
+ //
20
+ // 现在的语义:
21
+ // • BigInt 输入 → 精确接受(在 int64 范围内);
22
+ // • 十进制字符串输入 → 精确解析成 BigInt,安全范围内仍存成 number(兼容旧行为);
23
+ // • 超安全范围的 number → 明确报 1292(调用方在 JS 层就已丢精度,不能静默入库);
24
+ // • 超出 int64 范围 → 明确报 1292(持久化格式是 8 字节有符号整数)。
25
+ // ---------------------------------------------------------------------------
26
+ const INT64_MIN = -9223372036854775808n;
27
+ const INT64_MAX = 9223372036854775807n;
28
+ const SAFE_INT_MIN = BigInt(Number.MIN_SAFE_INTEGER);
29
+ const SAFE_INT_MAX = BigInt(Number.MAX_SAFE_INTEGER);
30
+ const DECIMAL_INT_RE = /^[+-]?\d+$/;
31
+
32
+ function integerError(type, v) {
33
+ return createError('ER_TRUNCATED_WRONG_VALUE', type, v);
34
+ }
35
+
36
+ /**
37
+ * 把已经确认是十进制整数的字符串/BigInt 归一成 number(安全范围内)或 BigInt。
38
+ * 超出 int64 抛 1292,避免持久化时被 BigInt64 写入静默回绕。
39
+ */
40
+ function normalizeInteger(v, type) {
41
+ let b;
42
+ if (typeof v === 'bigint') b = v;
43
+ else {
44
+ try { b = BigInt(v); } catch (_) { throw integerError(type, v); }
45
+ }
46
+ if (b < INT64_MIN || b > INT64_MAX) throw integerError(type, b.toString());
47
+ if (b >= SAFE_INT_MIN && b <= SAFE_INT_MAX) return Number(b);
48
+ return b;
49
+ }
50
+
12
51
  /**
13
52
  * 会被 _compileMatcher 编译成快路径的操作符。
14
53
  *
@@ -318,10 +357,14 @@ class Table {
318
357
  uniqueTracker[field].add(data[field]);
319
358
  }
320
359
  if (def.length && typeof data[field] === 'string' && data[field].length > def.length) throw createError('ER_DATA_TOO_LONG', field);
321
- if (def.type === 'integer' || def.type === 'number') {
322
- const v = def.type === 'integer' ? Number(data[field]) : data[field];
323
- if (data[field] !== undefined && data[field] !== null && def.min !== undefined && v < def.min) throw createError('ER_OUT_OF_RANGE', field);
324
- if (data[field] !== undefined && data[field] !== null && def.max !== undefined && v > def.max) throw createError('ER_OUT_OF_RANGE', field);
360
+ if (def.type === 'integer' || def.type === 'int' || def.type === 'bigint' || def.type === 'number') {
361
+ const raw = data[field];
362
+ // BigInt 与 number 的 </> 比较是精确的,不要先 Number() 丢精度
363
+ const v = typeof raw === 'bigint' ? raw : (def.type === 'number' ? raw : Number(raw));
364
+ const min = typeof raw === 'bigint' && Number.isInteger(def.min) ? BigInt(def.min) : def.min;
365
+ const max = typeof raw === 'bigint' && Number.isInteger(def.max) ? BigInt(def.max) : def.max;
366
+ if (raw !== undefined && raw !== null && def.min !== undefined && v < min) throw createError('ER_OUT_OF_RANGE', field);
367
+ if (raw !== undefined && raw !== null && def.max !== undefined && v > max) throw createError('ER_OUT_OF_RANGE', field);
325
368
  }
326
369
  if (def.check && data[field] !== undefined && !def.check(data[field], data)) throw createError('ER_CHECK_CONSTRAINT', this._name + '.' + field);
327
370
  }
@@ -1430,10 +1473,14 @@ class Table {
1430
1473
  }
1431
1474
 
1432
1475
  // 数值范围
1433
- if (def.type === 'integer' && data[field] !== undefined && data[field] !== null) {
1434
- const v = Number(data[field]);
1435
- if (def.min !== undefined && v < def.min) throw createError('ER_OUT_OF_RANGE', field);
1436
- if (def.max !== undefined && v > def.max) throw createError('ER_OUT_OF_RANGE', field);
1476
+ if ((def.type === 'integer' || def.type === 'int' || def.type === 'bigint')
1477
+ && data[field] !== undefined && data[field] !== null) {
1478
+ const raw = data[field];
1479
+ const v = typeof raw === 'bigint' ? raw : Number(raw);
1480
+ const min = typeof raw === 'bigint' && Number.isInteger(def.min) ? BigInt(def.min) : def.min;
1481
+ const max = typeof raw === 'bigint' && Number.isInteger(def.max) ? BigInt(def.max) : def.max;
1482
+ if (def.min !== undefined && v < min) throw createError('ER_OUT_OF_RANGE', field);
1483
+ if (def.max !== undefined && v > max) throw createError('ER_OUT_OF_RANGE', field);
1437
1484
  }
1438
1485
  }
1439
1486
 
@@ -1537,14 +1584,20 @@ class Table {
1537
1584
  if (v === undefined || v === null) continue;
1538
1585
  const t = String(def.type || '').toLowerCase();
1539
1586
  if (t === 'integer' || t === 'int' || t === 'bigint') {
1540
- if (typeof v === 'boolean') throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
1541
- if (typeof v === 'number') {
1542
- if (Number.isInteger(v)) result[field] = v;
1543
- else throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
1544
- } else if (typeof v === 'string' && v.trim() !== '' && Number.isInteger(Number(v))) {
1545
- result[field] = Number(v);
1587
+ if (typeof v === 'boolean') throw integerError(t, v);
1588
+ if (typeof v === 'bigint') {
1589
+ // 显式 BigInt:>2^53 也能精确保存(Issue #38)。
1590
+ result[field] = normalizeInteger(v, t);
1591
+ } else if (typeof v === 'number') {
1592
+ if (!Number.isInteger(v)) throw integerError(t, v);
1593
+ // 超过 2^53-1 的 number 在调用方就已经丢失精度,不能再假装
1594
+ // 存进去了:明确报错,要求传 BigInt 或十进制字符串。
1595
+ if (!Number.isSafeInteger(v)) throw integerError(t, v);
1596
+ result[field] = v;
1597
+ } else if (typeof v === 'string' && DECIMAL_INT_RE.test(v.trim())) {
1598
+ result[field] = normalizeInteger(v.trim(), t);
1546
1599
  } else {
1547
- throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
1600
+ throw integerError(t, v);
1548
1601
  }
1549
1602
  } else if (t === 'float' || t === 'double' || t === 'number' || t === 'real') {
1550
1603
  if (typeof v === 'boolean') throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
@@ -1,5 +1,7 @@
1
1
  const path = require('path');
2
2
  const { executeSQL } = require('./sql');
3
+ const { createError } = require('./errors');
4
+ const bigintJson = require('./bigint_json');
3
5
  const {
4
6
  createHookRegistry, applyPlugin, buildPluginContext, registerHook,
5
7
  runHooks, getPluginApis
@@ -77,6 +79,73 @@ function assertRowObject(row, index, isBatch) {
77
79
  throw new TypeError(`insert(): expected a row object${where}, got ${got}`);
78
80
  }
79
81
 
82
+ const INT64_MIN = -9223372036854775808n;
83
+ const INT64_MAX = 9223372036854775807n;
84
+ const SAFE_INT_MIN = BigInt(Number.MIN_SAFE_INTEGER);
85
+ const SAFE_INT_MAX = BigInt(Number.MAX_SAFE_INTEGER);
86
+ const DECIMAL_INT_RE = /^[+-]?\d+$/;
87
+
88
+ function isIntegerType(t) {
89
+ return t === 'integer' || t === 'int' || t === 'bigint';
90
+ }
91
+
92
+ /** 与 lib/table.js 的 normalizeInteger 一致(Issue #38)。 */
93
+ function normalizeInteger(v, type) {
94
+ let b;
95
+ if (typeof v === 'bigint') b = v;
96
+ else {
97
+ try { b = BigInt(String(v).trim()); } catch (_) { throw createError('ER_TRUNCATED_WRONG_VALUE', type, v); }
98
+ }
99
+ if (b < INT64_MIN || b > INT64_MAX) throw createError('ER_TRUNCATED_WRONG_VALUE', type, b.toString());
100
+ return (b >= SAFE_INT_MIN && b <= SAFE_INT_MAX) ? Number(b) : b;
101
+ }
102
+
103
+ /** Rust 绑定层把 >2^53 的整数以十进制字符串返回,这里还原为 BigInt。 */
104
+ function restoreInteger(v) {
105
+ if (typeof v === 'bigint') return v;
106
+ if (typeof v === 'string' && DECIMAL_INT_RE.test(v)) {
107
+ const b = BigInt(v);
108
+ return (b >= SAFE_INT_MIN && b <= SAFE_INT_MAX) ? Number(b) : b;
109
+ }
110
+ if (typeof v === 'number' && Number.isInteger(v) && !Number.isSafeInteger(v)) return BigInt(v);
111
+ return v;
112
+ }
113
+
114
+ /**
115
+ * 传给 Rust 的整数列值:BigInt / 十进制字符串 / 安全 number 转成精确值;
116
+ * 越界或小数/布尔直接抛 1292(与 JS 引擎一致)。
117
+ */
118
+ function normalizeIntegerInput(v, type) {
119
+ if (typeof v === 'bigint') return normalizeInteger(v, type);
120
+ if (typeof v === 'number') {
121
+ if (!Number.isInteger(v) || !Number.isSafeInteger(v)) throw createError('ER_TRUNCATED_WRONG_VALUE', type, v);
122
+ return v;
123
+ }
124
+ if (typeof v === 'string' && DECIMAL_INT_RE.test(v.trim())) return normalizeInteger(v.trim(), type);
125
+ throw createError('ER_TRUNCATED_WRONG_VALUE', type, v);
126
+ }
127
+
128
+ /** 按 schema 做插入/更新前的值归一(JSON 列序列化、整数列精确化)。 */
129
+ function normalizeRowForRust(table, schema, row) {
130
+ const out = {};
131
+ for (const [k, v] of Object.entries(row)) {
132
+ const def = schema[k];
133
+ let t = null;
134
+ if (typeof def === 'string') t = def.toLowerCase();
135
+ else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
136
+ if (v !== null && v !== undefined && ['json', 'array', 'object'].includes(t) && typeof v !== 'string') {
137
+ out[k] = safeStringify(v, table, k);
138
+ } else if (v !== null && v !== undefined && isIntegerType(t)) {
139
+ out[k] = normalizeIntegerInput(v, t);
140
+ } else if (v instanceof Date) {
141
+ out[k] = v.toISOString();
142
+ } else {
143
+ out[k] = v;
144
+ }
145
+ }
146
+ return out;
147
+ }
148
+
80
149
  const NATIVE_TYPE_MAP = {
81
150
  text: 'string',
82
151
  varchar: 'string',
@@ -91,7 +160,7 @@ const NATIVE_TYPE_MAP = {
91
160
  array: 'string',
92
161
  object: 'string',
93
162
  bool: 'boolean',
94
- bigint: 'string',
163
+ bigint: 'bigint',
95
164
  int: 'integer',
96
165
  };
97
166
 
@@ -182,9 +251,9 @@ function restoreRow(row, schema) {
182
251
  if (typeof colDef === 'string') t = colDef.toLowerCase();
183
252
  else if (colDef && typeof colDef === 'object' && colDef.type) t = String(colDef.type).toLowerCase();
184
253
  if (['json', 'array', 'object'].includes(t) && typeof value === 'string') {
185
- try { out[field] = JSON.parse(value); } catch (e) { out[field] = value; }
186
- } else if ((t === 'integer' || t === 'int' || t === 'bigint') && typeof value === 'string' && /^-?\d+$/.test(value)) {
187
- out[field] = Number(value);
254
+ try { out[field] = bigintJson.parse(value); } catch (e) { out[field] = value; }
255
+ } else if (isIntegerType(t)) {
256
+ out[field] = restoreInteger(value);
188
257
  } else if ((t === 'float' || t === 'double' || t === 'number') && typeof value === 'string' && !Number.isNaN(Number(value))) {
189
258
  out[field] = Number(value);
190
259
  } else {
@@ -450,7 +519,9 @@ class JSQL {
450
519
  let t = null;
451
520
  if (typeof def === 'string') t = def.toLowerCase();
452
521
  else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
453
- if (['integer', 'int', 'bigint'].includes(t)) return Number(id);
522
+ if (isIntegerType(t)) {
523
+ try { return normalizeIntegerInput(id, t); } catch (_) { return id; }
524
+ }
454
525
  return id;
455
526
  }
456
527
 
@@ -462,7 +533,7 @@ class JSQL {
462
533
  if (Number.isFinite(nid) && nid < 0) throw new Error('id must be a non-negative number');
463
534
  const filter = {};
464
535
  filter[pk] = this._coercePkId(table, id);
465
- const r = safeJsonParse(wasmBindings.jsql_find(table, JSON.stringify(filter), 1, 0));
536
+ const r = safeJsonParse(wasmBindings.jsql_find(table, bigintJson.stringify(filter), 1, 0));
466
537
  if (r && r.error) return r;
467
538
  return Array.isArray(r) && r.length > 0 ? r[0] : null;
468
539
  }
@@ -479,7 +550,7 @@ class JSQL {
479
550
  if (pk) {
480
551
  const filter = {};
481
552
  filter[pk] = this._coercePkId(table, id);
482
- const r = safeJsonParse(wasmBindings.jsql_find(table, JSON.stringify(filter), 1, 0));
553
+ const r = safeJsonParse(wasmBindings.jsql_find(table, bigintJson.stringify(filter), 1, 0));
483
554
  if (Array.isArray(r) && r.length > 0) return r[0].id;
484
555
  return null;
485
556
  }
@@ -504,25 +575,9 @@ class JSQL {
504
575
  // schema-aware 转换:JSON 列用 safeStringify 给出可读错误(环形对象等)
505
576
  const schema = this._schemas[table];
506
577
  if (schema) {
507
- rows = rows.map(row => {
508
- const out = {};
509
- for (const [k, v] of Object.entries(row)) {
510
- const def = schema[k];
511
- let t = null;
512
- if (typeof def === 'string') t = def.toLowerCase();
513
- else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
514
- if (v !== null && v !== undefined && ['json', 'array', 'object'].includes(t) && typeof v !== 'string') {
515
- out[k] = safeStringify(v, table, k);
516
- } else if (v instanceof Date) {
517
- out[k] = v.toISOString();
518
- } else {
519
- out[k] = v;
520
- }
521
- }
522
- return out;
523
- });
578
+ rows = rows.map(row => normalizeRowForRust(table, schema, row));
524
579
  }
525
- const rawJson = JSON.stringify(rows);
580
+ const rawJson = bigintJson.stringify(rows);
526
581
  const pk = this._pkOf(table);
527
582
  const isAutoPk = this._pkAutoIncrement(table);
528
583
  // #36:旧 WASM 二进制对自增主键表按「保留区间 [next_id, next_id+n)」批量分配 id,
@@ -547,7 +602,7 @@ class JSQL {
547
602
  throw new Error(`ER_DUP_ENTRY: Duplicate entry '${explicitPk}' for key '${pk}'`);
548
603
  }
549
604
  }
550
- const r = safeJsonParse(wasmBindings.jsql_insert_json(table, JSON.stringify([row])));
605
+ const r = safeJsonParse(wasmBindings.jsql_insert_json(table, bigintJson.stringify([row])));
551
606
  if (r && r.error) throw new Error(r.error);
552
607
  ids.push(explicitPk !== null ? explicitPk : (Array.isArray(r) ? r[0] : undefined));
553
608
  }
@@ -699,7 +754,7 @@ class JSQL {
699
754
  async find(table, filter, opts = {}) {
700
755
  await this._flush();
701
756
  if (!(await this._runHooks('beforeFind', [table, { filter, opts }]))) return [];
702
- const filterStr = filter ? JSON.stringify(filter) : '';
757
+ const filterStr = filter ? bigintJson.stringify(filter) : '';
703
758
  const { limit = 100, offset = 0 } = opts;
704
759
  // WASM 端 jsql_find 的 limit/offset 是 usize(无符号),负数会经 wasm-bindgen
705
760
  // 静默回绕成巨大值,导致空结果或溢出。#35:绑定层先校验(同 native_client.js)。
@@ -723,14 +778,15 @@ class JSQL {
723
778
  }
724
779
 
725
780
  async updateByIds(table, entries) {
781
+ const schema = this._schemas[table];
726
782
  const pairs = [];
727
783
  for (const [id, data] of entries) {
728
784
  const seq = this._seqIdByPk(table, id);
729
- if (seq !== null) pairs.push([seq, data]);
785
+ if (seq !== null) pairs.push([seq, schema ? normalizeRowForRust(table, schema, data) : data]);
730
786
  }
731
787
  if (!(await this._runHooks('beforeUpdate', [table, pairs]))) return;
732
788
  await this._flush();
733
- const r = safeJsonParse(wasmBindings.jsql_update_by_ids(table, JSON.stringify(pairs)));
789
+ const r = safeJsonParse(wasmBindings.jsql_update_by_ids(table, bigintJson.stringify(pairs)));
734
790
  if (r && r.error) throw new Error(r.error);
735
791
  this._emit('update', { table, entries: pairs, result: r });
736
792
  await this._runHooks('afterUpdate', [table, pairs, r]);
@@ -779,7 +835,9 @@ class JSQL {
779
835
  // 静默产生两行同主键 —— 主键索引只留一个,索引与实际行数组不一致。
780
836
  // MySQL / SQLite 都会报 UNIQUE constraint failed。
781
837
  await this._assertNoKeyConflict(table, id, data, seq);
782
- const r = safeJsonParse(wasmBindings.jsql_update_by_id(table, BigInt(seq), JSON.stringify(data)));
838
+ const schema = this._schemas[table];
839
+ const payload = schema ? normalizeRowForRust(table, schema, data) : data;
840
+ const r = safeJsonParse(wasmBindings.jsql_update_by_id(table, BigInt(seq), bigintJson.stringify(payload)));
783
841
  if (r && r.ok === false) throw new Error(r.error || 'update failed');
784
842
  this._emit('update', { table, id, data });
785
843
  await this._runHooks('afterUpdate', [table, id, data, r]);
Binary file
@@ -109,7 +109,7 @@ impl Engine for MemoryEngine {
109
109
 
110
110
  fn update_by_id(&mut self, table: &str, id: u64, data: HashMap<String, serde_json::Value>) -> Result<bool, String> {
111
111
  let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
112
- Ok(t.update_row(id, data))
112
+ t.update_row(id, data)
113
113
  }
114
114
 
115
115
  fn remove_by_id(&mut self, table: &str, id: u64) -> Result<bool, String> {
@@ -124,7 +124,7 @@ impl Engine for MemoryEngine {
124
124
 
125
125
  fn update_by_ids(&mut self, table: &str, batch: Vec<(u64, HashMap<String, serde_json::Value>)>) -> Result<usize, String> {
126
126
  let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
127
- Ok(t.update_rows(batch))
127
+ t.update_rows(batch)
128
128
  }
129
129
 
130
130
  fn update_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
@@ -147,7 +147,7 @@ impl Engine for MemoryEngine {
147
147
  };
148
148
  let count = ids.len();
149
149
  for id in &ids {
150
- t.update_row_ref(*id, &data);
150
+ t.update_row_ref(*id, &data)?;
151
151
  }
152
152
  Ok(count)
153
153
  }