jsql-neo 5.4.2 → 5.5.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.
Files changed (72) hide show
  1. package/LICENSE +202 -202
  2. package/README.md +8097 -7971
  3. package/bin/jsql +320 -320
  4. package/bin/jsql-server +90 -90
  5. package/bin/jsql-server-control.js +177 -177
  6. package/index.d.ts +385 -357
  7. package/index.js +124 -118
  8. package/lib/ast.js +214 -0
  9. package/lib/btree.js +413 -413
  10. package/lib/cache.js +328 -328
  11. package/lib/client.js +125 -125
  12. package/lib/database.js +1744 -1744
  13. package/lib/date-types.js +182 -182
  14. package/lib/errors.js +89 -89
  15. package/lib/jsql_format.js +497 -497
  16. package/lib/migrate.js +254 -254
  17. package/lib/mod.js +292 -292
  18. package/lib/mongo_server.js +607 -607
  19. package/lib/multiserver.js +140 -140
  20. package/lib/mysql_compat.js +413 -413
  21. package/lib/mysql_server.js +1237 -1237
  22. package/lib/native_client.js +732 -732
  23. package/lib/nedb_compat.js +506 -506
  24. package/lib/pg_server.js +864 -864
  25. package/lib/plugin.js +34 -34
  26. package/lib/query.js +815 -815
  27. package/lib/redis_server.js +571 -571
  28. package/lib/sql.js +3460 -2925
  29. package/lib/sqlite_compat.js +323 -323
  30. package/lib/sqlite_worker.js +264 -264
  31. package/lib/table.js +1304 -1304
  32. package/lib/tui.js +502 -502
  33. package/lib/wasm.js +1 -1
  34. package/lib/wasm_client.js +718 -718
  35. package/lib/web_ui.js +257 -257
  36. package/native/jsql-neo-native.node +0 -0
  37. package/nativesrc/jsql-neo-core/Cargo.lock +455 -455
  38. package/nativesrc/jsql-neo-core/Cargo.toml +24 -24
  39. package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +455 -455
  40. package/nativesrc/jsql-neo-core/src/engine/memory.rs +168 -168
  41. package/nativesrc/jsql-neo-core/src/engine/mod.rs +62 -62
  42. package/nativesrc/jsql-neo-core/src/engine/table.rs +700 -700
  43. package/nativesrc/jsql-neo-core/src/lib.rs +2 -2
  44. package/nativesrc/jsql-neo-core/src/storage/mod.rs +2 -2
  45. package/nativesrc/jsql-neo-core/src/storage/persistent.rs +1 -1
  46. package/nativesrc/jsql-neo-core/src/storage/wal.rs +85 -85
  47. package/nativesrc/jsql-neo-core/src/types.rs +93 -93
  48. package/nativesrc/jsql-neo-native/Cargo.lock +606 -606
  49. package/nativesrc/jsql-neo-native/Cargo.toml +16 -16
  50. package/nativesrc/jsql-neo-native/build.rs +3 -3
  51. package/nativesrc/jsql-neo-native/package.json +7 -7
  52. package/nativesrc/jsql-neo-native/src/lib.rs +269 -269
  53. package/nativesrc/jsql-neo-wasm/Cargo.lock +465 -465
  54. package/nativesrc/jsql-neo-wasm/Cargo.toml +15 -15
  55. package/nativesrc/jsql-neo-wasm/src/lib.rs +447 -447
  56. package/package.json +87 -86
  57. package/postinstall.js +158 -158
  58. package/test/btree.test.js +128 -128
  59. package/test/coverage.js +187 -187
  60. package/test/join.test.js +110 -110
  61. package/test/native.test.js +158 -158
  62. package/test/regress-5.1.0.js +271 -271
  63. package/test/smoke.js +58 -58
  64. package/test/sql-parser.test.js +273 -0
  65. package/test/wasm.test.js +110 -110
  66. package/wasm/browser.d.ts +89 -89
  67. package/wasm/browser.mjs +569 -569
  68. package/wasm/browser_bg.mjs +597 -597
  69. package/wasm/jsql_neo_wasm.d.ts +38 -38
  70. package/wasm/jsql_neo_wasm.js +520 -520
  71. package/wasm/jsql_neo_wasm_bg.wasm.d.ts +26 -26
  72. package/wasm/package.json +11 -11
