jsql-neo 3.3.0 → 3.4.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.
@@ -1,5 +1,4 @@
1
1
  const path = require('path');
2
-
3
2
  const wasmBindings = require(path.join(__dirname, '..', 'wasm', 'jsql_neo_wasm.js'));
4
3
 
5
4
  function safeJsonParse(str) {
@@ -8,14 +7,14 @@ function safeJsonParse(str) {
8
7
 
9
8
  class JSQL {
10
9
  constructor(opts = {}) {
11
- this._flushThreshold = opts.flushThreshold || 1000;
10
+ this._flushThreshold = opts.flushThreshold || 2000;
12
11
  this._buffer = {};
13
12
  this._bufferSize = 0;
14
13
  }
15
14
 
16
15
  async start() {}
17
16
 
18
- async _flushTable(table, rows) {
17
+ async _insertBatch(table, rows) {
19
18
  const r = safeJsonParse(wasmBindings.jsql_insert(table, JSON.stringify(rows)));
20
19
  if (r && r.error) throw new Error(r.error);
21
20
  return r;
@@ -24,25 +23,32 @@ class JSQL {
24
23
  async _flush() {
25
24
  for (const [table, rows] of Object.entries(this._buffer)) {
26
25
  if (rows.length === 0) continue;
27
- await this._flushTable(table, rows);
26
+ await this._insertBatch(table, rows);
28
27
  }
29
28
  this._buffer = {};
30
29
  this._bufferSize = 0;
31
30
  }
32
31
 
33
- _enqueue(table, data) {
34
- if (!this._buffer[table]) this._buffer[table] = [];
35
- const arr = Array.isArray(data) ? data : [data];
36
- this._buffer[table].push(...arr);
37
- this._bufferSize += arr.length;
38
- }
39
-
40
32
  async flush() {
41
33
  await this._flush();
42
34
  }
43
35
 
44
36
  async insert(table, data) {
45
- this._enqueue(table, data);
37
+ const arr = Array.isArray(data) ? data : [data];
38
+ if (arr.length > 1) {
39
+ await this._flush();
40
+ let result;
41
+ for (let i = 0; i < arr.length; i += this._flushThreshold) {
42
+ const chunk = arr.slice(i, i + this._flushThreshold);
43
+ const r = await this._insertBatch(table, chunk);
44
+ if (r && r.error) throw new Error(r.error);
45
+ if (!result) result = r;
46
+ }
47
+ return result;
48
+ }
49
+ if (!this._buffer[table]) this._buffer[table] = [];
50
+ this._buffer[table].push(arr[0]);
51
+ this._bufferSize++;
46
52
  if (this._bufferSize >= this._flushThreshold) {
47
53
  await this._flush();
48
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
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": [
@@ -1,304 +0,0 @@
1
- /* @ts-self-types="./jsql_neo_wasm.d.ts" */
2
-
3
- /**
4
- * @param {string} table
5
- * @returns {string}
6
- */
7
- function jsql_count(table) {
8
- let deferred2_0;
9
- let deferred2_1;
10
- try {
11
- const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
12
- const len0 = WASM_VECTOR_LEN;
13
- const ret = wasm.jsql_count(ptr0, len0);
14
- deferred2_0 = ret[0];
15
- deferred2_1 = ret[1];
16
- return getStringFromWasm0(ret[0], ret[1]);
17
- } finally {
18
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
19
- }
20
- }
21
- exports.jsql_count = jsql_count;
22
-
23
- /**
24
- * @param {string} name
25
- * @param {string} schema_json
26
- * @returns {string}
27
- */
28
- function jsql_create_table(name, schema_json) {
29
- let deferred3_0;
30
- let deferred3_1;
31
- try {
32
- const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
33
- const len0 = WASM_VECTOR_LEN;
34
- const ptr1 = passStringToWasm0(schema_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
35
- const len1 = WASM_VECTOR_LEN;
36
- const ret = wasm.jsql_create_table(ptr0, len0, ptr1, len1);
37
- deferred3_0 = ret[0];
38
- deferred3_1 = ret[1];
39
- return getStringFromWasm0(ret[0], ret[1]);
40
- } finally {
41
- wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
42
- }
43
- }
44
- exports.jsql_create_table = jsql_create_table;
45
-
46
- /**
47
- * @param {string} name
48
- * @returns {string}
49
- */
50
- function jsql_drop_table(name) {
51
- let deferred2_0;
52
- let deferred2_1;
53
- try {
54
- const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
55
- const len0 = WASM_VECTOR_LEN;
56
- const ret = wasm.jsql_drop_table(ptr0, len0);
57
- deferred2_0 = ret[0];
58
- deferred2_1 = ret[1];
59
- return getStringFromWasm0(ret[0], ret[1]);
60
- } finally {
61
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
62
- }
63
- }
64
- exports.jsql_drop_table = jsql_drop_table;
65
-
66
- /**
67
- * @param {string} table
68
- * @param {string} filter_json
69
- * @param {number} limit
70
- * @param {number} offset
71
- * @returns {string}
72
- */
73
- function jsql_find(table, filter_json, limit, offset) {
74
- let deferred3_0;
75
- let deferred3_1;
76
- try {
77
- const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
78
- const len0 = WASM_VECTOR_LEN;
79
- const ptr1 = passStringToWasm0(filter_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
80
- const len1 = WASM_VECTOR_LEN;
81
- const ret = wasm.jsql_find(ptr0, len0, ptr1, len1, limit, offset);
82
- deferred3_0 = ret[0];
83
- deferred3_1 = ret[1];
84
- return getStringFromWasm0(ret[0], ret[1]);
85
- } finally {
86
- wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
87
- }
88
- }
89
- exports.jsql_find = jsql_find;
90
-
91
- /**
92
- * @param {string} table
93
- * @param {bigint} id
94
- * @returns {string}
95
- */
96
- function jsql_find_by_id(table, id) {
97
- let deferred2_0;
98
- let deferred2_1;
99
- try {
100
- const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
101
- const len0 = WASM_VECTOR_LEN;
102
- const ret = wasm.jsql_find_by_id(ptr0, len0, id);
103
- deferred2_0 = ret[0];
104
- deferred2_1 = ret[1];
105
- return getStringFromWasm0(ret[0], ret[1]);
106
- } finally {
107
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
108
- }
109
- }
110
- exports.jsql_find_by_id = jsql_find_by_id;
111
-
112
- /**
113
- * @param {string} table
114
- * @param {string} data_json
115
- * @returns {string}
116
- */
117
- function jsql_insert(table, data_json) {
118
- let deferred3_0;
119
- let deferred3_1;
120
- try {
121
- const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
122
- const len0 = WASM_VECTOR_LEN;
123
- const ptr1 = passStringToWasm0(data_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
124
- const len1 = WASM_VECTOR_LEN;
125
- const ret = wasm.jsql_insert(ptr0, len0, ptr1, len1);
126
- deferred3_0 = ret[0];
127
- deferred3_1 = ret[1];
128
- return getStringFromWasm0(ret[0], ret[1]);
129
- } finally {
130
- wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
131
- }
132
- }
133
- exports.jsql_insert = jsql_insert;
134
-
135
- /**
136
- * @param {string} table
137
- * @param {bigint} id
138
- * @returns {string}
139
- */
140
- function jsql_remove_by_id(table, id) {
141
- let deferred2_0;
142
- let deferred2_1;
143
- try {
144
- const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
145
- const len0 = WASM_VECTOR_LEN;
146
- const ret = wasm.jsql_remove_by_id(ptr0, len0, id);
147
- deferred2_0 = ret[0];
148
- deferred2_1 = ret[1];
149
- return getStringFromWasm0(ret[0], ret[1]);
150
- } finally {
151
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
152
- }
153
- }
154
- exports.jsql_remove_by_id = jsql_remove_by_id;
155
-
156
- /**
157
- * @param {string} table
158
- * @param {bigint} id
159
- * @param {string} data_json
160
- * @returns {string}
161
- */
162
- function jsql_update_by_id(table, id, data_json) {
163
- let deferred3_0;
164
- let deferred3_1;
165
- try {
166
- const ptr0 = passStringToWasm0(table, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
167
- const len0 = WASM_VECTOR_LEN;
168
- const ptr1 = passStringToWasm0(data_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
169
- const len1 = WASM_VECTOR_LEN;
170
- const ret = wasm.jsql_update_by_id(ptr0, len0, id, ptr1, len1);
171
- deferred3_0 = ret[0];
172
- deferred3_1 = ret[1];
173
- return getStringFromWasm0(ret[0], ret[1]);
174
- } finally {
175
- wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
176
- }
177
- }
178
- exports.jsql_update_by_id = jsql_update_by_id;
179
- function __wbg_get_imports() {
180
- const import0 = {
181
- __proto__: null,
182
- __wbg___wbindgen_string_get_b0ca35b86a603356: function(arg0, arg1) {
183
- const obj = arg1;
184
- const ret = typeof(obj) === 'string' ? obj : undefined;
185
- var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
186
- var len1 = WASM_VECTOR_LEN;
187
- getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
188
- getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
189
- },
190
- __wbg___wbindgen_throw_344f42d3211c4765: function(arg0, arg1) {
191
- throw new Error(getStringFromWasm0(arg0, arg1));
192
- },
193
- __wbg_new_0_3da9e97f24fc69be: function() {
194
- const ret = new Date();
195
- return ret;
196
- },
197
- __wbg_toISOString_706fbe321055ee58: function(arg0) {
198
- const ret = arg0.toISOString();
199
- return ret;
200
- },
201
- __wbindgen_init_externref_table: function() {
202
- const table = wasm.__wbindgen_externrefs;
203
- const offset = table.grow(4);
204
- table.set(0, undefined);
205
- table.set(offset + 0, undefined);
206
- table.set(offset + 1, null);
207
- table.set(offset + 2, true);
208
- table.set(offset + 3, false);
209
- },
210
- };
211
- return {
212
- __proto__: null,
213
- "./jsql_neo_wasm_bg.js": import0,
214
- };
215
- }
216
-
217
- let cachedDataViewMemory0 = null;
218
- function getDataViewMemory0() {
219
- if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
220
- cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
221
- }
222
- return cachedDataViewMemory0;
223
- }
224
-
225
- function getStringFromWasm0(ptr, len) {
226
- return decodeText(ptr >>> 0, len);
227
- }
228
-
229
- let cachedUint8ArrayMemory0 = null;
230
- function getUint8ArrayMemory0() {
231
- if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
232
- cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
233
- }
234
- return cachedUint8ArrayMemory0;
235
- }
236
-
237
- function isLikeNone(x) {
238
- return x === undefined || x === null;
239
- }
240
-
241
- function passStringToWasm0(arg, malloc, realloc) {
242
- if (realloc === undefined) {
243
- const buf = cachedTextEncoder.encode(arg);
244
- const ptr = malloc(buf.length, 1) >>> 0;
245
- getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
246
- WASM_VECTOR_LEN = buf.length;
247
- return ptr;
248
- }
249
-
250
- let len = arg.length;
251
- let ptr = malloc(len, 1) >>> 0;
252
-
253
- const mem = getUint8ArrayMemory0();
254
-
255
- let offset = 0;
256
-
257
- for (; offset < len; offset++) {
258
- const code = arg.charCodeAt(offset);
259
- if (code > 0x7F) break;
260
- mem[ptr + offset] = code;
261
- }
262
- if (offset !== len) {
263
- if (offset !== 0) {
264
- arg = arg.slice(offset);
265
- }
266
- ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
267
- const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
268
- const ret = cachedTextEncoder.encodeInto(arg, view);
269
-
270
- offset += ret.written;
271
- ptr = realloc(ptr, len, offset, 1) >>> 0;
272
- }
273
-
274
- WASM_VECTOR_LEN = offset;
275
- return ptr;
276
- }
277
-
278
- let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
279
- cachedTextDecoder.decode();
280
- function decodeText(ptr, len) {
281
- return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
282
- }
283
-
284
- const cachedTextEncoder = new TextEncoder();
285
-
286
- if (!('encodeInto' in cachedTextEncoder)) {
287
- cachedTextEncoder.encodeInto = function (arg, view) {
288
- const buf = cachedTextEncoder.encode(arg);
289
- view.set(buf);
290
- return {
291
- read: arg.length,
292
- written: buf.length
293
- };
294
- };
295
- }
296
-
297
- let WASM_VECTOR_LEN = 0;
298
-
299
- const wasmPath = `${__dirname}/jsql_neo_wasm_bg.wasm`;
300
- const wasmBytes = require('fs').readFileSync(wasmPath);
301
- const wasmModule = new WebAssembly.Module(wasmBytes);
302
- let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());
303
- let wasm = wasmInstance.exports;
304
- wasm.__wbindgen_start();
Binary file