jsql-neo 3.4.5 → 3.4.7

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.
@@ -5,9 +5,102 @@ function safeJsonParse(str) {
5
5
  try { return JSON.parse(str); } catch { return str; }
6
6
  }
7
7
 
8
+ const textEncoder = new TextEncoder();
9
+
10
+ function utf8Len(s) {
11
+ let len = 0;
12
+ for (let i = 0; i < s.length; i++) {
13
+ const c = s.charCodeAt(i);
14
+ if (c < 0x80) len++;
15
+ else if (c < 0x800) len += 2;
16
+ else if (c < 0xD800 || c > 0xDFFF) len += 3;
17
+ else { len += 4; i++; }
18
+ }
19
+ return len;
20
+ }
21
+
22
+ function writeUtf8(buf, off, s) {
23
+ for (let i = 0; i < s.length; i++) {
24
+ const c = s.charCodeAt(i);
25
+ if (c < 0x80) { buf[off++] = c; }
26
+ else if (c < 0x800) { buf[off++] = 0xC0 | (c >> 6); buf[off++] = 0x80 | (c & 0x3F); }
27
+ else if (c < 0xD800 || c > 0xDFFF) { buf[off++] = 0xE0 | (c >> 12); buf[off++] = 0x80 | ((c >> 6) & 0x3F); buf[off++] = 0x80 | (c & 0x3F); }
28
+ else { const cp = 0x10000 + ((c & 0x3FF) << 10) + (s.charCodeAt(++i) & 0x3FF); buf[off++] = 0xF0 | (cp >> 18); buf[off++] = 0x80 | ((cp >> 12) & 0x3F); buf[off++] = 0x80 | ((cp >> 6) & 0x3F); buf[off++] = 0x80 | (cp & 0x3F); }
29
+ }
30
+ return off;
31
+ }
32
+
33
+ function encodeBatch(rows) {
34
+ if (rows.length === 0) return new Uint8Array(0);
35
+ const fieldNames = Object.keys(rows[0]);
36
+ const nFields = fieldNames.length;
37
+
38
+ // First pass: compute exact size, no allocations
39
+ let size = 1;
40
+ for (let fi = 0; fi < nFields; fi++) size += 1 + utf8Len(fieldNames[fi]);
41
+ size += 4;
42
+
43
+ for (let ri = 0; ri < rows.length; ri++) {
44
+ const row = rows[ri];
45
+ for (let fi = 0; fi < nFields; fi++) {
46
+ const val = row[fieldNames[fi]];
47
+ if (val === null || val === undefined) size += 1;
48
+ else if (typeof val === 'number') size += 9;
49
+ else if (typeof val === 'string') size += 1 + 4 + utf8Len(val);
50
+ else size += 2;
51
+ }
52
+ }
53
+
54
+ // Second pass: write directly to pre-allocated buffer
55
+ const buf = new Uint8Array(size);
56
+ const dv = new DataView(buf.buffer, 0, size);
57
+ let off = 0;
58
+
59
+ buf[off++] = nFields;
60
+ for (let fi = 0; fi < nFields; fi++) {
61
+ const s = fieldNames[fi];
62
+ buf[off++] = utf8Len(s);
63
+ off = writeUtf8(buf, off, s);
64
+ }
65
+
66
+ dv.setUint32(off, rows.length, true);
67
+ off += 4;
68
+
69
+ for (let ri = 0; ri < rows.length; ri++) {
70
+ const row = rows[ri];
71
+ for (let fi = 0; fi < nFields; fi++) {
72
+ const val = row[fieldNames[fi]];
73
+ if (val === null || val === undefined) {
74
+ buf[off++] = 0;
75
+ } else if (typeof val === 'number') {
76
+ if (Number.isInteger(val)) {
77
+ buf[off++] = 1;
78
+ dv.setBigInt64(off, BigInt(val), true);
79
+ off += 8;
80
+ } else {
81
+ buf[off++] = 2;
82
+ dv.setFloat64(off, val, true);
83
+ off += 8;
84
+ }
85
+ } else if (typeof val === 'string') {
86
+ buf[off++] = 3;
87
+ const strLen = utf8Len(val);
88
+ dv.setUint32(off, strLen, true);
89
+ off += 4;
90
+ off = writeUtf8(buf, off, val);
91
+ } else {
92
+ buf[off++] = 4;
93
+ buf[off++] = val ? 1 : 0;
94
+ }
95
+ }
96
+ }
97
+
98
+ return buf;
99
+ }
100
+
8
101
  class JSQL {
9
102
  constructor(opts = {}) {
10
- this._flushThreshold = opts.flushThreshold || 2000;
103
+ this._flushThreshold = opts.flushThreshold || 5000;
11
104
  this._buffer = {};
12
105
  this._bufferSize = 0;
13
106
  }
@@ -15,7 +108,8 @@ class JSQL {
15
108
  async start() {}
16
109
 
17
110
  async _insertBatch(table, rows) {
18
- const r = safeJsonParse(wasmBindings.jsql_insert(table, JSON.stringify(rows)));
111
+ const bin = encodeBatch(rows);
112
+ const r = safeJsonParse(wasmBindings.jsql_insert_buf(table, bin));
19
113
  if (r && r.error) throw new Error(r.error);
20
114
  return r;
21
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "3.4.5",
3
+ "version": "3.4.7",
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
  "files": [
@@ -15,6 +15,8 @@ export function jsql_insert(table: string, data_json: string): string;
15
15
 
16
16
  export function jsql_insert_buf(table: string, data: Uint8Array): string;
17
17
 
18
+ export function jsql_insert_buf_count(table: string, data: Uint8Array): string;
19
+
18
20
  export function jsql_remove_by_id(table: string, id: bigint): string;
19
21
 
20
22
  export function jsql_update_by_id(table: string, id: bigint, data_json: string): string;
@@ -155,6 +155,29 @@ function jsql_insert_buf(table, data) {
155
155
  }
156
156
  exports.jsql_insert_buf = jsql_insert_buf;
157
157
 
158
+ /**
159
+ * @param {string} table
160
+ * @param {Uint8Array} data
161
+ * @returns {string}
162
+ */
163
+ function jsql_insert_buf_count(table, data) {
164
+ let deferred3_0;
165
+ let deferred3_1;
166
+ try {
167
+ const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
168
+ const len0 = WASM_VECTOR_LEN;
169
+ const ptr1 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
170
+ const len1 = WASM_VECTOR_LEN;
171
+ const ret = wasm.jsql_insert_buf_count(ptr0, len0, ptr1, len1);
172
+ deferred3_0 = ret[0];
173
+ deferred3_1 = ret[1];
174
+ return getStringFromWasm0(ret[0], ret[1]);
175
+ } finally {
176
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
177
+ }
178
+ }
179
+ exports.jsql_insert_buf_count = jsql_insert_buf_count;
180
+
158
181
  /**
159
182
  * @param {string} table
160
183
  * @param {bigint} id
Binary file
@@ -8,6 +8,7 @@ export const jsql_find: (a: number, b: number, c: number, d: number, e: number,
8
8
  export const jsql_find_by_id: (a: number, b: number, c: bigint) => [number, number];
9
9
  export const jsql_insert: (a: number, b: number, c: number, d: number) => [number, number];
10
10
  export const jsql_insert_buf: (a: number, b: number, c: number, d: number) => [number, number];
11
+ export const jsql_insert_buf_count: (a: number, b: number, c: number, d: number) => [number, number];
11
12
  export const jsql_remove_by_id: (a: number, b: number, c: bigint) => [number, number];
12
13
  export const jsql_update_by_id: (a: number, b: number, c: bigint, d: number, e: number) => [number, number];
13
14
  export const __wbindgen_malloc: (a: number, b: number) => number;