jsql-neo 3.2.0 → 3.3.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/lib/wasm_client.js +49 -11
- package/package.json +1 -1
package/lib/wasm_client.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
|
|
3
|
-
// wasm-pack generated loader — initializes WASM on first require
|
|
4
3
|
const wasmBindings = require(path.join(__dirname, '..', 'wasm', 'jsql_neo_wasm.js'));
|
|
5
4
|
|
|
6
5
|
function safeJsonParse(str) {
|
|
@@ -8,36 +7,70 @@ function safeJsonParse(str) {
|
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
class JSQL {
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
constructor(opts = {}) {
|
|
11
|
+
this._flushThreshold = opts.flushThreshold || 1000;
|
|
12
|
+
this._buffer = {};
|
|
13
|
+
this._bufferSize = 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async start() {}
|
|
17
|
+
|
|
18
|
+
async _flushTable(table, rows) {
|
|
19
|
+
const r = safeJsonParse(wasmBindings.jsql_insert(table, JSON.stringify(rows)));
|
|
20
|
+
if (r && r.error) throw new Error(r.error);
|
|
21
|
+
return r;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async _flush() {
|
|
25
|
+
for (const [table, rows] of Object.entries(this._buffer)) {
|
|
26
|
+
if (rows.length === 0) continue;
|
|
27
|
+
await this._flushTable(table, rows);
|
|
28
|
+
}
|
|
29
|
+
this._buffer = {};
|
|
30
|
+
this._bufferSize = 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
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
|
+
async flush() {
|
|
41
|
+
await this._flush();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async insert(table, data) {
|
|
45
|
+
this._enqueue(table, data);
|
|
46
|
+
if (this._bufferSize >= this._flushThreshold) {
|
|
47
|
+
await this._flush();
|
|
48
|
+
}
|
|
13
49
|
}
|
|
14
50
|
|
|
15
51
|
async createTable(name, schema) {
|
|
52
|
+
await this._flush();
|
|
16
53
|
const r = safeJsonParse(wasmBindings.jsql_create_table(name, JSON.stringify(schema)));
|
|
17
54
|
if (r && r.ok === false) throw new Error(r.error || 'create table failed');
|
|
18
55
|
return r;
|
|
19
56
|
}
|
|
20
57
|
|
|
21
58
|
async dropTable(name) {
|
|
59
|
+
await this._flush();
|
|
22
60
|
const r = safeJsonParse(wasmBindings.jsql_drop_table(name));
|
|
23
61
|
if (r && r.ok === false) throw new Error(r.error || 'drop table failed');
|
|
24
62
|
return r;
|
|
25
63
|
}
|
|
26
64
|
|
|
27
|
-
async insert(table, data) {
|
|
28
|
-
const arr = Array.isArray(data) ? data : [data];
|
|
29
|
-
const r = safeJsonParse(wasmBindings.jsql_insert(table, JSON.stringify(arr)));
|
|
30
|
-
if (r && r.error) throw new Error(r.error);
|
|
31
|
-
return r;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
65
|
async findById(table, id) {
|
|
66
|
+
await this._flush();
|
|
35
67
|
const r = safeJsonParse(wasmBindings.jsql_find_by_id(table, BigInt(id)));
|
|
36
68
|
if (r && r.error) throw new Error(r.error);
|
|
37
69
|
return r;
|
|
38
70
|
}
|
|
39
71
|
|
|
40
72
|
async find(table, filter, opts = {}) {
|
|
73
|
+
await this._flush();
|
|
41
74
|
const filterStr = filter ? JSON.stringify(filter) : '';
|
|
42
75
|
const { limit = 100, offset = 0 } = opts;
|
|
43
76
|
const r = safeJsonParse(wasmBindings.jsql_find(table, filterStr, limit, offset));
|
|
@@ -46,23 +79,28 @@ class JSQL {
|
|
|
46
79
|
}
|
|
47
80
|
|
|
48
81
|
async count(table) {
|
|
82
|
+
await this._flush();
|
|
49
83
|
const r = parseInt(wasmBindings.jsql_count(table), 10);
|
|
50
84
|
return isNaN(r) ? 0 : r;
|
|
51
85
|
}
|
|
52
86
|
|
|
53
87
|
async updateById(table, id, data) {
|
|
88
|
+
await this._flush();
|
|
54
89
|
const r = safeJsonParse(wasmBindings.jsql_update_by_id(table, BigInt(id), JSON.stringify(data)));
|
|
55
90
|
if (r && r.ok === false) throw new Error(r.error || 'update failed');
|
|
56
91
|
return r;
|
|
57
92
|
}
|
|
58
93
|
|
|
59
94
|
async removeById(table, id) {
|
|
95
|
+
await this._flush();
|
|
60
96
|
const r = safeJsonParse(wasmBindings.jsql_remove_by_id(table, BigInt(id)));
|
|
61
97
|
if (r && r.ok === false) throw new Error(r.error || 'remove failed');
|
|
62
98
|
return r;
|
|
63
99
|
}
|
|
64
100
|
|
|
65
|
-
async stop() {
|
|
101
|
+
async stop() {
|
|
102
|
+
await this._flush();
|
|
103
|
+
}
|
|
66
104
|
}
|
|
67
105
|
|
|
68
106
|
module.exports = { JSQL };
|