jsql-neo 6.8.0 → 6.8.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 +1 -1
- package/index.d.ts +4 -1
- package/index.js +1 -1
- package/lib/client.js +1 -1
- package/lib/database.js +21 -2
- package/lib/mongo_server.js +60 -30
- package/lib/native_client.js +26 -0
- package/lib/objectid.js +22 -1
- package/lib/sql.js +129 -30
- package/lib/table.js +78 -27
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +7 -0
- package/nativesrc/jsql-neo-core/src/engine/memory.rs +5 -0
- package/nativesrc/jsql-neo-core/src/engine/mod.rs +2 -0
- package/nativesrc/jsql-neo-core/src/engine/table.rs +36 -0
- package/nativesrc/jsql-neo-native/src/lib.rs +5 -0
- package/nativesrc/jsql-neo-wasm/src/lib.rs +8 -0
- package/package.json +4 -4
- package/postinstall.js +27 -32
- package/test/issue-118-136.test.js +86 -0
- package/test/issue-127.test.js +38 -0
- package/test/mongo-arrays.test.js +175 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MongoDB 数组字段过滤语义 + find 排序(与上游 dict 的 6 项缺陷对应):
|
|
3
|
+
*
|
|
4
|
+
* 1. find 命令静默忽略 sort —— 排序只在 aggregate 生效,sort+skip/limit 分页取错页。
|
|
5
|
+
* 2. 数组字段无元素匹配语义:{tags:"a"} / $in / $nin 拿整个数组对象比较,全部落空。
|
|
6
|
+
* 3. $ne 用严格 !== :同字节 ObjectId 等值被误判(编译快路径 $eq/$in/$nin 同病)。
|
|
7
|
+
* 4. $all 未实现:mongo_server 里未知操作符 vacuous 通过,table.js 里恒 false。
|
|
8
|
+
* 5. $elemMatch 只认对象元素:标量元素的操作符条件永远不命中。
|
|
9
|
+
* 6. 索引快路径丢行:数组字段的元素命中不在标量索引键里;range 路径还把
|
|
10
|
+
* 空索引命中当「无数据」。schemaless / JSON 列现在跳过索引走全表过滤。
|
|
11
|
+
*
|
|
12
|
+
* 范围声明:null/缺失不参与范围比较(JS 的 null > -1 === true 是陷阱);
|
|
13
|
+
* findAndModify 现在也尊重 doc.sort。
|
|
14
|
+
*/
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const { Database, createMongoServer } = require(path.join(__dirname, '..', 'index.js'));
|
|
17
|
+
const { ObjectId } = require(path.join(__dirname, '..', 'lib', 'objectid.js'));
|
|
18
|
+
|
|
19
|
+
let pass = 0, fail = 0;
|
|
20
|
+
function ok(name, cond, extra) {
|
|
21
|
+
if (cond) { pass++; console.log('[OK] ', name); }
|
|
22
|
+
else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra, (k, v) => v && v.bytes ? v.toString() : v).slice(0, 300) : ''); }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function insert(srv, docs) {
|
|
26
|
+
const r = await srv._dispatch('insert', { insert: 'c', $db: 'test', documents: docs });
|
|
27
|
+
if (r.ok !== 1 || r.n !== docs.length) throw new Error('insert failed: ' + JSON.stringify(r));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function find(srv, filter, extra = {}) {
|
|
31
|
+
const r = await srv._dispatch('find', { find: 'c', $db: 'test', filter, ...extra });
|
|
32
|
+
return r.cursor ? r.cursor.firstBatch : [];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const ids = (batch) => batch.map((d) => d._id).sort((a, b) => a - b);
|
|
36
|
+
|
|
37
|
+
(async () => {
|
|
38
|
+
try {
|
|
39
|
+
/* ---------- 1. find + sort + skip/limit(mongo_server) ---------- */
|
|
40
|
+
{
|
|
41
|
+
const srv = createMongoServer({ port: 0, noAuth: true, dataDir: ':memory:' });
|
|
42
|
+
await (await srv._getEngine('test')).createTable('c', {});
|
|
43
|
+
await insert(srv, [
|
|
44
|
+
{ _id: 1, score: 30, g: 'b' },
|
|
45
|
+
{ _id: 2, score: 10, g: 'a' },
|
|
46
|
+
{ _id: 3, score: 20, g: 'a' },
|
|
47
|
+
{ _id: 4, score: 20, g: 'b' },
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const asc = await find(srv, {}, { sort: { score: 1 } });
|
|
51
|
+
ok('find sort 升序生效', JSON.stringify(asc.map((d) => d.score)) === JSON.stringify([10, 20, 20, 30]), asc.map((d) => d.score));
|
|
52
|
+
|
|
53
|
+
const page = await find(srv, {}, { sort: { score: -1 }, skip: 1, limit: 2 });
|
|
54
|
+
ok('find sort+skip+limit 分页(sort 先于分页)', JSON.stringify(page.map((d) => d.score)) === JSON.stringify([20, 20]), page.map((d) => d.score));
|
|
55
|
+
|
|
56
|
+
const multi = await find(srv, {}, { sort: { g: 1, score: -1 } });
|
|
57
|
+
ok('find 多键排序', JSON.stringify(multi.map((d) => [d.g, d.score])) === JSON.stringify([['a', 20], ['a', 10], ['b', 30], ['b', 20]]), multi.map((d) => [d.g, d.score]));
|
|
58
|
+
|
|
59
|
+
const fam = await srv._dispatch('findAndModify', {
|
|
60
|
+
findAndModify: 'c', $db: 'test', query: {}, sort: { score: -1 }, update: { $set: { top: true } },
|
|
61
|
+
});
|
|
62
|
+
ok('findAndModify 尊重 sort(取最高分)', fam.value && fam.value._id === 1, fam.value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ---------- 2-5. 数组字段过滤语义(mongo_server._match) ---------- */
|
|
66
|
+
{
|
|
67
|
+
const srv = createMongoServer({ port: 0, noAuth: true, dataDir: ':memory:' });
|
|
68
|
+
await (await srv._getEngine('test')).createTable('c', {});
|
|
69
|
+
await insert(srv, [
|
|
70
|
+
{ _id: 1, tags: ['a', 'b'], scores: [5, 15] },
|
|
71
|
+
{ _id: 2, tags: ['b'], scores: [25] },
|
|
72
|
+
{ _id: 3, tags: ['c'], scores: [20], note: 'no arrays on other fields' },
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
ok('{tags:"a"} 元素命中', JSON.stringify(ids(await find(srv, { tags: 'a' }))) === '[1]');
|
|
76
|
+
ok('{tags:{$eq:"b"}} 元素命中', JSON.stringify(ids(await find(srv, { tags: { $eq: 'b' } }))) === '[1,2]');
|
|
77
|
+
ok('{tags:{$in:["a","c"]}}', JSON.stringify(ids(await find(srv, { tags: { $in: ['a', 'c'] } }))) === '[1,3]');
|
|
78
|
+
ok('{tags:{$nin:["b"]}}', JSON.stringify(ids(await find(srv, { tags: { $nin: ['b'] } }))) === '[3]');
|
|
79
|
+
ok('{tags:{$ne:"b"}}', JSON.stringify(ids(await find(srv, { tags: { $ne: 'b' } }))) === '[3]');
|
|
80
|
+
ok('{tags:{$all:["a","b"]}}', JSON.stringify(ids(await find(srv, { tags: { $all: ['a', 'b'] } }))) === '[1]');
|
|
81
|
+
ok('{tags:{$all:["b"]}}', JSON.stringify(ids(await find(srv, { tags: { $all: ['b'] } }))) === '[1,2]');
|
|
82
|
+
ok('{tags:{$gt:"a"}} 元素范围("b">"a"、"c">"a")', JSON.stringify(ids(await find(srv, { tags: { $gt: 'a' } }))) === '[1,2,3]');
|
|
83
|
+
ok('{scores:{$elemMatch:{$gt:10,$lt:25}}} 标量元素', JSON.stringify(ids(await find(srv, { scores: { $elemMatch: { $gt: 10, $lt: 25 } } }))) === '[1,3]');
|
|
84
|
+
// shortcut: 整数组等值({arr:[1,2]} / $in:[[1,2]])仍是引用严格相等,需要时再补深度相等
|
|
85
|
+
ok('{note:{$all:["no arrays on other fields"]}} 标量字段退化等值', JSON.stringify(ids(await find(srv, { note: { $all: ['no arrays on other fields'] } }))) === '[3]');
|
|
86
|
+
|
|
87
|
+
// aggregate $match 复用同一 _match,抽查一个
|
|
88
|
+
const agg = await srv._dispatch('aggregate', {
|
|
89
|
+
aggregate: 'c', $db: 'test', pipeline: [{ $match: { tags: 'a' } }],
|
|
90
|
+
});
|
|
91
|
+
ok('aggregate $match 数组语义', JSON.stringify(ids(agg.cursor.firstBatch)) === '[1]');
|
|
92
|
+
|
|
93
|
+
// null/缺失不参与范围比较:null > -1 在 JS 里是 true,必须挡住
|
|
94
|
+
const nullSrv = createMongoServer({ port: 0, noAuth: true, dataDir: ':memory:' });
|
|
95
|
+
await (await nullSrv._getEngine('test')).createTable('c', {});
|
|
96
|
+
await insert(nullSrv, [{ _id: 1, score: null }, { _id: 2, score: 5 }]);
|
|
97
|
+
ok('{score:{$gt:-1}} 不命中 null', JSON.stringify(ids(await find(nullSrv, { score: { $gt: -1 } }))) === '[2]');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* ---------- 3. $ne 同字节 ObjectId(table.js 编译快路径) ---------- */
|
|
101
|
+
{
|
|
102
|
+
const db = new Database(':memory:');
|
|
103
|
+
await db.createTable('oid', {});
|
|
104
|
+
await db.insert('oid', { _id: 1, ref: new ObjectId('aaaaaaaaaaaaaaaaaaaaaaaa') });
|
|
105
|
+
await db.insert('oid', { _id: 2, ref: new ObjectId('bbbbbbbbbbbbbbbbbbbbbbbb') });
|
|
106
|
+
const rows = await db.find('oid', { ref: { $ne: new ObjectId('aaaaaaaaaaaaaaaaaaaaaaaa') } });
|
|
107
|
+
ok('table $ne:同字节 ObjectId 实例视为相等', JSON.stringify(rows.map((r) => r._id)) === '[2]', rows.map((r) => r._id));
|
|
108
|
+
const eq = await db.find('oid', { ref: new ObjectId('bbbbbbbbbbbbbbbbbbbbbbbb') });
|
|
109
|
+
ok('table 隐式等值:同字节 ObjectId 命中(快路径 valuesEqual)', JSON.stringify(eq.map((r) => r._id)) === '[2]', eq.map((r) => r._id));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/* ---------- 2/4/5/6. table.js 文档 API + 索引正确性 ---------- */
|
|
113
|
+
{
|
|
114
|
+
const db = new Database(':memory:');
|
|
115
|
+
// type 'object':json 类型的字符串值必须是合法 JSON,会拒绝标量 'a';
|
|
116
|
+
// object 类型不做该校验,标量与数组可以共存
|
|
117
|
+
await db.createTable('t', { id: { type: 'integer', primaryKey: true }, tags: { type: 'object' } });
|
|
118
|
+
await db.insert('t', { id: 1, tags: ['a', 'b'] });
|
|
119
|
+
await db.insert('t', { id: 2, tags: 'a' }); // 标量共存:索引命中部分行时数组行不能丢
|
|
120
|
+
await db.insert('t', { id: 3, tags: ['c'] });
|
|
121
|
+
|
|
122
|
+
// 6:先建索引(hash + B-Tree 都过一遍),数组行不得被索引预过滤丢掉
|
|
123
|
+
const table = db._ensureTable('t');
|
|
124
|
+
const withHash = await db.find('t', { tags: 'a' });
|
|
125
|
+
table.createIndex('tags');
|
|
126
|
+
const withHashIdx = await db.find('t', { tags: 'a' });
|
|
127
|
+
table.dropIndex('tags');
|
|
128
|
+
table.createBTreeIndex('tags');
|
|
129
|
+
const withBTree = await db.find('t', { tags: 'a' });
|
|
130
|
+
ok('无索引:{tags:"a"} 命中数组+标量行', JSON.stringify(withHash.map((r) => r.id)) === '[1,2]', withHash.map((r) => r.id));
|
|
131
|
+
ok('hash 索引不丢数组行', JSON.stringify(withHashIdx.map((r) => r.id)) === '[1,2]', withHashIdx.map((r) => r.id));
|
|
132
|
+
ok('B-Tree 索引不丢数组行', JSON.stringify(withBTree.map((r) => r.id)) === '[1,2]', withBTree.map((r) => r.id));
|
|
133
|
+
|
|
134
|
+
const inRows = await db.find('t', { tags: { $in: ['a', 'c'] } });
|
|
135
|
+
ok('table $in 数组语义(B-Tree 在场)', JSON.stringify(inRows.map((r) => r.id).sort()) === '[1,2,3]', inRows.map((r) => r.id));
|
|
136
|
+
|
|
137
|
+
const ninRows = await db.find('t', { tags: { $nin: ['b', 'a'] } });
|
|
138
|
+
ok('table $nin 数组语义', JSON.stringify(ninRows.map((r) => r.id)) === '[3]', ninRows.map((r) => r.id));
|
|
139
|
+
|
|
140
|
+
const allRows = await db.find('t', { tags: { $all: ['a', 'b'] } });
|
|
141
|
+
ok('table $all', JSON.stringify(allRows.map((r) => r.id)) === '[1]', allRows.map((r) => r.id));
|
|
142
|
+
|
|
143
|
+
const emRows = await db.find('t', { tags: { $elemMatch: { $eq: 'c' } } });
|
|
144
|
+
ok('table $elemMatch 标量元素(操作符条件)', JSON.stringify(emRows.map((r) => r.id)) === '[3]', emRows.map((r) => r.id));
|
|
145
|
+
|
|
146
|
+
// 6:JSON 列上的范围查询走全表过滤(B-Tree 范围键只有标量行)
|
|
147
|
+
await db.createTable('r', { id: { type: 'integer', primaryKey: true }, v: { type: 'json' } });
|
|
148
|
+
await db.insert('r', { id: 1, v: [5] });
|
|
149
|
+
await db.insert('r', { id: 2, v: [15] });
|
|
150
|
+
await db.insert('r', { id: 3, v: 10 });
|
|
151
|
+
const rt = db._ensureTable('r');
|
|
152
|
+
rt.createBTreeIndex('v');
|
|
153
|
+
const gt = await db.find('r', { v: { $gt: 6 } });
|
|
154
|
+
ok('JSON 列范围查询索引不丢行(元素+标量都命中)', JSON.stringify(gt.map((x) => x.id).sort()) === '[2,3]', gt.map((x) => x.id));
|
|
155
|
+
|
|
156
|
+
// 标量类型列的索引快路径保持原行为(不受 guard 影响)
|
|
157
|
+
await db.createTable('s', { id: { type: 'integer', primaryKey: true }, n: { type: 'integer' } });
|
|
158
|
+
await db.insert('s', { id: 1, n: 1 });
|
|
159
|
+
await db.insert('s', { id: 2, n: 2 });
|
|
160
|
+
db._ensureTable('s').createBTreeIndex('n');
|
|
161
|
+
const sRows = await db.find('s', { n: { $gte: 2 } });
|
|
162
|
+
ok('标量列 B-Tree 范围仍正确', JSON.stringify(sRows.map((x) => x.id)) === '[2]', sRows.map((x) => x.id));
|
|
163
|
+
|
|
164
|
+
// update/remove 也走 _applyFilterOptimized:数组字段不得漏改漏删
|
|
165
|
+
const upd = await db.update('t', { tags: 'a' }, { $set: { hit: true } });
|
|
166
|
+
ok('update 按数组语义命中所有行', upd === 2, upd);
|
|
167
|
+
}
|
|
168
|
+
} catch (e) {
|
|
169
|
+
fail++;
|
|
170
|
+
console.log('[FAIL] unexpected exception:', e && e.stack || e);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
|
|
174
|
+
if (fail > 0) process.exit(1);
|
|
175
|
+
})();
|