jsql-neo 3.5.2 → 4.0.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/LICENSE +202 -0
- package/bin/jsql +97 -0
- package/index.js +20 -0
- package/lib/database.js +484 -28
- package/lib/mod.js +316 -0
- package/lib/mysql_compat.js +232 -0
- package/lib/mysql_server.js +514 -0
- package/lib/native_client.js +470 -0
- package/lib/nedb_compat.js +506 -0
- package/lib/plugin.js +35 -0
- package/lib/sql.js +1160 -0
- package/lib/table.js +42 -21
- package/lib/wasm_client.js +176 -8
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/Cargo.toml +24 -0
- package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +449 -0
- package/nativesrc/jsql-neo-core/src/engine/memory.rs +147 -0
- package/nativesrc/jsql-neo-core/src/engine/mod.rs +41 -0
- package/nativesrc/jsql-neo-core/src/engine/table.rs +664 -0
- package/nativesrc/jsql-neo-core/src/lib.rs +3 -0
- package/nativesrc/jsql-neo-core/src/storage/mod.rs +1 -0
- package/nativesrc/jsql-neo-core/src/storage/persistent.rs +2 -0
- package/nativesrc/jsql-neo-core/src/storage/wal.rs +85 -0
- package/nativesrc/jsql-neo-core/src/types.rs +94 -0
- package/nativesrc/jsql-neo-native/Cargo.lock +606 -0
- package/nativesrc/jsql-neo-native/Cargo.toml +16 -0
- package/nativesrc/jsql-neo-native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-native/package.json +7 -0
- package/nativesrc/jsql-neo-native/src/lib.rs +281 -0
- package/package.json +9 -1
- package/wasm/jsql_neo_wasm.d.ts +8 -0
- package/wasm/jsql_neo_wasm.js +455 -6
- package/wasm/jsql_neo_wasm_bg.js +22 -0
- package/wasm/jsql_neo_wasm_bg.wasm +0 -0
- package/wasm/jsql_neo_wasm_bg.wasm.d.ts +4 -0
- package/wasm/package.json +1 -8
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
use std::cell::RefCell;
|
|
2
|
+
use std::collections::HashMap;
|
|
3
|
+
use jsql_neo_core::engine::{Engine, HybridEngine, FieldValue, RowStore};
|
|
4
|
+
use jsql_neo_core::types::{FieldSchema, TableDefinition, schema_order_preserving};
|
|
5
|
+
use napi_derive::napi;
|
|
6
|
+
use napi::JsBuffer;
|
|
7
|
+
|
|
8
|
+
thread_local! {
|
|
9
|
+
static ENGINE: RefCell<HybridEngine> = RefCell::new(HybridEngine::new());
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
fn safe_json_result(result: Result<String, String>) -> String {
|
|
13
|
+
match result {
|
|
14
|
+
Ok(s) => s,
|
|
15
|
+
Err(e) => format!(r#"{{"error":"{}"}}"#, e.replace('"', r#"\""#)),
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#[napi]
|
|
20
|
+
pub fn jsql_open(dir: String, mode: String) -> String {
|
|
21
|
+
ENGINE.with(|eng| {
|
|
22
|
+
let open_res = eng.borrow_mut().open(&dir, &mode);
|
|
23
|
+
match open_res {
|
|
24
|
+
Ok(()) => {
|
|
25
|
+
let names = eng.borrow().list_tables();
|
|
26
|
+
let mut out = serde_json::Map::new();
|
|
27
|
+
out.insert("ok".into(), serde_json::Value::Bool(true));
|
|
28
|
+
out.insert("tables".into(), serde_json::Value::Array(
|
|
29
|
+
names.iter().map(|n| serde_json::Value::String(n.clone())).collect()
|
|
30
|
+
));
|
|
31
|
+
let mut schemas = serde_json::Map::new();
|
|
32
|
+
for n in &names {
|
|
33
|
+
let s = {
|
|
34
|
+
let eng_ref = eng.borrow();
|
|
35
|
+
eng_ref.table_schema(n)
|
|
36
|
+
};
|
|
37
|
+
if let Some(s) = s {
|
|
38
|
+
let mut v = serde_json::Map::new();
|
|
39
|
+
for (k, fs) in &s {
|
|
40
|
+
if let Ok(fv) = serde_json::to_value(fs) {
|
|
41
|
+
v.insert(k.clone(), fv);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
schemas.insert(n.clone(), serde_json::Value::Object(v));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
out.insert("schemas".into(), serde_json::Value::Object(schemas));
|
|
48
|
+
serde_json::to_string(&out).unwrap_or_else(|_| r#"{"ok":true}"#.to_string())
|
|
49
|
+
}
|
|
50
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e.replace('"', r#"\""#)),
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#[napi]
|
|
56
|
+
pub fn jsql_flush_dirty() -> String {
|
|
57
|
+
ENGINE.with(|eng| {
|
|
58
|
+
match eng.borrow().flush_dirty() {
|
|
59
|
+
Ok(n) => format!(r#"{{"ok":true,"flushed":{}}}"#, n),
|
|
60
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e.replace('"', r#"\""#)),
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[napi]
|
|
66
|
+
pub fn jsql_evict() -> String {
|
|
67
|
+
ENGINE.with(|eng| {
|
|
68
|
+
match eng.borrow().evict_one() {
|
|
69
|
+
Some(name) => format!(r#"{{"ok":true,"evicted":"{}","remaining":{}}}"#, name, eng.borrow().mem_count()),
|
|
70
|
+
None => r#"{"ok":true,"evicted":null,"remaining":0}"#.to_string(),
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#[napi]
|
|
76
|
+
pub fn jsql_close() -> String {
|
|
77
|
+
ENGINE.with(|eng| {
|
|
78
|
+
eng.borrow_mut().close();
|
|
79
|
+
r#"{"ok":true}"#.to_string()
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#[napi]
|
|
84
|
+
pub fn jsql_create_table(name: String, schema_json: String) -> String {
|
|
85
|
+
use schema_order_preserving;
|
|
86
|
+
let mut d = serde_json::Deserializer::from_str(&schema_json);
|
|
87
|
+
let schema: Vec<(String, FieldSchema)> = match schema_order_preserving::deserialize(&mut d) {
|
|
88
|
+
Ok(s) => s,
|
|
89
|
+
Err(e) => return format!(r#"{{"ok":false,"error":"invalid schema: {}"}}"#, e),
|
|
90
|
+
};
|
|
91
|
+
let def = TableDefinition { name: name.clone(), schema };
|
|
92
|
+
ENGINE.with(|eng| match eng.borrow_mut().create_table(def) {
|
|
93
|
+
Ok(()) => r#"{"ok":true}"#.to_string(),
|
|
94
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
#[napi]
|
|
99
|
+
pub fn jsql_drop_table(name: String) -> String {
|
|
100
|
+
ENGINE.with(|eng| match eng.borrow_mut().drop_table(&name) {
|
|
101
|
+
Ok(()) => r#"{"ok":true}"#.to_string(),
|
|
102
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[napi]
|
|
107
|
+
pub fn jsql_insert(table: String, data_json: String) -> String {
|
|
108
|
+
ENGINE.with(|eng| {
|
|
109
|
+
let data: Vec<HashMap<String, serde_json::Value>> = match serde_json::from_str(&data_json) {
|
|
110
|
+
Ok(d) => d,
|
|
111
|
+
Err(e) => return format!(r#"{{"error":"invalid data: {}"}}"#, e),
|
|
112
|
+
};
|
|
113
|
+
match eng.borrow_mut().insert(&table, data) {
|
|
114
|
+
Ok(ids) => serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()),
|
|
115
|
+
Err(e) => format!(r#"{{"error":"{}"}}"#, e),
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#[inline(always)]
|
|
121
|
+
unsafe fn parse_field_value_unsafe(data: &[u8], off: &mut usize) -> FieldValue {
|
|
122
|
+
let tag = *data.get_unchecked(*off); *off += 1;
|
|
123
|
+
match tag {
|
|
124
|
+
0 => FieldValue::Null,
|
|
125
|
+
1 => {
|
|
126
|
+
let n = i64::from_le_bytes([
|
|
127
|
+
*data.get_unchecked(*off), *data.get_unchecked(*off+1),
|
|
128
|
+
*data.get_unchecked(*off+2), *data.get_unchecked(*off+3),
|
|
129
|
+
*data.get_unchecked(*off+4), *data.get_unchecked(*off+5),
|
|
130
|
+
*data.get_unchecked(*off+6), *data.get_unchecked(*off+7),
|
|
131
|
+
]);
|
|
132
|
+
*off += 8;
|
|
133
|
+
FieldValue::Int(n)
|
|
134
|
+
}
|
|
135
|
+
2 => {
|
|
136
|
+
let n = f64::from_le_bytes([
|
|
137
|
+
*data.get_unchecked(*off), *data.get_unchecked(*off+1),
|
|
138
|
+
*data.get_unchecked(*off+2), *data.get_unchecked(*off+3),
|
|
139
|
+
*data.get_unchecked(*off+4), *data.get_unchecked(*off+5),
|
|
140
|
+
*data.get_unchecked(*off+6), *data.get_unchecked(*off+7),
|
|
141
|
+
]);
|
|
142
|
+
*off += 8;
|
|
143
|
+
FieldValue::Float(n)
|
|
144
|
+
}
|
|
145
|
+
3 => {
|
|
146
|
+
let len = u32::from_le_bytes([
|
|
147
|
+
*data.get_unchecked(*off), *data.get_unchecked(*off+1),
|
|
148
|
+
*data.get_unchecked(*off+2), *data.get_unchecked(*off+3),
|
|
149
|
+
]) as usize;
|
|
150
|
+
*off += 4;
|
|
151
|
+
let s = std::str::from_utf8_unchecked(std::slice::from_raw_parts(
|
|
152
|
+
data.as_ptr().add(*off), len
|
|
153
|
+
));
|
|
154
|
+
*off += len;
|
|
155
|
+
FieldValue::String(s.to_string())
|
|
156
|
+
}
|
|
157
|
+
4 => { let b = *data.get_unchecked(*off) != 0; *off += 1; FieldValue::Bool(b) }
|
|
158
|
+
5 => {
|
|
159
|
+
let n = i32::from_le_bytes([
|
|
160
|
+
*data.get_unchecked(*off), *data.get_unchecked(*off+1),
|
|
161
|
+
*data.get_unchecked(*off+2), *data.get_unchecked(*off+3),
|
|
162
|
+
]) as i64;
|
|
163
|
+
*off += 4;
|
|
164
|
+
FieldValue::Int(n)
|
|
165
|
+
}
|
|
166
|
+
_ => FieldValue::Null,
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#[napi]
|
|
171
|
+
pub fn jsql_insert_buf(table: String, data: JsBuffer) -> String {
|
|
172
|
+
let buf_val = match data.into_value() {
|
|
173
|
+
Ok(v) => v,
|
|
174
|
+
Err(_) => return r#"{"error":"invalid buffer"}"#.to_string(),
|
|
175
|
+
};
|
|
176
|
+
ENGINE.with(|eng| {
|
|
177
|
+
let buf: &[u8] = buf_val.as_ref();
|
|
178
|
+
let res = eng.borrow().insert_buf(&table, buf)
|
|
179
|
+
.map(|ids| serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_string()));
|
|
180
|
+
safe_json_result(res.map_err(|e| e))
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
#[napi]
|
|
185
|
+
pub fn jsql_find(table: String, filter_json: String, limit: i32, offset: i32) -> String {
|
|
186
|
+
ENGINE.with(|eng| {
|
|
187
|
+
let filter: Option<HashMap<String, serde_json::Value>> =
|
|
188
|
+
if filter_json.is_empty() { None } else { serde_json::from_str(&filter_json).ok() };
|
|
189
|
+
match eng.borrow().find(&table, &filter, Some(limit as usize), Some(offset as usize), None, &None, &None) {
|
|
190
|
+
Ok((rows, _, _)) => serde_json::to_string(&rows).unwrap_or_else(|_| "[]".to_string()),
|
|
191
|
+
Err(e) => format!(r#"{{"error":"{}"}}"#, e),
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
#[napi]
|
|
197
|
+
pub fn jsql_find_by_id(table: String, id: i64) -> String {
|
|
198
|
+
ENGINE.with(|eng| {
|
|
199
|
+
match eng.borrow().find_by_id_json(&table, id as u64) {
|
|
200
|
+
Ok(Some(s)) => s,
|
|
201
|
+
Ok(None) => "null".to_string(),
|
|
202
|
+
Err(e) => format!(r#"{{"error":"{}"}}"#, e),
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
#[napi]
|
|
210
|
+
pub fn jsql_count(table: String) -> String {
|
|
211
|
+
ENGINE.with(|eng| match eng.borrow().count(&table) {
|
|
212
|
+
Ok(c) => format!("{}", c),
|
|
213
|
+
Err(e) => format!(r#"{{"error":"{}"}}"#, e),
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
#[napi]
|
|
218
|
+
pub fn jsql_update_by_id(table: String, id: i64, data_json: String) -> String {
|
|
219
|
+
ENGINE.with(|eng| {
|
|
220
|
+
let data: HashMap<String, serde_json::Value> = match serde_json::from_str(&data_json) {
|
|
221
|
+
Ok(d) => d,
|
|
222
|
+
Err(e) => return format!(r#"{{"ok":false,"error":"invalid data: {}"}}"#, e),
|
|
223
|
+
};
|
|
224
|
+
match eng.borrow_mut().update_by_id(&table, id as u64, data) {
|
|
225
|
+
Ok(true) => r#"{"ok":true}"#.to_string(),
|
|
226
|
+
Ok(false) => r#"{"ok":false,"error":"not found"}"#.to_string(),
|
|
227
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#[napi]
|
|
233
|
+
pub fn jsql_remove_by_id(table: String, id: i64) -> String {
|
|
234
|
+
ENGINE.with(|eng| match eng.borrow_mut().remove_by_id(&table, id as u64) {
|
|
235
|
+
Ok(true) => r#"{"ok":true}"#.to_string(),
|
|
236
|
+
Ok(false) => r#"{"ok":false,"error":"not found"}"#.to_string(),
|
|
237
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#[napi]
|
|
242
|
+
pub fn jsql_remove_by_ids(table: String, ids_json: String) -> String {
|
|
243
|
+
ENGINE.with(|eng| {
|
|
244
|
+
let ids: Vec<u64> = match serde_json::from_str(&ids_json) {
|
|
245
|
+
Ok(v) => v,
|
|
246
|
+
Err(e) => return format!(r#"{{"error":"invalid ids: {}"}}"#, e),
|
|
247
|
+
};
|
|
248
|
+
match eng.borrow_mut().remove_by_ids(&table, &ids) {
|
|
249
|
+
Ok(n) => format!(r#"{{"ok":true,"count":{}}}"#, n),
|
|
250
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
|
|
251
|
+
}
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
#[napi]
|
|
256
|
+
pub fn jsql_update_by_ids(table: String, batch_json: String) -> String {
|
|
257
|
+
ENGINE.with(|eng| {
|
|
258
|
+
let batch: Vec<(u64, HashMap<String, serde_json::Value>)> = match serde_json::from_str(&batch_json) {
|
|
259
|
+
Ok(v) => v,
|
|
260
|
+
Err(e) => return format!(r#"{{"error":"invalid batch: {}"}}"#, e),
|
|
261
|
+
};
|
|
262
|
+
match eng.borrow_mut().update_by_ids(&table, batch) {
|
|
263
|
+
Ok(n) => format!(r#"{{"ok":true,"count":{}}}"#, n),
|
|
264
|
+
Err(e) => format!(r#"{{"ok":false,"error":"{}"}}"#, e),
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#[napi]
|
|
270
|
+
pub fn jsql_find_by_ids(table: String, ids_json: String) -> String {
|
|
271
|
+
ENGINE.with(|eng| {
|
|
272
|
+
let ids: Vec<u64> = match serde_json::from_str(&ids_json) {
|
|
273
|
+
Ok(v) => v,
|
|
274
|
+
Err(e) => return format!(r#"{{"error":"invalid ids: {}"}}"#, e),
|
|
275
|
+
};
|
|
276
|
+
match eng.borrow().find_by_ids_json(&table, &ids) {
|
|
277
|
+
Ok(s) => s,
|
|
278
|
+
Err(e) => format!(r#"{{"error":"{}"}}"#, e),
|
|
279
|
+
}
|
|
280
|
+
})
|
|
281
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.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
|
+
"bin": {
|
|
7
|
+
"jsql": "./bin/jsql"
|
|
8
|
+
},
|
|
6
9
|
"files": [
|
|
7
10
|
"index.js",
|
|
8
11
|
"lib/",
|
|
9
12
|
"bin/",
|
|
10
13
|
"wasm/",
|
|
14
|
+
"native/",
|
|
15
|
+
"nativesrc/",
|
|
11
16
|
"postinstall.js",
|
|
12
17
|
"README.md",
|
|
13
18
|
"LICENSE"
|
|
@@ -32,5 +37,8 @@
|
|
|
32
37
|
"license": "Apache-2.0",
|
|
33
38
|
"engines": {
|
|
34
39
|
"node": ">=14.0.0"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@vexify-org/yaggs": "^8.1.0"
|
|
35
43
|
}
|
|
36
44
|
}
|
package/wasm/jsql_neo_wasm.d.ts
CHANGED
|
@@ -11,12 +11,20 @@ export function jsql_find(table: string, filter_json: string, limit: number, off
|
|
|
11
11
|
|
|
12
12
|
export function jsql_find_by_id(table: string, id: bigint): string;
|
|
13
13
|
|
|
14
|
+
export function jsql_find_by_ids(table: string, ids_json: string): string;
|
|
15
|
+
|
|
14
16
|
export function jsql_insert(table: string, data_json: string): string;
|
|
15
17
|
|
|
16
18
|
export function jsql_insert_buf(table: string, data: Uint8Array): string;
|
|
17
19
|
|
|
18
20
|
export function jsql_insert_buf_count(table: string, data: Uint8Array): string;
|
|
19
21
|
|
|
22
|
+
export function jsql_insert_json(table: string, json: string): string;
|
|
23
|
+
|
|
20
24
|
export function jsql_remove_by_id(table: string, id: bigint): string;
|
|
21
25
|
|
|
26
|
+
export function jsql_remove_by_ids(table: string, ids_json: string): string;
|
|
27
|
+
|
|
22
28
|
export function jsql_update_by_id(table: string, id: bigint, data_json: string): string;
|
|
29
|
+
|
|
30
|
+
export function jsql_update_by_ids(table: string, batch_json: string): string;
|