jsql-neo 6.8.27 → 6.10.2
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/README.md +9 -4
- package/bin/jsql-server-control.js +58 -14
- package/index.d.ts +3 -0
- package/index.js +1 -1
- package/lib/bigint_json.js +13 -1
- package/lib/btree.js +12 -3
- package/lib/cache.js +12 -1
- package/lib/database.js +272 -36
- package/lib/jsql_format.js +22 -9
- package/lib/migrate.js +184 -98
- package/lib/mod.js +48 -13
- package/lib/mongo_server.js +652 -246
- package/lib/mysql_compat.js +54 -11
- package/lib/mysql_server.js +248 -65
- package/lib/nedb_compat.js +7 -1
- package/lib/objectid.js +14 -6
- package/lib/pg_server.js +385 -9
- package/lib/plugin.js +34 -3
- package/lib/query.js +12 -6
- package/lib/redis_server.js +102 -42
- package/lib/sql.js +446 -48
- package/lib/table.js +111 -41
- package/lib/tui.js +28 -9
- package/lib/web_ui.js +33 -2
- package/package.json +4 -4
- package/test/alter-table.test.js +11 -5
- package/test/compat-v1.test.js +1 -1
- package/test/extra-coverage.test.js +6 -7
- package/test/issue-258-300.test.js +6 -0
- package/test/issue-312-320.test.js +11 -4
- package/test/issue-326.test.js +42 -0
- package/test/issue-327-418.test.js +1 -1
- package/test/issue-336.test.js +56 -0
- package/test/issue-353.test.js +34 -0
- package/test/issue-356.test.js +27 -0
- package/test/issue-357-365.test.js +1 -1
- package/test/issue-361.test.js +55 -0
- package/test/issue-370.test.js +43 -0
- package/test/issue-373.test.js +40 -0
- package/test/issue-374.test.js +62 -0
- package/test/issue-378.test.js +39 -0
- package/test/issue-379.test.js +39 -0
- package/test/issue-38.test.js +2 -2
- package/test/issue-380.test.js +27 -0
- package/test/issue-383-402.test.js +4 -3
- package/test/issue-384.test.js +41 -0
- package/test/issue-392.test.js +25 -0
- package/test/issue-397.test.js +44 -0
- package/test/issue-403.test.js +47 -0
- package/test/issue-404.test.js +58 -0
- package/test/issue-407.test.js +40 -0
- package/test/issue-408.test.js +92 -0
- package/test/issue-409.test.js +32 -0
- package/test/issue-410.test.js +36 -0
- package/test/issue-411.test.js +38 -0
- package/test/issue-412.test.js +50 -0
- package/test/issue-414.test.js +46 -0
- package/test/issue-417.test.js +56 -0
- package/test/issue-420.test.js +87 -0
- package/test/issue-421.test.js +30 -0
- package/test/issue-422.test.js +33 -0
- package/test/issue-424.test.js +44 -0
- package/test/issue-425.test.js +22 -0
- package/test/issue-426.test.js +22 -0
- package/test/issue-427.test.js +36 -0
- package/test/issue-429.test.js +38 -0
- package/test/issue-431.test.js +41 -0
- package/test/issue-432.test.js +37 -0
- package/test/issue-436.test.js +47 -0
- package/test/issue-445-449.test.js +14 -7
- package/test/issue-455-459.test.js +6 -3
- package/test/issue-474.test.js +59 -0
- package/test/issue-475.test.js +38 -0
- package/test/issue-476.test.js +50 -0
- package/test/issue-477.test.js +54 -0
- package/test/issue-478.test.js +52 -0
- package/test/issue-479.test.js +44 -0
- package/test/issue-480.test.js +42 -0
- package/test/issue-481.test.js +51 -0
- package/test/issue-482.test.js +36 -0
- package/test/issue-483.test.js +57 -0
- package/test/issue-485.test.js +47 -0
- package/test/issue-486.test.js +69 -0
- package/test/issue-487.test.js +54 -0
- package/test/issue-488.test.js +69 -0
- package/test/issue-490.test.js +20 -0
- package/test/issue-491.test.js +21 -0
- package/test/issue-492.test.js +19 -0
- package/test/issue-493.test.js +30 -0
- package/test/issue-494.test.js +82 -0
- package/test/issue-495.test.js +86 -0
- package/test/issue-496.test.js +98 -0
- package/test/issue-497.test.js +29 -0
- package/test/issue-498.test.js +32 -0
- package/test/issue-499.test.js +41 -0
- package/test/issue-500.test.js +39 -0
- package/test/issue-501-506.test.js +29 -0
- package/test/issue-502.test.js +18 -0
- package/test/issue-503.test.js +43 -0
- package/test/issue-504.test.js +83 -0
- package/test/issue-505.test.js +29 -0
- package/test/issue-529.test.js +39 -0
- package/test/issue-530.test.js +69 -0
- package/test/issue-536.test.js +46 -0
- package/test/issue-537.test.js +52 -0
- package/test/issue-539.test.js +65 -0
- package/test/issue-540.test.js +31 -0
- package/test/issue-545.test.js +38 -0
- package/test/issue-546.test.js +68 -0
- package/test/issue-547.test.js +27 -0
- package/test/issue-548.test.js +68 -0
- package/test/issue-549.test.js +26 -0
- package/test/issue-550.test.js +52 -0
- package/test/issue-551.test.js +34 -0
- package/test/issue-552.test.js +30 -0
- package/test/issue-553.test.js +29 -0
- package/test/issue-554.test.js +29 -0
- package/test/issue-555.test.js +22 -0
- package/test/issue-556.test.js +25 -0
- package/test/issue-565.test.js +32 -0
- package/test/issue-566.test.js +30 -0
- package/test/issue-567.test.js +56 -0
- package/test/issue-568.test.js +46 -0
- package/test/issue-569.test.js +25 -0
- package/test/issue-570.test.js +30 -0
- package/test/issue-571.test.js +32 -0
- package/test/issue-572.test.js +64 -0
- package/test/issue-573.test.js +71 -0
- package/test/issue-574.test.js +63 -0
- package/test/issue-581.test.js +46 -0
- package/test/issue-59.test.js +17 -4
- package/test/issue-69-70.test.js +1 -1
- package/test/issue-mysql-harness.js +260 -0
- package/test/issue-pg5-helper.js +135 -0
- package/test/mongo-arrays.test.js +63 -0
- package/test/mongo-objectid.test.js +2 -2
- package/test/mongo-persistence.test.js +3 -3
- package/test/mongo-update-dates.test.js +128 -0
- package/test/wal-recovery.test.js +17 -9
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
|
|
4
4
|
> MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
|
|
5
5
|
|
|
6
|
-
> **v6.
|
|
6
|
+
> **v6.10.2** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|