@@ -1,447 +1,447 @@
1
- use jsql_neo_core::engine::{Engine, MemoryEngine, FieldValue, RowStore, default_value};
2
- use jsql_neo_core::types::{schema_order_preserving, FieldSchema};
3
- use std::cell::RefCell;
4
- use wasm_bindgen::prelude::*;
5
-
6
- thread_local! {
7
- static ENGINE: RefCell<MemoryEngine> = RefCell::new(MemoryEngine::new());
8
- }
9
-
10
- #[wasm_bindgen]
11
- pub fn jsql_reset() {
12
- ENGINE.with(|eng| {
13
- eng.borrow_mut().clear();
14
- });
15
- }
16
-
17
- #[wasm_bindgen]
18
- pub fn jsql_create_table(name: &str, schema_json: &str) -> String {
19
- ENGINE.with(|eng| {
20
- let schema: Vec<(String, FieldSchema)> = match schema_order_preserving::deserialize(
21
- &mut serde_json::Deserializer::from_str(schema_json),
22
- ) {
23
- Ok(s) => s,
24
- Err(e) => return format!(r#"{{"ok":false,"error":"invalid schema: {}"}}"#, e),
25
- };
26
- let def = jsql_neo_core::types::TableDefinition {
27
- name: name.to_string(),
28
- schema,
29
- };
30
- match eng.borrow_mut().create_table(def) {
31
- Ok(()) => r#"{"ok":true}"#.to_string(),
32
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
33
- }
34
- })
35
- }
36
-
37
- #[wasm_bindgen]
38
- pub fn jsql_drop_table(name: &str) -> String {
39
- ENGINE.with(|eng| match eng.borrow_mut().drop_table(name) {
40
- Ok(()) => r#"{"ok":true}"#.to_string(),
41
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
42
- })
43
- }
44
-
45
- #[wasm_bindgen]
46
- pub fn jsql_insert(table: &str, data_json: &str) -> String {
47
- ENGINE.with(|eng| {
48
- let data: Vec<std::collections::HashMap<String, serde_json::Value>> =
49
- match serde_json::from_str(data_json) {
50
- Ok(d) => d,
51
- Err(e) => return format!(r#"{{"error":"invalid data: {}"}}"#, e),
52
- };
53
- match eng.borrow_mut().insert(table, data) {
54
- Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
55
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
56
- }
57
- })
58
- }
59
-
60
- #[wasm_bindgen]
61
- pub fn jsql_insert_json(table: &str, json: &str) -> String {
62
- ENGINE.with(|eng| {
63
- let res = (|| -> Result<Vec<u64>, String> {
64
- let mut eng = eng.borrow_mut();
65
- let t = eng.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
66
-
67
- let data: Vec<serde_json::Value> = serde_json::from_str(json)
68
- .map_err(|e| format!("invalid JSON: {}", e))?;
69
- let n = data.len();
70
- if n == 0 { return Ok(Vec::new()); }
71
-
72
- let first = data[0].as_object()
73
- .ok_or_else(|| "expected array of objects".to_string())?;
74
- let field_names: Vec<String> = first.keys().cloned().collect();
75
-
76
- let remap: Vec<Option<usize>> = t.field_order.iter()
77
- .map(|name| field_names.iter().position(|n| n == name))
78
- .collect();
79
-
80
- let pk_idx = t.pk_field.as_ref().and_then(|pk| {
81
- t.schema.get(pk.as_str()).and_then(|fs| {
82
- if fs.auto_increment { t.field_index.get(pk.as_str()).copied() } else { None }
83
- })
84
- });
85
-
86
- let now = js_sys::Date::now() as u64;
87
- let start_id = t.next_id;
88
- t.next_id += n as u64;
89
- t.pk_index.reserve(n);
90
- t.rows.reserve(n);
91
- t.values.reserve(t.field_order.len());
92
- let start_ri = t.rows.len();
93
-
94
- for (ri, row_val) in data.iter().enumerate() {
95
- let auto_id = start_id + ri as u64;
96
- let mut id = auto_id;
97
- let obj = row_val.as_object()
98
- .ok_or_else(|| "non-object in array".to_string())?;
99
-
100
- let mut values = Vec::with_capacity(t.field_order.len());
101
- for (i, rp) in remap.iter().enumerate() {
102
- if let Some(fi) = rp {
103
- let key = &field_names[*fi];
104
- if let Some(jv) = obj.get(key) {
105
- values.push(FieldValue::from_json(jv.clone()));
106
- continue;
107
- }
108
- } else if let Some(pi) = pk_idx {
109
- if pi == i {
110
- values.push(FieldValue::Int(id as i64));
111
- continue;
112
- }
113
- }
114
- if let Some(name) = t.field_order.get(i) {
115
- if let Some(fs) = t.schema.get(name) {
116
- if let Some(ref default) = fs.default {
117
- values.push(FieldValue::from_json(default.clone()));
118
- continue;
119
- }
120
- if !fs.nullable {
121
- values.push(default_value(&fs.field_type));
122
- continue;
123
- }
124
- }
125
- }
126
- values.push(FieldValue::Null);
127
- }
128
-
129
- // auto-increment PK: keep explicit positive value, else fill with auto id
130
- if let Some(pi) = pk_idx {
131
- if let Some(v) = values.get_mut(pi) {
132
- match v {
133
- FieldValue::Null => *v = FieldValue::Int(id as i64),
134
- FieldValue::Int(e) if *e > 0 => {
135
- id = *e as u64;
136
- t.next_id = t.next_id.max(id + 1);
137
- }
138
- _ => {}
139
- }
140
- }
141
- }
142
-
143
- t.pk_index.insert(id, start_ri + ri);
144
- let vs = t.values.len();
145
- t.values.extend(values);
146
- t.rows.push(RowStore { id, values_start: vs, num_values: t.field_order.len(), created_at: now, updated_at: now });
147
- }
148
-
149
- let ids: Vec<u64> = (start_id..start_id + n as u64).collect();
150
- Ok(ids)
151
- })();
152
- match res {
153
- Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
154
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
155
- }
156
- })
157
- }
158
-
159
- fn insert_buf_parse(eng: &mut MemoryEngine, table: &str, data: &[u8]) -> Result<Vec<u64>, String> {
160
- let t = eng.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
161
-
162
- let mut off = 0usize;
163
- let n_fields = *data.get(off).ok_or_else(|| "truncated header".to_string())? as usize;
164
- off += 1;
165
- if n_fields == 0 {
166
- return Err("invalid header".to_string());
167
- }
168
- let mut field_names = Vec::with_capacity(n_fields);
169
- for _ in 0..n_fields {
170
- let name_len = *data.get(off).ok_or_else(|| "truncated header".to_string())? as usize;
171
- off += 1;
172
- let name = std::str::from_utf8(
173
- data.get(off..off + name_len).ok_or_else(|| "truncated header".to_string())?,
174
- )
175
- .map_err(|_| "invalid utf8 in header".to_string())?
176
- .to_string();
177
- off += name_len;
178
- field_names.push(name);
179
- }
180
-
181
- let seg4 = data.get(off..off + 4).ok_or_else(|| "truncated".to_string())?;
182
- let row_count = u32::from_le_bytes([seg4[0], seg4[1], seg4[2], seg4[3]]) as usize;
183
- off += 4;
184
- if row_count == 0 {
185
- return Ok(Vec::new());
186
- }
187
-
188
- let pk_idx = t.pk_field.as_ref().and_then(|pk| {
189
- t.schema.get(pk.as_str()).and_then(|fs| {
190
- if fs.auto_increment { t.field_index.get(pk.as_str()).copied() } else { None }
191
- })
192
- });
193
-
194
- let order_matches = field_names.len() == t.field_order.len()
195
- && field_names.iter().zip(&t.field_order).all(|(a, b)| a == b);
196
- let remap: Vec<Option<usize>> = t.field_order.iter()
197
- .map(|name| field_names.iter().position(|n| n == name))
198
- .collect();
199
-
200
- let start_id = t.next_id;
201
- t.next_id += row_count as u64;
202
- t.pk_index.reserve(row_count);
203
- t.rows.reserve(row_count);
204
- t.values.reserve(t.field_order.len() * row_count);
205
- let now = js_sys::Date::now() as u64;
206
- let start_ri = t.rows.len();
207
-
208
- for ri in 0..row_count {
209
- let auto_id = start_id + ri as u64;
210
- let mut id = auto_id;
211
- let mut row_vals = Vec::with_capacity(n_fields);
212
- for _fi in 0..n_fields {
213
- let tag = *data.get(off).ok_or_else(|| "truncated row data".to_string())?;
214
- off += 1;
215
- match tag {
216
- 0 => row_vals.push(FieldValue::Null),
217
- 1 => {
218
- let b = data.get(off..off + 8).ok_or_else(|| "truncated row data".to_string())?;
219
- off += 8;
220
- row_vals.push(FieldValue::Int(i64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])));
221
- }
222
- 2 => {
223
- let b = data.get(off..off + 8).ok_or_else(|| "truncated row data".to_string())?;
224
- off += 8;
225
- row_vals.push(FieldValue::Float(f64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])));
226
- }
227
- 3 => {
228
- let seg = data.get(off..off + 4).ok_or_else(|| "truncated row data".to_string())?;
229
- let len = u32::from_le_bytes([seg[0], seg[1], seg[2], seg[3]]) as usize;
230
- off += 4;
231
- let s = std::str::from_utf8(
232
- data.get(off..off + len).ok_or_else(|| "truncated row data".to_string())?,
233
- )
234
- .map_err(|_| "invalid utf8 in row data".to_string())?
235
- .to_string();
236
- off += len;
237
- row_vals.push(FieldValue::String(s));
238
- }
239
- 4 => {
240
- let b = *data.get(off).ok_or_else(|| "truncated row data".to_string())?;
241
- off += 1;
242
- row_vals.push(FieldValue::Bool(b != 0));
243
- }
244
- 5 => {
245
- let b = data.get(off..off + 4).ok_or_else(|| "truncated row data".to_string())?;
246
- off += 4;
247
- row_vals.push(FieldValue::Int(i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as i64));
248
- }
249
- _ => row_vals.push(FieldValue::Null),
250
- };
251
- }
252
-
253
- let mut values = Vec::with_capacity(t.field_order.len());
254
- if order_matches {
255
- values = row_vals;
256
- if values.len() < t.field_order.len() {
257
- values.resize(t.field_order.len(), FieldValue::Null);
258
- }
259
- } else {
260
- for (i, rp) in remap.iter().enumerate() {
261
- if let Some(si) = rp {
262
- if *si < row_vals.len() {
263
- values.push(std::mem::take(&mut row_vals[*si]));
264
- continue;
265
- }
266
- }
267
- if let Some(pi) = pk_idx {
268
- if pi == i {
269
- values.push(FieldValue::Int(id as i64));
270
- continue;
271
- }
272
- }
273
- if let Some(name) = t.field_order.get(i) {
274
- if let Some(fs) = t.schema.get(name) {
275
- if let Some(ref default) = fs.default {
276
- values.push(FieldValue::from_json(default.clone()));
277
- continue;
278
- }
279
- }
280
- }
281
- values.push(FieldValue::Null);
282
- }
283
- }
284
-
285
- // auto-increment PK: keep explicit positive value (row id = pk), else fill with auto id
286
- if let Some(pi) = pk_idx {
287
- if let Some(v) = values.get_mut(pi) {
288
- match v {
289
- FieldValue::Null => *v = FieldValue::Int(id as i64),
290
- FieldValue::Int(e) if *e > 0 => {
291
- id = *e as u64;
292
- t.next_id = t.next_id.max(id + 1);
293
- }
294
- _ => {}
295
- }
296
- }
297
- }
298
-
299
- t.pk_index.insert(id, start_ri + ri);
300
- let vs = t.values.len();
301
- t.values.extend(values);
302
- t.rows.push(RowStore { id, values_start: vs, num_values: t.field_order.len(), created_at: now, updated_at: now });
303
- }
304
-
305
- Ok((start_id..start_id + row_count as u64).collect())
306
- }
307
-
308
- #[wasm_bindgen]
309
- pub fn jsql_insert_buf(table: &str, data: &[u8]) -> String {
310
- ENGINE.with(|eng| {
311
- match insert_buf_parse(&mut eng.borrow_mut(), table, data) {
312
- Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
313
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
314
- }
315
- })
316
- }
317
- #[wasm_bindgen]
318
- pub fn jsql_insert_buf_count(table: &str, data: &[u8]) -> String {
319
- ENGINE.with(|eng| {
320
- match insert_buf_parse(&mut eng.borrow_mut(), table, data) {
321
- Ok(ids) => format!("{}", ids.len()),
322
- Err(_) => "0".to_string(),
323
- }
324
- })
325
- }
326
- #[wasm_bindgen]
327
- pub fn jsql_find(table: &str, filter_json: &str, limit: usize, offset: usize) -> String {
328
- ENGINE.with(|eng| {
329
- let filter: Option<std::collections::HashMap<String, serde_json::Value>> =
330
- if filter_json.is_empty() { None } else { serde_json::from_str(filter_json).ok() };
331
- match eng.borrow().find(table, &filter, Some(limit), Some(offset), None, &None, &None) {
332
- Ok((rows, _, _)) => serde_json::to_string(&rows).unwrap_or_else(|_| "[]".to_string()),
333
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
334
- }
335
- })
336
- }
337
-
338
- #[wasm_bindgen]
339
- pub fn jsql_find_by_id(table: &str, id: u64) -> String {
340
- ENGINE.with(|eng| {
341
- match eng.borrow().find_by_id_json(table, id) {
342
- Ok(Some(s)) => s,
343
- Ok(None) => "null".to_string(),
344
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
345
- }
346
- })
347
- }
348
-
349
- #[wasm_bindgen]
350
- pub fn jsql_count(table: &str) -> String {
351
- ENGINE.with(|eng| match eng.borrow().count(table) {
352
- Ok(c) => format!("{}", c),
353
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
354
- })
355
- }
356
-
357
- #[wasm_bindgen]
358
- pub fn jsql_update_by_id(table: &str, id: u64, data_json: &str) -> String {
359
- ENGINE.with(|eng| {
360
- let data: std::collections::HashMap<String, serde_json::Value> =
361
- match serde_json::from_str(data_json) {
362
- Ok(d) => d,
363
- Err(e) => return format!(r#"{{"ok":false,"error":"invalid data: {}"}}"#, e),
364
- };
365
- match eng.borrow_mut().update_by_id(table, id, data) {
366
- Ok(true) => r#"{"ok":true}"#.to_string(),
367
- Ok(false) => r#"{"ok":false,"error":"not found"}"#.to_string(),
368
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
369
- }
370
- })
371
- }
372
-
373
- #[wasm_bindgen]
374
- pub fn jsql_remove_by_id(table: &str, id: u64) -> String {
375
- ENGINE.with(|eng| match eng.borrow_mut().remove_by_id(table, id) {
376
- Ok(true) => r#"{"ok":true}"#.to_string(),
377
- Ok(false) => r#"{"ok":false,"error":"not found"}"#.to_string(),
378
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
379
- })
380
- }
381
-
382
- #[wasm_bindgen]
383
- pub fn jsql_remove_by_ids(table: &str, ids_json: &str) -> String {
384
- ENGINE.with(|eng| {
385
- let ids: Vec<u64> = match serde_json::from_str(ids_json) {
386
- Ok(v) => v,
387
- Err(e) => return format!(r#"{{"error":"invalid ids: {}"}}"#, e),
388
- };
389
- match eng.borrow_mut().remove_by_ids(table, &ids) {
390
- Ok(n) => format!(r#"{{"ok":true,"count":{}}}"#, n),
391
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
392
- }
393
- })
394
- }
395
-
396
- #[wasm_bindgen]
397
- pub fn jsql_update_by_ids(table: &str, batch_json: &str) -> String {
398
- ENGINE.with(|eng| {
399
- let batch: Vec<(u64, std::collections::HashMap<String, serde_json::Value>)> =
400
- match serde_json::from_str(batch_json) {
401
- Ok(v) => v,
402
- Err(e) => return format!(r#"{{"error":"invalid batch: {}"}}"#, e),
403
- };
404
- match eng.borrow_mut().update_by_ids(table, batch) {
405
- Ok(n) => format!(r#"{{"ok":true,"count":{}}}"#, n),
406
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
407
- }
408
- })
409
- }
410
-
411
- #[wasm_bindgen]
412
- pub fn jsql_find_by_ids(table: &str, ids_json: &str) -> String {
413
- ENGINE.with(|eng| {
414
- let ids: Vec<u64> = match serde_json::from_str(ids_json) {
415
- Ok(v) => v,
416
- Err(e) => return format!(r#"{{"error":"invalid ids: {}"}}"#, e),
417
- };
418
- match eng.borrow().find_by_ids_json(table, &ids) {
419
- Ok(s) => s,
420
- Err(e) => format!(r#"{{"error":"{}"}}"#, e),
421
- }
422
- })
423
- }
424
-
425
- #[wasm_bindgen]
426
- pub fn jsql_begin_tx() -> String {
427
- ENGINE.with(|eng| match eng.borrow_mut().begin_tx() {
428
- Ok(tx_id) => serde_json::json!({ "ok": true, "txId": tx_id }).to_string(),
429
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
430
- })
431
- }
432
-
433
- #[wasm_bindgen]
434
- pub fn jsql_commit_tx(tx_id: &str) -> String {
435
- ENGINE.with(|eng| match eng.borrow_mut().commit_tx(tx_id) {
436
- Ok(()) => r#"{"ok":true}"#.to_string(),
437
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
438
- })
439
- }
440
-
441
- #[wasm_bindgen]
442
- pub fn jsql_rollback_tx(tx_id: &str) -> String {
443
- ENGINE.with(|eng| match eng.borrow_mut().rollback_tx(tx_id) {
444
- Ok(()) => r#"{"ok":true}"#.to_string(),
445
- Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
446
- })
447
- }
1
+ use jsql_neo_core::engine::{Engine, MemoryEngine, FieldValue, RowStore, default_value};
2
+ use jsql_neo_core::types::{schema_order_preserving, FieldSchema};
3
+ use std::cell::RefCell;
4
+ use wasm_bindgen::prelude::*;
5
+
6
+ thread_local! {
7
+ static ENGINE: RefCell<MemoryEngine> = RefCell::new(MemoryEngine::new());
8
+ }
9
+
10
+ #[wasm_bindgen]
11
+ pub fn jsql_reset() {
12
+ ENGINE.with(|eng| {
13
+ eng.borrow_mut().clear();
14
+ });
15
+ }
16
+
17
+ #[wasm_bindgen]
18
+ pub fn jsql_create_table(name: &str, schema_json: &str) -> String {
19
+ ENGINE.with(|eng| {
20
+ let schema: Vec<(String, FieldSchema)> = match schema_order_preserving::deserialize(
21
+ &mut serde_json::Deserializer::from_str(schema_json),
22
+ ) {
23
+ Ok(s) => s,
24
+ Err(e) => return format!(r#"{{"ok":false,"error":"invalid schema: {}"}}"#, e),
25
+ };
26
+ let def = jsql_neo_core::types::TableDefinition {
27
+ name: name.to_string(),
28
+ schema,
29
+ };
30
+ match eng.borrow_mut().create_table(def) {
31
+ Ok(()) => r#"{"ok":true}"#.to_string(),
32
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
33
+ }
34
+ })
35
+ }
36
+
37
+ #[wasm_bindgen]
38
+ pub fn jsql_drop_table(name: &str) -> String {
39
+ ENGINE.with(|eng| match eng.borrow_mut().drop_table(name) {
40
+ Ok(()) => r#"{"ok":true}"#.to_string(),
41
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
42
+ })
43
+ }
44
+
45
+ #[wasm_bindgen]
46
+ pub fn jsql_insert(table: &str, data_json: &str) -> String {
47
+ ENGINE.with(|eng| {
48
+ let data: Vec<std::collections::HashMap<String, serde_json::Value>> =
49
+ match serde_json::from_str(data_json) {
50
+ Ok(d) => d,
51
+ Err(e) => return format!(r#"{{"error":"invalid data: {}"}}"#, e),
52
+ };
53
+ match eng.borrow_mut().insert(table, data) {
54
+ Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
55
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
56
+ }
57
+ })
58
+ }
59
+
60
+ #[wasm_bindgen]
61
+ pub fn jsql_insert_json(table: &str, json: &str) -> String {
62
+ ENGINE.with(|eng| {
63
+ let res = (|| -> Result<Vec<u64>, String> {
64
+ let mut eng = eng.borrow_mut();
65
+ let t = eng.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
66
+
67
+ let data: Vec<serde_json::Value> = serde_json::from_str(json)
68
+ .map_err(|e| format!("invalid JSON: {}", e))?;
69
+ let n = data.len();
70
+ if n == 0 { return Ok(Vec::new()); }
71
+
72
+ let first = data[0].as_object()
73
+ .ok_or_else(|| "expected array of objects".to_string())?;
74
+ let field_names: Vec<String> = first.keys().cloned().collect();
75
+
76
+ let remap: Vec<Option<usize>> = t.field_order.iter()
77
+ .map(|name| field_names.iter().position(|n| n == name))
78
+ .collect();
79
+
80
+ let pk_idx = t.pk_field.as_ref().and_then(|pk| {
81
+ t.schema.get(pk.as_str()).and_then(|fs| {
82
+ if fs.auto_increment { t.field_index.get(pk.as_str()).copied() } else { None }
83
+ })
84
+ });
85
+
86
+ let now = js_sys::Date::now() as u64;
87
+ let start_id = t.next_id;
88
+ t.next_id += n as u64;
89
+ t.pk_index.reserve(n);
90
+ t.rows.reserve(n);
91
+ t.values.reserve(t.field_order.len());
92
+ let start_ri = t.rows.len();
93
+
94
+ for (ri, row_val) in data.iter().enumerate() {
95
+ let auto_id = start_id + ri as u64;
96
+ let mut id = auto_id;
97
+ let obj = row_val.as_object()
98
+ .ok_or_else(|| "non-object in array".to_string())?;
99
+
100
+ let mut values = Vec::with_capacity(t.field_order.len());
101
+ for (i, rp) in remap.iter().enumerate() {
102
+ if let Some(fi) = rp {
103
+ let key = &field_names[*fi];
104
+ if let Some(jv) = obj.get(key) {
105
+ values.push(FieldValue::from_json(jv.clone()));
106
+ continue;
107
+ }
108
+ } else if let Some(pi) = pk_idx {
109
+ if pi == i {
110
+ values.push(FieldValue::Int(id as i64));
111
+ continue;
112
+ }
113
+ }
114
+ if let Some(name) = t.field_order.get(i) {
115
+ if let Some(fs) = t.schema.get(name) {
116
+ if let Some(ref default) = fs.default {
117
+ values.push(FieldValue::from_json(default.clone()));
118
+ continue;
119
+ }
120
+ if !fs.nullable {
121
+ values.push(default_value(&fs.field_type));
122
+ continue;
123
+ }
124
+ }
125
+ }
126
+ values.push(FieldValue::Null);
127
+ }
128
+
129
+ // auto-increment PK: keep explicit positive value, else fill with auto id
130
+ if let Some(pi) = pk_idx {
131
+ if let Some(v) = values.get_mut(pi) {
132
+ match v {
133
+ FieldValue::Null => *v = FieldValue::Int(id as i64),
134
+ FieldValue::Int(e) if *e > 0 => {
135
+ id = *e as u64;
136
+ t.next_id = t.next_id.max(id + 1);
137
+ }
138
+ _ => {}
139
+ }
140
+ }
141
+ }
142
+
143
+ t.pk_index.insert(id, start_ri + ri);
144
+ let vs = t.values.len();
145
+ t.values.extend(values);
146
+ t.rows.push(RowStore { id, values_start: vs, num_values: t.field_order.len(), created_at: now, updated_at: now });
147
+ }
148
+
149
+ let ids: Vec<u64> = (start_id..start_id + n as u64).collect();
150
+ Ok(ids)
151
+ })();
152
+ match res {
153
+ Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
154
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
155
+ }
156
+ })
157
+ }
158
+
159
+ fn insert_buf_parse(eng: &mut MemoryEngine, table: &str, data: &[u8]) -> Result<Vec<u64>, String> {
160
+ let t = eng.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
161
+
162
+ let mut off = 0usize;
163
+ let n_fields = *data.get(off).ok_or_else(|| "truncated header".to_string())? as usize;
164
+ off += 1;
165
+ if n_fields == 0 {
166
+ return Err("invalid header".to_string());
167
+ }
168
+ let mut field_names = Vec::with_capacity(n_fields);
169
+ for _ in 0..n_fields {
170
+ let name_len = *data.get(off).ok_or_else(|| "truncated header".to_string())? as usize;
171
+ off += 1;
172
+ let name = std::str::from_utf8(
173
+ data.get(off..off + name_len).ok_or_else(|| "truncated header".to_string())?,
174
+ )
175
+ .map_err(|_| "invalid utf8 in header".to_string())?
176
+ .to_string();
177
+ off += name_len;
178
+ field_names.push(name);
179
+ }
180
+
181
+ let seg4 = data.get(off..off + 4).ok_or_else(|| "truncated".to_string())?;
182
+ let row_count = u32::from_le_bytes([seg4[0], seg4[1], seg4[2], seg4[3]]) as usize;
183
+ off += 4;
184
+ if row_count == 0 {
185
+ return Ok(Vec::new());
186
+ }
187
+
188
+ let pk_idx = t.pk_field.as_ref().and_then(|pk| {
189
+ t.schema.get(pk.as_str()).and_then(|fs| {
190
+ if fs.auto_increment { t.field_index.get(pk.as_str()).copied() } else { None }
191
+ })
192
+ });
193
+
194
+ let order_matches = field_names.len() == t.field_order.len()
195
+ && field_names.iter().zip(&t.field_order).all(|(a, b)| a == b);
196
+ let remap: Vec<Option<usize>> = t.field_order.iter()
197
+ .map(|name| field_names.iter().position(|n| n == name))
198
+ .collect();
199
+
200
+ let start_id = t.next_id;
201
+ t.next_id += row_count as u64;
202
+ t.pk_index.reserve(row_count);
203
+ t.rows.reserve(row_count);
204
+ t.values.reserve(t.field_order.len() * row_count);
205
+ let now = js_sys::Date::now() as u64;
206
+ let start_ri = t.rows.len();
207
+
208
+ for ri in 0..row_count {
209
+ let auto_id = start_id + ri as u64;
210
+ let mut id = auto_id;
211
+ let mut row_vals = Vec::with_capacity(n_fields);
212
+ for _fi in 0..n_fields {
213
+ let tag = *data.get(off).ok_or_else(|| "truncated row data".to_string())?;
214
+ off += 1;
215
+ match tag {
216
+ 0 => row_vals.push(FieldValue::Null),
217
+ 1 => {
218
+ let b = data.get(off..off + 8).ok_or_else(|| "truncated row data".to_string())?;
219
+ off += 8;
220
+ row_vals.push(FieldValue::Int(i64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])));
221
+ }
222
+ 2 => {
223
+ let b = data.get(off..off + 8).ok_or_else(|| "truncated row data".to_string())?;
224
+ off += 8;
225
+ row_vals.push(FieldValue::Float(f64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])));
226
+ }
227
+ 3 => {
228
+ let seg = data.get(off..off + 4).ok_or_else(|| "truncated row data".to_string())?;
229
+ let len = u32::from_le_bytes([seg[0], seg[1], seg[2], seg[3]]) as usize;
230
+ off += 4;
231
+ let s = std::str::from_utf8(
232
+ data.get(off..off + len).ok_or_else(|| "truncated row data".to_string())?,
233
+ )
234
+ .map_err(|_| "invalid utf8 in row data".to_string())?
235
+ .to_string();
236
+ off += len;
237
+ row_vals.push(FieldValue::String(s));
238
+ }
239
+ 4 => {
240
+ let b = *data.get(off).ok_or_else(|| "truncated row data".to_string())?;
241
+ off += 1;
242
+ row_vals.push(FieldValue::Bool(b != 0));
243
+ }
244
+ 5 => {
245
+ let b = data.get(off..off + 4).ok_or_else(|| "truncated row data".to_string())?;
246
+ off += 4;
247
+ row_vals.push(FieldValue::Int(i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as i64));
248
+ }
249
+ _ => row_vals.push(FieldValue::Null),
250
+ };
251
+ }
252
+
253
+ let mut values = Vec::with_capacity(t.field_order.len());
254
+ if order_matches {
255
+ values = row_vals;
256
+ if values.len() < t.field_order.len() {
257
+ values.resize(t.field_order.len(), FieldValue::Null);
258
+ }
259
+ } else {
260
+ for (i, rp) in remap.iter().enumerate() {
261
+ if let Some(si) = rp {
262
+ if *si < row_vals.len() {
263
+ values.push(std::mem::take(&mut row_vals[*si]));
264
+ continue;
265
+ }
266
+ }
267
+ if let Some(pi) = pk_idx {
268
+ if pi == i {
269
+ values.push(FieldValue::Int(id as i64));
270
+ continue;
271
+ }
272
+ }
273
+ if let Some(name) = t.field_order.get(i) {
274
+ if let Some(fs) = t.schema.get(name) {
275
+ if let Some(ref default) = fs.default {
276
+ values.push(FieldValue::from_json(default.clone()));
277
+ continue;
278
+ }
279
+ }
280
+ }
281
+ values.push(FieldValue::Null);
282
+ }
283
+ }
284
+
285
+ // auto-increment PK: keep explicit positive value (row id = pk), else fill with auto id
286
+ if let Some(pi) = pk_idx {
287
+ if let Some(v) = values.get_mut(pi) {
288
+ match v {
289
+ FieldValue::Null => *v = FieldValue::Int(id as i64),
290
+ FieldValue::Int(e) if *e > 0 => {
291
+ id = *e as u64;
292
+ t.next_id = t.next_id.max(id + 1);
293
+ }
294
+ _ => {}
295
+ }
296
+ }
297
+ }
298
+
299
+ t.pk_index.insert(id, start_ri + ri);
300
+ let vs = t.values.len();
301
+ t.values.extend(values);
302
+ t.rows.push(RowStore { id, values_start: vs, num_values: t.field_order.len(), created_at: now, updated_at: now });
303
+ }
304
+
305
+ Ok((start_id..start_id + row_count as u64).collect())
306
+ }
307
+
308
+ #[wasm_bindgen]
309
+ pub fn jsql_insert_buf(table: &str, data: &[u8]) -> String {
310
+ ENGINE.with(|eng| {
311
+ match insert_buf_parse(&mut eng.borrow_mut(), table, data) {
312
+ Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
313
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
314
+ }
315
+ })
316
+ }
317
+ #[wasm_bindgen]
318
+ pub fn jsql_insert_buf_count(table: &str, data: &[u8]) -> String {
319
+ ENGINE.with(|eng| {
320
+ match insert_buf_parse(&mut eng.borrow_mut(), table, data) {
321
+ Ok(ids) => format!("{}", ids.len()),
322
+ Err(_) => "0".to_string(),
323
+ }
324
+ })
325
+ }
326
+ #[wasm_bindgen]
327
+ pub fn jsql_find(table: &str, filter_json: &str, limit: usize, offset: usize) -> String {
328
+ ENGINE.with(|eng| {
329
+ let filter: Option<std::collections::HashMap<String, serde_json::Value>> =
330
+ if filter_json.is_empty() { None } else { serde_json::from_str(filter_json).ok() };
331
+ match eng.borrow().find(table, &filter, Some(limit), Some(offset), None, &None, &None) {
332
+ Ok((rows, _, _)) => serde_json::to_string(&rows).unwrap_or_else(|_| "[]".to_string()),
333
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
334
+ }
335
+ })
336
+ }
337
+
338
+ #[wasm_bindgen]
339
+ pub fn jsql_find_by_id(table: &str, id: u64) -> String {
340
+ ENGINE.with(|eng| {
341
+ match eng.borrow().find_by_id_json(table, id) {
342
+ Ok(Some(s)) => s,
343
+ Ok(None) => "null".to_string(),
344
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
345
+ }
346
+ })
347
+ }
348
+
349
+ #[wasm_bindgen]
350
+ pub fn jsql_count(table: &str) -> String {
351
+ ENGINE.with(|eng| match eng.borrow().count(table) {
352
+ Ok(c) => format!("{}", c),
353
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
354
+ })
355
+ }
356
+
357
+ #[wasm_bindgen]
358
+ pub fn jsql_update_by_id(table: &str, id: u64, data_json: &str) -> String {
359
+ ENGINE.with(|eng| {
360
+ let data: std::collections::HashMap<String, serde_json::Value> =
361
+ match serde_json::from_str(data_json) {
362
+ Ok(d) => d,
363
+ Err(e) => return format!(r#"{{"ok":false,"error":"invalid data: {}"}}"#, e),
364
+ };
365
+ match eng.borrow_mut().update_by_id(table, id, data) {
366
+ Ok(true) => r#"{"ok":true}"#.to_string(),
367
+ Ok(false) => r#"{"ok":false,"error":"not found"}"#.to_string(),
368
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
369
+ }
370
+ })
371
+ }
372
+
373
+ #[wasm_bindgen]
374
+ pub fn jsql_remove_by_id(table: &str, id: u64) -> String {
375
+ ENGINE.with(|eng| match eng.borrow_mut().remove_by_id(table, id) {
376
+ Ok(true) => r#"{"ok":true}"#.to_string(),
377
+ Ok(false) => r#"{"ok":false,"error":"not found"}"#.to_string(),
378
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
379
+ })
380
+ }
381
+
382
+ #[wasm_bindgen]
383
+ pub fn jsql_remove_by_ids(table: &str, ids_json: &str) -> String {
384
+ ENGINE.with(|eng| {
385
+ let ids: Vec<u64> = match serde_json::from_str(ids_json) {
386
+ Ok(v) => v,
387
+ Err(e) => return format!(r#"{{"error":"invalid ids: {}"}}"#, e),
388
+ };
389
+ match eng.borrow_mut().remove_by_ids(table, &ids) {
390
+ Ok(n) => format!(r#"{{"ok":true,"count":{}}}"#, n),
391
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
392
+ }
393
+ })
394
+ }
395
+
396
+ #[wasm_bindgen]
397
+ pub fn jsql_update_by_ids(table: &str, batch_json: &str) -> String {
398
+ ENGINE.with(|eng| {
399
+ let batch: Vec<(u64, std::collections::HashMap<String, serde_json::Value>)> =
400
+ match serde_json::from_str(batch_json) {
401
+ Ok(v) => v,
402
+ Err(e) => return format!(r#"{{"error":"invalid batch: {}"}}"#, e),
403
+ };
404
+ match eng.borrow_mut().update_by_ids(table, batch) {
405
+ Ok(n) => format!(r#"{{"ok":true,"count":{}}}"#, n),
406
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
407
+ }
408
+ })
409
+ }
410
+
411
+ #[wasm_bindgen]
412
+ pub fn jsql_find_by_ids(table: &str, ids_json: &str) -> String {
413
+ ENGINE.with(|eng| {
414
+ let ids: Vec<u64> = match serde_json::from_str(ids_json) {
415
+ Ok(v) => v,
416
+ Err(e) => return format!(r#"{{"error":"invalid ids: {}"}}"#, e),
417
+ };
418
+ match eng.borrow().find_by_ids_json(table, &ids) {
419
+ Ok(s) => s,
420
+ Err(e) => format!(r#"{{"error":"{}"}}"#, e),
421
+ }
422
+ })
423
+ }
424
+
425
+ #[wasm_bindgen]
426
+ pub fn jsql_begin_tx() -> String {
427
+ ENGINE.with(|eng| match eng.borrow_mut().begin_tx() {
428
+ Ok(tx_id) => serde_json::json!({ "ok": true, "txId": tx_id }).to_string(),
429
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
430
+ })
431
+ }
432
+
433
+ #[wasm_bindgen]
434
+ pub fn jsql_commit_tx(tx_id: &str) -> String {
435
+ ENGINE.with(|eng| match eng.borrow_mut().commit_tx(tx_id) {
436
+ Ok(()) => r#"{"ok":true}"#.to_string(),
437
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
438
+ })
439
+ }
440
+
441
+ #[wasm_bindgen]
442
+ pub fn jsql_rollback_tx(tx_id: &str) -> String {
443
+ ENGINE.with(|eng| match eng.borrow_mut().rollback_tx(tx_id) {
444
+ Ok(()) => r#"{"ok":true}"#.to_string(),
445
+ Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
446
+ })
447
+ }