jsql-neo 6.0.0 → 6.0.4

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 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. The browser. **And it fits in one npm package.**
5
5
 
6
- > **v6.0.0** — official release build · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo)
6
+ > **v6.0.1** — official release build · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo)
7
7
 
8
8
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
@@ -124,6 +124,7 @@ For the complete Chinese edition of every topic below, jump to the [中文版](#
124
124
  - [Migration Tools](#migration-tools)
125
125
  - [Storage & Performance](#storage--performance)
126
126
  - [Security](#security)
127
+ - [Plugin System](#plugin-system)
127
128
  - [Errors](#errors)
128
129
  - [TypeScript](#typescript)
129
130
  - [Architecture](#architecture)
@@ -232,7 +233,7 @@ npm install && npm run build # option 3: from source
232
233
  Verify:
233
234
 
234
235
  ```bash
235
- node -e "console.log(require('jsql-neo/package.json').version)" # 6.0.0
236
+ node -e "console.log(require('jsql-neo/package.json').version)" # 6.0.1
236
237
  ```
237
238
 
238
239
  ### 30-second demo
@@ -410,7 +411,7 @@ Auth failure returns the standard `Access denied for user 'x'@'...' (using passw
410
411
 
411
412
  The server answers the system probes ORMs and GUI tools run at connect time:
412
413
 
413
- - `SELECT VERSION()` → `8.0.0-jsql-neo`
414
+ - `SELECT VERSION()` → `8.0.1-jsql-neo`
414
415
  - System variables: `@@version`, `@@version_comment`, …
415
416
  - `SHOW DATABASES / SHOW TABLES / SHOW COLUMNS / SHOW CREATE TABLE`
416
417
  - `information_schema.tables / columns / statistics`
@@ -834,7 +835,7 @@ CREATE TABLE IF NOT EXISTS orders (
834
835
 
835
836
  **SELECT grammar**
836
837
 
837
- ```sql
838
+ ```text
838
839
  WITH [RECURSIVE] cte_name [(col, ...)] AS (SELECT ...)[, ...] -- CTE,见下节
839
840
  SELECT [DISTINCT] select_list
840
841
  FROM table_reference
@@ -1064,7 +1065,7 @@ SELECT AGE('2026-08-12', '2020-01-01'); -- '6 years 7 mons 11 days'
1064
1065
  **Conditional / null** — `IF(cond, a, b)`, `IIF`, `IFNULL(a, b)`, `COALESCE(...)`, `NULLIF`,
1065
1066
  `ISNULL`, `CASE WHEN ... THEN ... ELSE ... END` (search/simple form, works with aggregates).
1066
1067
 
1067
- **System & misc** — `VERSION()` → `8.0.0-jsql-neo`, `DATABASE()`, `USER()`/`CURRENT_USER`,
1068
+ **System & misc** — `VERSION()` → `8.0.1-jsql-neo`, `DATABASE()`, `USER()`/`CURRENT_USER`,
1068
1069
  `LAST_INSERT_ID()`, `ROW_COUNT()`, `CAST`/`CONVERT`, `JSON_EXTRACT(doc, '$.path')`,
1069
1070
  `JSON_OBJECT`/`JSON_ARRAY`, `JSON_UNQUOTE`, `JSON_CONTAINS`, `JSON_SET/INSERT/REPLACE/REMOVE`,
1070
1071
  `UUID()`, `TYPEOF(x)`, `PRINT(x)`.
@@ -1091,28 +1092,34 @@ ORDER BY avg_sal DESC;
1091
1092
 
1092
1093
  ### Operators
1093
1094
 
1094
- **Arithmetic** — `+ - * / % MOD DIV ^` (power, PG style).
1095
+ **Arithmetic** — `+ - * / %`,以及 `MOD` / `DIV` / `POWER()`。
1095
1096
 
1096
1097
  **Comparison** — `= <> != < <= > >=`, `IS NULL`, `IS TRUE/FALSE` (three-valued),
1097
1098
  `BETWEEN a AND b`, `IN (...)`, `LIKE`/`NOT LIKE`, `ILIKE` (case-insensitive),
1098
- `RLIKE`/`REGEXP`, `~ ~* !~ !~*` (PG regex), `EXISTS`, `ANY`/`ALL`/`SOME`, `<=>` (NULL-safe).
1099
+ `RLIKE`/`REGEXP`, `EXISTS`, `= ANY` / `> ALL` / `SOME`.
1099
1100
 
1100
- **Logic** — `AND OR NOT XOR` (short-circuit), MySQL aliases `&&` `||`.
1101
+ **Logic** — `AND OR NOT` (short-circuit).
1101
1102
 
1102
- **Bitwise** — `& | ^ ~ << >>`.
1103
+ **Bitwise** — `& | ^ ~ << >>`(`^` 是按位异或,`~` 是按位取反)。
1103
1104
 
1104
- **JSON (PG style)** — `->` (JSON result), `->>` (text), `#>`, `#>>` (paths), `@>` `<@`
1105
- (containment), `?` `?|` `?&` (key existence).
1105
+ > **运算符方言说明。** `^` 与 `~` 在 MySQL 与 PostgreSQL 中含义冲突,本实现按上下文区分:
1106
+ > - `^` 统一为**按位异或**(MySQL 语义);幂运算请用 `POWER(x, n)`。
1107
+ > - `~` 在**单目**位置是按位取反(MySQL),在**双目**位置是正则匹配(PG),
1108
+ > 另有 `~*`(不敏感)、`!~`、`!~*`。
1109
+ > - 需要正则时也可用 `REGEXP` / `RLIKE`;PG 的 `^`(幂)**暂不支持**。
1110
+
1111
+ **JSON (PG style)** — `->` / `->>`(取字段)、`#>` / `#>>`(路径取值)、`@>` / `<@`(包含)、
1112
+ `?` / `?|` / `?&`(键存在),可链式(`meta->'addr'->>'city'`)。
1106
1113
 
1107
1114
  ### LIKE / ILIKE / regex
1108
1115
 
1109
- - `%` any length, `_` one char, `\` escape (`ESCAPE` clause)
1110
- - `ILIKE` = LIKE, case-insensitive
1111
- - `REGEXP`/`RLIKE`/`~` use the JS RegExp engine; flags `i/m/s`
1116
+ - `%` any length, `_` one char;`ESCAPE '!'` 可自定义转义符(默认 `\`)
1117
+ - `ILIKE` = LIKE,大小写不敏感
1118
+ - `REGEXP` / `RLIKE` 使用 JS RegExp 引擎;flags `i/m/s`
1112
1119
 
1113
1120
  ```sql
1114
1121
  SELECT * FROM users
1115
- WHERE email ~* '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$';
1122
+ WHERE email REGEXP '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$';
1116
1123
  ```
1117
1124
 
1118
1125
  ### Indexes & constraints
@@ -1263,16 +1270,17 @@ const result = await executeSQL(db, sql, params);
1263
1270
  ```js
1264
1271
  {
1265
1272
  columns: ['id', 'name', 'age'],
1266
- columnTypes: ['INTEGER', 'VARCHAR', 'INTEGER'],
1273
+ columnTypes: ['INT', 'VARCHAR(255)', 'INT'],
1267
1274
  rows: [[1, 'Alice', 30]],
1268
- rowCount: 2, affectedRows: 0, insertId: 1,
1269
- message: '2 rows selected', command: 'SELECT',
1275
+ rowCount: 1, affectedRows: 0,
1276
+ message: '1 row selected', command: 'SELECT',
1270
1277
  durationMs: 0.42, warnings: [],
1271
1278
  }
1272
1279
  ```
1273
1280
 
1274
- Result matrix: SELECT → `rows`; INSERT → `affectedRows` + `insertId`; UPDATE/DELETE →
1275
- `affectedRows`; DDL/txn → `message`. Params: positional `?`, named `:name`, or object maps.
1281
+ Result matrix: SELECT → `rows`(另有 `columnTypes`、`rowCount`、`command`、`message`、
1282
+ `durationMs`、`warnings`);INSERT → `affectedRows` + `insertId`(另有 `ids`);
1283
+ UPDATE/DELETE → `affectedRows`;DDL/txn → `message`。 Params: positional `?`, named `:name`, or object maps.
1276
1284
  Multi-statement supported; `SQLExecutor` class runs batched SQL from a string/stream.
1277
1285
 
1278
1286
  #### 参数绑定:两种方式
@@ -1457,7 +1465,7 @@ jsql mod --engine wasm # switch engine (restart required)
1457
1465
 
1458
1466
  ```bash
1459
1467
  $ jsql version
1460
- jsql-neo v6.0.0
1468
+ jsql-neo v6.0.1
1461
1469
  engine: native (napi) | wasm | js
1462
1470
  node: v22.0.0 platform: linux x64
1463
1471
  ```
@@ -1478,7 +1486,7 @@ jsql tui --memory -q # memory mode, quiet
1478
1486
  jsql tui --prompt 'db> ' --no-color
1479
1487
  ```
1480
1488
 
1481
- The status bar shows: `db=<name> dialect=<d> mode=<tui|batch> ver=6.0.0`.
1489
+ The status bar shows: `db=<name> dialect=<d> mode=<tui|batch> ver=6.0.1`.
1482
1490
 
1483
1491
  ### Keyboard shortcuts
1484
1492
 
@@ -1753,6 +1761,105 @@ normalization on data dirs and import/export paths; Web UI db-name whitelist (no
1753
1761
  }
1754
1762
  ```
1755
1763
 
1764
+ ## Plugin System
1765
+
1766
+ JSQL-NEO ships with a unified plugin system that works across all three engine implementations:
1767
+ **Native** (`NativeJSQL`), **WASM** (`JSQL`), and **Pure JS** (`Database`). Full documentation
1768
+ is in [PLUGINS.md](./PLUGINS.md).
1769
+
1770
+ ### Quick start
1771
+
1772
+ ```js
1773
+ const { Database, Plugin } = require('jsql-neo');
1774
+ const db = new Database();
1775
+
1776
+ db.use({
1777
+ name: 'my-plugin',
1778
+ install(db, ctx) {
1779
+ db.myFlag = true;
1780
+ },
1781
+ hooks: {
1782
+ beforeInsert(table, data) { /* modify data */ return data; },
1783
+ afterInsert(table, data, ids) {},
1784
+ beforeUpdate(table, id, data) { return true; }, // false = abort
1785
+ afterUpdate(table, id, data, result) {},
1786
+ beforeDelete(table, id) { return true; },
1787
+ afterDelete(table, id, result) {},
1788
+ beforeFind(table, query) { return true; },
1789
+ afterFind(table, query, result) {},
1790
+ },
1791
+ });
1792
+ ```
1793
+
1794
+ ### Hooks reference (18 unified hooks)
1795
+
1796
+ | Hook | Parameters | Return | Description |
1797
+ |---|---|---|---|
1798
+ | `beforeInsert` | `(table, data[])` | modified data / `false` to abort | pre-insert |
1799
+ | `afterInsert` | `(table, data[], ids)` | — | post-insert |
1800
+ | `beforeUpdate` | `(table, id, data)` | `true`/`false` | pre-update |
1801
+ | `afterUpdate` | `(table, id, data, result)` | — | post-update |
1802
+ | `beforeDelete` | `(table, id)` | `true`/`false` | pre-delete |
1803
+ | `afterDelete` | `(table, id, result)` | — | post-delete |
1804
+ | `beforeFind` | `(table, query)` | `true`/`false` | pre-query |
1805
+ | `afterFind` | `(table, query, result)` | — | post-query |
1806
+ | `beforeCreateTable` | `(name, schema)` | `true`/`false` | pre-DDL |
1807
+ | `afterCreateTable` | `(name, schema)` | — | post-DDL |
1808
+ | `beforeDropTable` | `(name)` | `true`/`false` | pre-drop |
1809
+ | `afterDropTable` | `(name)` | — | post-drop |
1810
+ | `beforeFlush` | `()` | `true`/`false` | pre-flush |
1811
+ | `afterFlush` | `()` | — | post-flush |
1812
+ | `beforeCount` | `(table)` | `true`/`false` | pre-count |
1813
+ | `afterCount` | `(table, count)` | — | post-count |
1814
+ | `onStart` | `()` | — | engine start |
1815
+ | `onStop` | `()` | — | engine stop |
1816
+
1817
+ ### Events
1818
+
1819
+ | Event | Payload |
1820
+ |---|---|
1821
+ | `insert` | `{ table, count, ids }` |
1822
+ | `update` | `{ table, id, data }` |
1823
+ | `delete` | `{ table, ids }` |
1824
+ | `createTable` | `{ name, schema }` |
1825
+ | `dropTable` | `{ name }` |
1826
+ | `start` / `stop` | `{}` |
1827
+
1828
+ ### Plugin helper class
1829
+
1830
+ ```js
1831
+ const timestampPlugin = new Plugin('timestamp')
1832
+ .on('beforeInsert', (table, data) => {
1833
+ data.forEach(r => r.created_at = new Date().toISOString());
1834
+ return data;
1835
+ })
1836
+ .onEvent((ev, payload) => console.log(ev))
1837
+ .install((db, ctx) => {})
1838
+ .build();
1839
+
1840
+ db.use(timestampPlugin);
1841
+ ```
1842
+
1843
+ ### Module system (CLI)
1844
+
1845
+ ```bash
1846
+ jsql mod add --address /path/mod.js # register module
1847
+ jsql mod enable <name> # enable (auto-enables dependencies)
1848
+ jsql mod disable <name> # disable (cascades)
1849
+ jsql mod list | ls [--json] # list
1850
+ ```
1851
+
1852
+ Modules persist to `~/.config/jsql/mod.config`. Circular dependencies are rejected;
1853
+ disabling cascades to dependents.
1854
+
1855
+ > **插件没有沙箱,请只装可信来源的模块。** 两条装载路径都不做隔离:
1856
+ > - `db.use(plugin)` —— 直接把插件对象挂进核心,与核心同权;
1857
+ > - `jsql mod add <dir>` —— 模块文件走普通 `require()`,同样与核心同权,
1858
+ > 可访问 `fs` / `child_process` 等一切 Node API。
1859
+ >
1860
+ > 早期文档曾写"模块文件在 VM 沙箱中执行",实际代码中不存在 vm 隔离,此处已更正。
1861
+ > 需要隔离时请在进程级解决(容器 / 子进程 / 单独的用户与权限)。
1862
+
1756
1863
  ---
1757
1864
 
1758
1865
  ## TypeScript
@@ -2378,7 +2485,7 @@ createMysqlServer({
2378
2485
 
2379
2486
  支持常见系统查询,让 ORM 与 GUI 工具的初始化探测流程正常工作:
2380
2487
 
2381
- - `SELECT VERSION()` → `8.0.0-jsql-neo`
2488
+ - `SELECT VERSION()` → `8.0.1-jsql-neo`
2382
2489
  - `SELECT @@version` / `@@version_comment` 等系统变量
2383
2490
  - `SHOW DATABASES` / `SHOW TABLES` / `SHOW COLUMNS FROM t` / `SHOW CREATE TABLE t`
2384
2491
  - `information_schema.tables` / `information_schema.columns` / `information_schema.statistics`
@@ -2410,7 +2517,7 @@ SELECT TABLE_NAME, TABLE_ROWS FROM information_schema.tables WHERE TABLE_SCHEMA
2410
2517
 
2411
2518
  | 函数 | 行为 |
2412
2519
  |---|---|
2413
- | `VERSION()` | `8.0.0-jsql-neo` |
2520
+ | `VERSION()` | `8.0.1-jsql-neo` |
2414
2521
  | `NOW()` / `CURRENT_TIMESTAMP` | `YYYY-MM-DD HH:MM:SS`(UTC) |
2415
2522
  | `CONCAT(a, b, ...)` | 字符串拼接 |
2416
2523
  | `GROUP_CONCAT(x)` | 聚合拼接(逗号分隔) |
@@ -3031,7 +3138,7 @@ DELETE FROM orders WHERE status = 'cancelled' LIMIT 100;
3031
3138
 
3032
3139
  #### 查询语法 SELECT
3033
3140
 
3034
- ```sql
3141
+ ```text
3035
3142
  SELECT [DISTINCT] select_list
3036
3143
  FROM table_reference
3037
3144
  [JOIN table_reference ON condition]
@@ -3104,26 +3211,28 @@ SELECT * FROM orders LIMIT 40 OFFSET 20; -- 标准偏移语法
3104
3211
  | `BEGIN` / `START TRANSACTION` | 开启事务 |
3105
3212
  | `COMMIT` | 提交 |
3106
3213
  | `ROLLBACK` | 回滚 |
3107
- | `SAVEPOINT sp` / `ROLLBACK TO SAVEPOINT sp` / `RELEASE SAVEPOINT sp` | 保存点 |
3214
+ | `SAVEPOINT sp` / `RELEASE SAVEPOINT sp` | 保存点(**仅登记**,无快照能力) |
3215
+ | `ROLLBACK TO SAVEPOINT sp` | **不支持**——会抛错,不会静默"假成功" |
3108
3216
  | 自动事务 | 无显式事务时单语句原子 |
3109
3217
 
3110
3218
  事务保证:
3111
3219
 
3112
3220
  - 读已提交(read committed)隔离级别的等价语义
3113
3221
  - 事务内修改对事务外不可见(提交前),提交后原子可见
3114
- - 回滚撤销本事务全部修改(含保存点回滚)
3222
+ - 回滚撤销本事务全部修改
3115
3223
  - 支持跨表事务(引擎级提交日志)
3116
3224
 
3117
3225
  ```sql
3118
3226
  BEGIN;
3119
3227
  UPDATE accounts SET balance = balance - 100 WHERE id = 1;
3120
3228
  UPDATE accounts SET balance = balance + 100 WHERE id = 2;
3121
- SAVEPOINT sp1;
3122
- UPDATE accounts SET balance = balance - 100 WHERE id = 3;
3123
- ROLLBACK TO SAVEPOINT sp1; -- 撤销 id=3 的修改
3124
3229
  COMMIT;
3125
3230
  ```
3126
3231
 
3232
+ > **`ROLLBACK TO SAVEPOINT` 目前不可用。** 引擎没有保存点/快照能力,无法只回退到某个
3233
+ > 保存点。这里刻意选择"抛错"而不是"静默成功"——假装回滚成功会让调用方误以为数据已还原。
3234
+ > 需要部分回滚时请用 `ROLLBACK` 放弃整个事务。
3235
+
3127
3236
  #### 其他语句
3128
3237
 
3129
3238
  | 语句 | 说明 |
@@ -3245,7 +3354,7 @@ INTERVAL 单位:`DAY` `HOUR` `MINUTE` `SECOND` `WEEK` `MONTH` `YEAR` 及其组
3245
3354
 
3246
3355
  | 函数 | 说明 | 示例 |
3247
3356
  |---|---|---|
3248
- | `VERSION()` | 服务器版本 | `8.0.0-jsql-neo` |
3357
+ | `VERSION()` | 服务器版本 | `8.0.1-jsql-neo` |
3249
3358
  | `DATABASE()` | 当前库名 | `DATABASE()` |
3250
3359
  | `USER()` / `CURRENT_USER` | 当前用户 | `USER()` → `root` |
3251
3360
  | `LAST_INSERT_ID()` | 最近自增 ID | 配合 AUTO_INCREMENT 使用 |
@@ -3295,7 +3404,6 @@ INTERVAL 单位:`DAY` `HOUR` `MINUTE` `SECOND` `WEEK` `MONTH` `YEAR` 及其组
3295
3404
  | `+` `-` `*` `/` | 四则 | `(a + b) * 2` |
3296
3405
  | `%` / `MOD` | 取模 | `a % 3` |
3297
3406
  | `DIV` | 整数除法(MySQL) | `7 DIV 2` → 3 |
3298
- | `^` | 幂(PG) | `2 ^ 10` → 1024 |
3299
3407
 
3300
3408
  #### 比较运算符
3301
3409
 
@@ -3337,10 +3445,15 @@ INTERVAL 单位:`DAY` `HOUR` `MINUTE` `SECOND` `WEEK` `MONTH` `YEAR` 及其组
3337
3445
  |---|---|---|
3338
3446
  | `->` | 取 JSON 字段(返回 JSON) | `meta->'name'` |
3339
3447
  | `->>` | 取 JSON 字段(返回文本) | `meta->>'name'` |
3340
- | `#>`, `#>>` | 路径访问 | `meta#>>'{a,b}'` |
3448
+ | `#>` | 路径取值(返回 JSON) | `meta#>'{addr,city}'` |
3449
+ | `#>>` | 路径取值(返回文本) | `meta#>>'{addr,city}'` |
3341
3450
  | `@>` | 包含 | `meta @> '{"plan":"pro"}'` |
3342
- | `<@` | 被包含 | `'{"a":1}'::jsonb <@ meta` |
3343
- | `?` / `?|` / `?&` | 键存在 | `meta ? 'plan'` |
3451
+ | `<@` | 被包含 | `'{"a":1}' <@ meta` |
3452
+ | `?` | 含指定键 | `meta ? 'plan'` |
3453
+ | `?\|` | 含任一键 | `meta ?\| '{a,b}'` |
3454
+ | `?&` | 含全部键 | `meta ?& '{a,b}'` |
3455
+
3456
+ 可链式取值:`meta->'addr'->>'city'`。
3344
3457
 
3345
3458
  ### LIKE / ILIKE / 正则
3346
3459
 
@@ -3358,14 +3471,12 @@ INTERVAL 单位:`DAY` `HOUR` `MINUTE` `SECOND` `WEEK` `MONTH` `YEAR` 及其组
3358
3471
 
3359
3472
  - 基于 JS RegExp 引擎
3360
3473
  - 支持 flags:`i`(忽略大小写)、`m`(多行)、`s`(点匹配换行)
3361
- - PG 风格的 `~` / `~*` / `!~` / `!~*` 运算符也支持:
3362
- - `name ~ '^A'`(大小写敏感匹配)
3363
- - `name ~* '^a'`(不敏感)
3364
- - `name !~ '^X'`(不匹配)
3474
+ - PG 风格的 `~`(大小写敏感)/ `~*`(不敏感)/ `!~` / `!~*` 双目运算符也支持;
3475
+ 单目位置的 `~` 仍是按位取反。
3365
3476
 
3366
3477
  ```sql
3367
3478
  SELECT * FROM users
3368
- WHERE email ~* '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$';
3479
+ WHERE email REGEXP '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$';
3369
3480
  ```
3370
3481
 
3371
3482
  ### 事务 Transactions
@@ -4042,7 +4153,7 @@ Data dir: /root/.jsql-neo/data
4042
4153
 
4043
4154
  ```bash
4044
4155
  $ jsql version
4045
- jsql-neo v6.0.0
4156
+ jsql-neo v6.0.1
4046
4157
  engine: native (napi) | wasm | js
4047
4158
  node: v22.0.0
4048
4159
  platform: linux x64
@@ -4711,7 +4822,7 @@ jsql-neo/
4711
4822
  ├── index.js # ★ 公共 API 出口
4712
4823
  ├── index.d.ts # TypeScript 声明
4713
4824
  ├── test/ # 测试套件
4714
- ├── docs/ # 文档(本文件即索引)
4825
+ ├── PLUGINS.md # 插件与模块文档(与 README 同级,无 docs/ 目录)
4715
4826
  ├── src/rust/ # Rust 引擎源码(native + wasm)
4716
4827
  ├── data/ # 默认数据目录(运行时创建)
4717
4828
  └── package.json
@@ -4902,7 +5013,7 @@ npm run typecheck
4902
5013
  - `src/rust/`:Rust 引擎(native + wasm 同源)
4903
5014
  - `lib/`:Node 侧全部逻辑(协议/解析/TUI/兼容层)
4904
5015
  - `test/`:单元 + 集成测试
4905
- - `docs/`:扩展文档
5016
+ - 扩展文档位于仓库根目录:`PLUGINS.md`(插件与模块)。本仓库**没有** `docs/` 目录。
4906
5017
 
4907
5018
  ---
4908
5019
 
@@ -5119,7 +5230,7 @@ Apache License
5119
5230
 
5120
5231
  *JSQL-NEO — One engine to rule them all. MySQL. PostgreSQL. MongoDB. Redis. SQL. TypeScript. The browser.*
5121
5232
 
5122
- *文档版本:v6.0.0 · 最后更新:2026-09-26*
5233
+ *文档版本:v6.0.1 · 最后更新:2026-09-27*
5123
5234
 
5124
5235
  ---
5125
5236
 
@@ -5692,7 +5803,8 @@ expr := literal
5692
5803
  | expr [NOT] LIKE pattern [ESCAPE char]
5693
5804
  | expr [NOT] ILIKE pattern
5694
5805
  | expr [NOT] RLIKE pattern | expr [NOT] REGEXP pattern
5695
- | expr [NOT] ~ pattern | expr [NOT] ~* pattern | expr !~ pattern
5806
+ | expr ~ pattern | expr ~* pattern | expr !~ pattern | expr !~* pattern
5807
+ | expr ? key | expr ?| keys | expr ?& keys
5696
5808
  | expr IS [NOT] NULL | expr IS [NOT] TRUE | expr IS [NOT] FALSE
5697
5809
  | expr [NOT] IN ( ... )
5698
5810
  | expr [NOT] ANY '(' select_statement ')'
@@ -5738,16 +5850,17 @@ function_call := func_name '(' [DISTINCT] args ')' -- 普通/聚合
5738
5850
  ### 运算符优先级(从高到低)
5739
5851
 
5740
5852
  ```
5741
- 1. () . [] -> ->> #> #>>
5742
- 2. :: CAST 一元 + - ~
5853
+ 1. () . [] -> ->> #> #>> ::
5854
+ 2. CAST 一元 + - ~
5743
5855
  3. ^ * / % DIV MOD
5744
5856
  4. + -
5745
5857
  5. << >> & | ^(位)
5746
- 6. = <> != < <= > >= <=> BETWEEN IN LIKE ILIKE RLIKE REGEXP ~ ~* @> <@ ?
5858
+ 6. = <> != < <= > >= <=> @> <@ ? ?| ?& BETWEEN IN LIKE ILIKE RLIKE REGEXP ~ ~* !~ !~*
5747
5859
  7. NOT
5748
- 8. AND
5749
- 9. OR XOR
5750
- 10. 三元(IF / CASE 解析为函数)
5860
+ 8. AND &&
5861
+ 9. XOR
5862
+ 10. OR ||
5863
+ 11. 三元(IF / CASE 解析为函数)
5751
5864
  ```
5752
5865
 
5753
5866
  ### 关键词语法表
@@ -6594,7 +6707,7 @@ FROM orders;
6594
6707
  #### 会话信息
6595
6708
 
6596
6709
  ```sql
6597
- SELECT VERSION(); -- '8.0.0-jsql-neo'
6710
+ SELECT VERSION(); -- '8.0.1-jsql-neo'
6598
6711
  SELECT DATABASE(); -- 'app'
6599
6712
  SELECT USER(); -- 'root@localhost'
6600
6713
  SELECT CURRENT_USER; -- 'root@localhost'
@@ -6889,7 +7002,7 @@ Usage: jsql version
6889
7002
 
6890
7003
  输出版本与环境信息:
6891
7004
 
6892
- jsql-neo v6.0.0
7005
+ jsql-neo v6.0.1
6893
7006
  engine: native (napi) | wasm | js
6894
7007
  node: v22.0.0
6895
7008
  platform: linux x64
@@ -7344,7 +7457,7 @@ jsql import ./data app.sql
7344
7457
  | 字符串字面量 | 单引号 | 单引号 | 单引号(双引号按标识符,PG 语义) |
7345
7458
  | 布尔 | `TRUE/FALSE`(1/0) | `t/f`(三值逻辑) | 兼容两者 |
7346
7459
  | 分页 | `LIMIT off, n` | `LIMIT n OFFSET off` | 两者 |
7347
- | JSON 访问 | `JSON_EXTRACT` | `->` `->>` | 两者 + `#>>` |
7460
+ | JSON 访问 | `JSON_EXTRACT` | `->` `->>` `#>>` | 两者 |
7348
7461
  | 类型转换 | `CAST`/`CONVERT` | `::` | 三者 |
7349
7462
  | 条件分支 | `IF()` | `CASE` | `IF` + `CASE` + `IIF` |
7350
7463
  | 空值回退 | `IFNULL()` | `COALESCE` | 两者 |
@@ -7545,7 +7658,7 @@ CI(GitHub Actions)矩阵:`node 20/22` × `linux/macos/windows` × `native/
7545
7658
 
7546
7659
  *JSQL-NEO — One engine to rule them all. MySQL. PostgreSQL. MongoDB. Redis. SQL. TypeScript. The browser.*
7547
7660
 
7548
- *文档版本:v5.4.0 · 共 19 个附录 · 最后更新:2026-08-12*
7661
+ *文档版本:v6.0.1 · 共 19 个附录 · 最后更新:2026-09-27*
7549
7662
 
7550
7663
  ---
7551
7664
 
@@ -8152,4 +8265,4 @@ npm test
8152
8265
 
8153
8266
  *JSQL-NEO — One engine to rule them all. MySQL. PostgreSQL. MongoDB. Redis. SQL. TypeScript. The browser.*
8154
8267
 
8155
- *文档版本:v5.4.0 · 附录 A–Z · 全文 6000+ 行 · 最后更新:2026-08-12*
8268
+ *文档版本:v6.0.1 · 附录 A–Z · 全文 6000+ 行 · 最后更新:2026-09-27*
@@ -17,6 +17,33 @@ function safeParse(str, fallback) {
17
17
  try { return JSON.parse(str); } catch (e) { return fallback; }
18
18
  }
19
19
 
20
+ /**
21
+ * 序列化对象/数组字段(JSON 列)并给出可读错误。直接 JSON.stringify 遇到循环引用会抛
22
+ * "Converting circular structure to JSON",指向不到具体列,这里补上表/列信息。
23
+ * 与 lib/wasm_client.js 的 safeStringify 保持一致。
24
+ */
25
+ function safeStringify(value, table, column) {
26
+ try {
27
+ return JSON.stringify(value);
28
+ } catch (e) {
29
+ const msg = (e && e.message) ? e.message : String(e);
30
+ throw new Error(`Cannot serialize column '${column}' of table '${table}': ${msg}`);
31
+ }
32
+ }
33
+
34
+ /** 插入行必须是普通对象;null/undefined/数组/基本类型在这里就被拦下(与 lib/wasm_client.js 一致) */
35
+ function assertRowObject(row, index, isBatch) {
36
+ const ok = row !== null && row !== undefined && typeof row === 'object' &&
37
+ !Array.isArray(row) && !(row instanceof Date);
38
+ if (ok) return;
39
+ const got = row === null ? 'null'
40
+ : Array.isArray(row) ? 'array'
41
+ : row instanceof Date ? 'Date'
42
+ : typeof row;
43
+ const where = isBatch ? ` at index ${index}` : '';
44
+ throw new TypeError(`insert(): expected a row object${where}, got ${got}`);
45
+ }
46
+
20
47
  const NATIVE_TYPE_MAP = {
21
48
  text: 'string',
22
49
  varchar: 'string',
@@ -399,8 +426,17 @@ class JSQL {
399
426
  rows = rows.map(row => {
400
427
  const out = {};
401
428
  for (const [k, v] of Object.entries(row)) {
402
- if (v instanceof Date) out[k] = v.toISOString();
403
- else out[k] = v;
429
+ const def = schema[k];
430
+ let t = null;
431
+ if (typeof def === 'string') t = def.toLowerCase();
432
+ else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
433
+ if (v !== null && v !== undefined && ['json', 'array', 'object'].includes(t) && typeof v !== 'string') {
434
+ out[k] = safeStringify(v, table, k);
435
+ } else if (v instanceof Date) {
436
+ out[k] = v.toISOString();
437
+ } else {
438
+ out[k] = v;
439
+ }
404
440
  }
405
441
  return out;
406
442
  });
@@ -450,18 +486,39 @@ class JSQL {
450
486
  async _flush() {
451
487
  this._flushOpsNow();
452
488
  if (!this._runHooks('beforeFlush', [])) return null;
453
- const flushed = {};
454
- for (const [table, rows] of Object.entries(this._buffer)) {
455
- if (rows.length === 0) continue;
456
- const r = await this._insertBatch(table, rows);
457
- flushed[table] = r;
458
- }
489
+ // 先把整个 buffer 原子取出再写入。
490
+ // 旧写法是遍历 this._buffer、await 之后才清空 —— 期间其他协程继续往同一个数组
491
+ // push,同一批行会被多个 flush 重复写入(并发 insert 行数暴涨/丢行)。
492
+ // 与 lib/wasm_client.js 的修法保持一致。
493
+ const pending = this._buffer;
459
494
  this._buffer = {};
460
495
  this._bufferSize = 0;
496
+ const flushed = {};
497
+ try {
498
+ for (const [table, rows] of Object.entries(pending)) {
499
+ if (rows.length === 0) continue;
500
+ flushed[table] = await this._insertBatch(table, rows);
501
+ }
502
+ } catch (e) {
503
+ // 坏行不回写 buffer:否则 stop() 二次 flush 会再抛同一个错导致进程 exit 1
504
+ if (!this._flushErrors) this._flushErrors = [];
505
+ this._flushErrors.push({
506
+ at: Date.now(),
507
+ message: (e && e.message) ? e.message : String(e),
508
+ tables: Object.keys(pending),
509
+ rowCounts: Object.fromEntries(Object.entries(pending).map(([t, r]) => [t, r.length])),
510
+ });
511
+ throw e;
512
+ }
461
513
  this._runHooks('afterFlush', []);
462
514
  return flushed;
463
515
  }
464
516
 
517
+ /** 返回历次 flush 失败的记录(坏行不会回写 buffer,只能从这里查) */
518
+ lastFlushErrors() {
519
+ return (this._flushErrors || []).slice();
520
+ }
521
+
465
522
  _flushOpsNow() {
466
523
  if (this._opTimer) {
467
524
  clearTimeout(this._opTimer);
@@ -479,7 +536,11 @@ class JSQL {
479
536
  }
480
537
 
481
538
  async insert(table, data) {
539
+ // 入参校验:null / undefined / 非对象会在下游 Object.entries() 处炸成
540
+ // 难以定位的 "Cannot convert undefined or null to object",这里直接给明确 TypeError
541
+ // (与 lib/wasm_client.js 的 assertRowObject 保持一致)
482
542
  const arr = Array.isArray(data) ? data : [data];
543
+ arr.forEach((row, i) => assertRowObject(row, i, arr.length > 1 || Array.isArray(data)));
483
544
  var filtered = arr;
484
545
  if (!this._runHooks('beforeInsert', [table, filtered])) return [];
485
546
  if (arr.length > 1) {
package/lib/pg_server.js CHANGED
@@ -861,4 +861,10 @@ class PgServer {
861
861
  }
862
862
  }
863
863
 
864
- module.exports = { PgServer, PgConnection, buildRowDescription, buildDataRow, buildCommandComplete, buildErrorResponse, buildReadyForQuery, buildParameterStatus, buildBackendKeyData, buildParseComplete, buildBindComplete, buildCloseComplete, buildNoData, buildParameterDesc, TYPE_OIDS, pgTypeOid, pgFormatValue };
864
+ // README 的 createXxxServer 工厂系列里,PG 这个之前漏了实现:
865
+ // index.js 一直从本模块解构 createPgServer,实际拿到 undefined。
866
+ function createPgServer(options) {
867
+ return new PgServer(options || {});
868
+ }
869
+
870
+ module.exports = { PgServer, createPgServer, PgConnection, buildRowDescription, buildDataRow, buildCommandComplete, buildErrorResponse, buildReadyForQuery, buildParameterStatus, buildBackendKeyData, buildParseComplete, buildBindComplete, buildCloseComplete, buildNoData, buildParameterDesc, TYPE_OIDS, pgTypeOid, pgFormatValue };