|
|
@@ -172,9 +172,14 @@ await db.start();
|
|
|
172
172
|
File mode (`new Database('./data/db.json')`) is **not** an in-memory store that only
|
|
173
173
|
writes on exit. Every `insert` / `insertMany` / `update` / `updateById` / `remove` /
|
|
174
174
|
`removeById` / `softDelete` / `truncate` appends one JSONL record to `<file>.wal` and
|
|
175
|
-
|
|
176
|
-
`
|
|
177
|
-
|
|
175
|
+
writes it to the kernel before returning, so a `kill -9` loses nothing that already
|
|
176
|
+
committed. `fsync` is coalesced into a short window (`walSyncInterval`, default 20 ms)
|
|
177
|
+
so bulk single-row writes no longer pay one disk flush per row — set it to `0` for the
|
|
178
|
+
strictest per-write `fsync` behaviour (roughly an order of magnitude slower). Each insert
|
|
179
|
+
still appends its own record; `db.syncWAL()` forces an immediate lightweight `fsync`
|
|
180
|
+
(no snapshot) when you need a durability barrier at an exact point.
|
|
181
|
+
`save()` / `close()` / `stop()` checkpoint the WAL (write a full snapshot, `fsync`, then
|
|
182
|
+
truncate) and always flush any pending window first.
|
|
178
183
|
WAL is on by default for file mode — pass `wal: false` for the old semantics, or tune
|
|
179
184
|
auto-checkpointing with `walCheckpointOps` (default 1000, `0` disables). Directory
|
|
180
185
|
(`mode: 'disk' | 'hybrid'`) and encrypted (`encryptKey`) databases keep it off unless
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const os = require('os');
|
|
7
7
|
const path = require('path');
|
|
8
|
+
const crypto = require('crypto');
|
|
8
9
|
const { spawn } = require('child_process');
|
|
9
10
|
|
|
10
11
|
const STATE_DIR = path.join(os.homedir(), '.jsql');
|
|
@@ -16,15 +17,35 @@ function ensureStateDir() {
|
|
|
16
17
|
fs.mkdirSync(STATE_DIR, { recursive: true, mode: 0o700 });
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
// #537:PID 文件同时保存启动令牌。仅凭数字 PID 判活会在 PID 被复用后
|
|
21
|
+
// 误把无关进程当成 jsql server 并 SIGTERM。
|
|
22
|
+
function readPidInfo() {
|
|
20
23
|
try {
|
|
21
|
-
const
|
|
22
|
-
|
|
24
|
+
const raw = fs.readFileSync(PID_FILE, 'utf8').trim();
|
|
25
|
+
let pid = null, token = null;
|
|
26
|
+
try {
|
|
27
|
+
const parsed = JSON.parse(raw);
|
|
28
|
+
if (parsed && typeof parsed === 'object') {
|
|
29
|
+
pid = parsed.pid;
|
|
30
|
+
if (typeof parsed.token === 'string' && parsed.token) token = parsed.token;
|
|
31
|
+
} else {
|
|
32
|
+
pid = parseInt(raw, 10);
|
|
33
|
+
}
|
|
34
|
+
} catch (_) {
|
|
35
|
+
pid = parseInt(raw, 10);
|
|
36
|
+
}
|
|
37
|
+
if (!Number.isInteger(pid) || pid <= 0) return null;
|
|
38
|
+
return { pid, token };
|
|
23
39
|
} catch (e) {
|
|
24
40
|
return null;
|
|
25
41
|
}
|
|
26
42
|
}
|
|
27
43
|
|
|
44
|
+
function readPid() {
|
|
45
|
+
const info = readPidInfo();
|
|
46
|
+
return info ? info.pid : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
28
49
|
function isAlive(pid) {
|
|
29
50
|
if (!pid) return false;
|
|
30
51
|
try {
|
|
@@ -35,6 +56,26 @@ function isAlive(pid) {
|
|
|
35
56
|
}
|
|
36
57
|
}
|
|
37
58
|
|
|
59
|
+
// 令牌通过环境变量注入子进程;/proc/<pid>/environ 反映进程启动时的环境。
|
|
60
|
+
function hasIdentity(pid, token) {
|
|
61
|
+
if (!token) return false; // 旧格式 PID 文件无法校验身份,保守视为非本服务
|
|
62
|
+
const marker = 'JSQL_SERVER_TOKEN=' + token;
|
|
63
|
+
try {
|
|
64
|
+
return fs.readFileSync('/proc/' + pid + '/environ', 'utf8').split('\0').includes(marker);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
// 非 Linux 环境退而校验命令行是否指向本服务的启动入口
|
|
67
|
+
try {
|
|
68
|
+
const { execFileSync } = require('child_process');
|
|
69
|
+
const cmd = execFileSync('ps', ['-p', String(pid), '-o', 'command='], { encoding: 'utf8' });
|
|
70
|
+
return cmd.includes(path.join(__dirname, 'jsql-server'));
|
|
71
|
+
} catch (_) { return false; }
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isOurServer(info) {
|
|
76
|
+
return !!(info && isAlive(info.pid) && hasIdentity(info.pid, info.token));
|
|
77
|
+
}
|
|
78
|
+
|
|
38
79
|
function isPortOpen(port, host) {
|
|
39
80
|
return new Promise(resolve => {
|
|
40
81
|
const net = require('net');
|
|
@@ -68,9 +109,9 @@ function parseAuthSpecs(list) {
|
|
|
68
109
|
|
|
69
110
|
async function start(opts = {}) {
|
|
70
111
|
ensureStateDir();
|
|
71
|
-
const existing =
|
|
72
|
-
if (
|
|
73
|
-
return `jsql server is already running (pid ${existing}).`;
|
|
112
|
+
const existing = readPidInfo();
|
|
113
|
+
if (isOurServer(existing)) {
|
|
114
|
+
return `jsql server is already running (pid ${existing.pid}).`;
|
|
74
115
|
}
|
|
75
116
|
|
|
76
117
|
const configPath = opts.config || CONF_FILE;
|
|
@@ -94,10 +135,11 @@ async function start(opts = {}) {
|
|
|
94
135
|
|
|
95
136
|
const entry = path.join(__dirname, 'jsql-server');
|
|
96
137
|
const logFd = fs.openSync(LOG_FILE, 'a');
|
|
138
|
+
const token = crypto.randomBytes(16).toString('hex'); // #537:进程身份令牌
|
|
97
139
|
const child = spawn(process.execPath, [entry, '--config', CONF_FILE], {
|
|
98
140
|
detached: true,
|
|
99
141
|
stdio: ['ignore', logFd, logFd],
|
|
100
|
-
env: Object.assign({}, process.env, { JSQL_SERVER_MAIN: '1' }),
|
|
142
|
+
env: Object.assign({}, process.env, { JSQL_SERVER_MAIN: '1', JSQL_SERVER_TOKEN: token }),
|
|
101
143
|
});
|
|
102
144
|
child.once('error', err => {
|
|
103
145
|
fs.closeSync(logFd);
|
|
@@ -105,7 +147,7 @@ async function start(opts = {}) {
|
|
|
105
147
|
});
|
|
106
148
|
child.unref();
|
|
107
149
|
|
|
108
|
-
fs.writeFileSync(PID_FILE,
|
|
150
|
+
fs.writeFileSync(PID_FILE, JSON.stringify({ pid: child.pid, token }), 'utf8');
|
|
109
151
|
|
|
110
152
|
const ready = await waitReady(cfg.port, cfg.host, 5000);
|
|
111
153
|
if (!ready) {
|
|
@@ -131,12 +173,13 @@ function waitReady(port, host, timeoutMs) {
|
|
|
131
173
|
}
|
|
132
174
|
|
|
133
175
|
async function stop(opts = {}) {
|
|
134
|
-
const
|
|
135
|
-
if (!
|
|
136
|
-
|
|
137
|
-
fs.unlinkSync(PID_FILE);
|
|
176
|
+
const info = readPidInfo();
|
|
177
|
+
if (!isOurServer(info)) {
|
|
178
|
+
// #537:PID 复用或旧格式文件都不能作为发信号依据,只清理陈旧文件
|
|
179
|
+
try { fs.unlinkSync(PID_FILE); } catch (_) {}
|
|
138
180
|
return 'jsql server is not running.';
|
|
139
181
|
}
|
|
182
|
+
const pid = info.pid;
|
|
140
183
|
try {
|
|
141
184
|
process.kill(pid, 'SIGTERM');
|
|
142
185
|
} catch (e) {
|
|
@@ -155,10 +198,11 @@ async function stop(opts = {}) {
|
|
|
155
198
|
}
|
|
156
199
|
|
|
157
200
|
async function status(opts = {}) {
|
|
158
|
-
const
|
|
159
|
-
if (!
|
|
201
|
+
const info = readPidInfo();
|
|
202
|
+
if (!isOurServer(info)) {
|
|
160
203
|
return 'jsql server is not running.';
|
|
161
204
|
}
|
|
205
|
+
const pid = info.pid;
|
|
162
206
|
let cfg = {};
|
|
163
207
|
try { cfg = JSON.parse(fs.readFileSync(CONF_FILE, 'utf8')); } catch (e) {}
|
|
164
208
|
const port = (opts.port != null ? opts.port : cfg.port) || 3306;
|
package/index.d.ts
CHANGED
|
@@ -345,6 +345,7 @@ declare namespace JSQLNeo {
|
|
|
345
345
|
versioning?: boolean;
|
|
346
346
|
wal?: boolean;
|
|
347
347
|
walCheckpointOps?: number;
|
|
348
|
+
walSyncInterval?: number;
|
|
348
349
|
isolationLevel?: 'READ_COMMITTED' | 'REPEATABLE_READ';
|
|
349
350
|
slowQueryThreshold?: number;
|
|
350
351
|
fileLock?: boolean;
|
|
@@ -360,6 +361,8 @@ declare namespace JSQLNeo {
|
|
|
360
361
|
constructor(filePath?: string, options?: DatabaseOptions);
|
|
361
362
|
createTable(name: string, schema: Schema): Promise<unknown>;
|
|
362
363
|
insert(table: string, data: Record<string, unknown>): Promise<unknown>;
|
|
364
|
+
/** 轻量持久化屏障:只 fsync 已写入的 WAL 记录,不写全量快照。 */
|
|
365
|
+
syncWAL(): this;
|
|
363
366
|
find(table: string, filter?: Record<string, unknown>): Promise<Row[]>;
|
|
364
367
|
exists(table: string, filter?: Record<string, unknown>): boolean;
|
|
365
368
|
prepare(sql: string): PreparedStatement;
|
package/index.js
CHANGED
package/lib/bigint_json.js
CHANGED
|
@@ -41,7 +41,19 @@ function reviver(_key, value) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
function stringify(value, space) {
|
|
44
|
-
|
|
44
|
+
// 快路径:绝大多数行不含 BigInt / 标记对象。replacer 会对每个属性回调一次,
|
|
45
|
+
// 百万行批量写入时是主要 CPU 开销;这里先走原生无 replacer 序列化,
|
|
46
|
+
// 仅在结果里出现标记(需要 #169 转义)或抛 BigInt TypeError 时回退慢路径。
|
|
47
|
+
try {
|
|
48
|
+
const text = JSON.stringify(value, undefined, space);
|
|
49
|
+
if (typeof text === 'string' && text.indexOf(MARKER) !== -1) {
|
|
50
|
+
return JSON.stringify(value, replacer, space);
|
|
51
|
+
}
|
|
52
|
+
return text;
|
|
53
|
+
} catch (e) {
|
|
54
|
+
if (e instanceof TypeError) return JSON.stringify(value, replacer, space);
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
45
57
|
}
|
|
46
58
|
|
|
47
59
|
function parse(text) {
|
package/lib/btree.js
CHANGED
|
@@ -78,9 +78,18 @@ class BTree {
|
|
|
78
78
|
|
|
79
79
|
_insertNonFull(node, key, rowIndex) {
|
|
80
80
|
if (node.leaf) {
|
|
81
|
-
|
|
81
|
+
// 顺序追加快路径(自增主键 / 已排序批量导入):key 大于当前末键时直接 push,
|
|
82
|
+
// 省掉二分查找和 splice 的返回值分配。批量插入 100 万行时是主要热点。
|
|
83
|
+
const keys = node.keys;
|
|
84
|
+
const last = keys.length - 1;
|
|
85
|
+
if (last < 0 || key > keys[last]) {
|
|
86
|
+
keys.push(key);
|
|
87
|
+
node.values.push([rowIndex]);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const i = this._lowerBound(keys, key);
|
|
82
91
|
|
|
83
|
-
if (i <
|
|
92
|
+
if (i < keys.length && keys[i] === key) {
|
|
84
93
|
if (this._unique) {
|
|
85
94
|
this._size--;
|
|
86
95
|
return;
|
|
@@ -92,7 +101,7 @@ class BTree {
|
|
|
92
101
|
return;
|
|
93
102
|
}
|
|
94
103
|
|
|
95
|
-
|
|
104
|
+
keys.splice(i, 0, key);
|
|
96
105
|
node.values.splice(i, 0, [rowIndex]);
|
|
97
106
|
} else {
|
|
98
107
|
let i = this._route(node, key);
|
package/lib/cache.js
CHANGED
|
@@ -17,12 +17,16 @@ class Cache {
|
|
|
17
17
|
this._startTTLCleaner();
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
set(key, value) {
|
|
20
|
+
set(key, value, ttlMs) {
|
|
21
21
|
const k = String(key);
|
|
22
22
|
this._getData().set(k, String(value));
|
|
23
23
|
// #382:普通 SET 必须清除既有 TTL(Redis 语义),否则新值会继承旧过期时间,
|
|
24
24
|
// 甚至已过期但尚未清理的键被“复活”。
|
|
25
25
|
this._getExpires().delete(k);
|
|
26
|
+
// #503:index.d.ts 声明 set(key, value, ttlMs?),第三参按毫秒设置过期时间。
|
|
27
|
+
if (typeof ttlMs === 'number' && Number.isFinite(ttlMs) && ttlMs > 0) {
|
|
28
|
+
this._getExpires().set(k, Date.now() + ttlMs);
|
|
29
|
+
}
|
|
26
30
|
this._dirty = true;
|
|
27
31
|
return 'OK';
|
|
28
32
|
}
|
|
@@ -122,6 +126,8 @@ class Cache {
|
|
|
122
126
|
}
|
|
123
127
|
_incrby(key, delta) {
|
|
124
128
|
const k = String(key);
|
|
129
|
+
// #417:过期残留键对所有命令不可见,写命令按新键处理
|
|
130
|
+
this._isExpired(k);
|
|
125
131
|
const isInt = (v) => typeof v === 'bigint'
|
|
126
132
|
|| (typeof v === 'number' && Number.isSafeInteger(v))
|
|
127
133
|
|| (typeof v === 'string' && /^[-+]?\d+$/.test(v.trim()));
|
|
@@ -140,6 +146,8 @@ class Cache {
|
|
|
140
146
|
|
|
141
147
|
hset(key, field, value) {
|
|
142
148
|
const k = String(key);
|
|
149
|
+
// #417:先做惰性删除,过期键视同不存在
|
|
150
|
+
this._isExpired(k);
|
|
143
151
|
// #166:_type 是内部类型标记,不能被用户字段覆盖
|
|
144
152
|
if (field === '_type') throw new Error('ERR reserved field name _type');
|
|
145
153
|
let hash = this._getData().get(k);
|
|
@@ -195,6 +203,7 @@ class Cache {
|
|
|
195
203
|
|
|
196
204
|
lpush(key, ...values) {
|
|
197
205
|
const k = String(key);
|
|
206
|
+
this._isExpired(k); // #417
|
|
198
207
|
let list = this._getData().get(k);
|
|
199
208
|
if (list !== undefined && (!Array.isArray(list) || list._type === 'hash')) {
|
|
200
209
|
throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
@@ -206,6 +215,7 @@ class Cache {
|
|
|
206
215
|
}
|
|
207
216
|
rpush(key, ...values) {
|
|
208
217
|
const k = String(key);
|
|
218
|
+
this._isExpired(k); // #417
|
|
209
219
|
let list = this._getData().get(k);
|
|
210
220
|
if (list !== undefined && (!Array.isArray(list) || list._type === 'hash')) {
|
|
211
221
|
throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
@@ -254,6 +264,7 @@ class Cache {
|
|
|
254
264
|
|
|
255
265
|
sadd(key, ...members) {
|
|
256
266
|
const k = String(key);
|
|
267
|
+
this._isExpired(k); // #417
|
|
257
268
|
let set = this._getData().get(k);
|
|
258
269
|
if (set !== undefined && !(set instanceof Set)) {
|
|
259
270
|
throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|