monsqlize 1.3.0 → 2.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/CHANGELOG.md +56 -60
- package/LICENSE +201 -21
- package/README.md +537 -1828
- package/changelogs/README.md +160 -0
- package/changelogs/v2.0.0.md +222 -0
- package/dist/cjs/index.cjs +10600 -0
- package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
- package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
- package/dist/cjs/transaction/Transaction.cjs +158 -0
- package/dist/cjs/transaction/TransactionManager.cjs +298 -0
- package/dist/esm/index.mjs +10650 -0
- package/dist/types/base.d.ts +81 -0
- package/dist/types/collection.d.ts +1031 -0
- package/dist/types/expression.d.ts +115 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/lock.d.ts +74 -0
- package/dist/types/model.d.ts +526 -0
- package/dist/types/mongodb.d.ts +49 -0
- package/dist/types/monsqlize.d.ts +491 -0
- package/dist/types/pool.d.ts +84 -0
- package/dist/types/runtime.d.ts +362 -0
- package/dist/types/saga.d.ts +143 -0
- package/dist/types/slow-query-log.d.ts +126 -0
- package/dist/types/sync.d.ts +103 -0
- package/dist/types/transaction.d.ts +132 -0
- package/package.json +67 -69
- package/index.d.ts +0 -206
- package/index.mjs +0 -52
- package/lib/cache-invalidation.js +0 -279
- package/lib/cache.js +0 -530
- package/lib/common/cursor.js +0 -59
- package/lib/common/docs-urls.js +0 -73
- package/lib/common/index-options.js +0 -223
- package/lib/common/log.js +0 -61
- package/lib/common/namespace.js +0 -22
- package/lib/common/normalize.js +0 -34
- package/lib/common/page-result.js +0 -43
- package/lib/common/runner.js +0 -57
- package/lib/common/server-features.js +0 -232
- package/lib/common/shape-builders.js +0 -27
- package/lib/common/validation.js +0 -113
- package/lib/connect.js +0 -99
- package/lib/constants.js +0 -55
- package/lib/count-queue.js +0 -188
- package/lib/distributed-cache-invalidator.js +0 -260
- package/lib/errors.js +0 -168
- package/lib/expression/cache/ExpressionCache.js +0 -114
- package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
- package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
- package/lib/expression/detector.js +0 -84
- package/lib/expression/factory.js +0 -29
- package/lib/expression/index.js +0 -19
- package/lib/function-cache.js +0 -533
- package/lib/index.js +0 -1251
- package/lib/infrastructure/ConnectionPoolManager.js +0 -464
- package/lib/infrastructure/HealthChecker.js +0 -281
- package/lib/infrastructure/PoolConfig.js +0 -199
- package/lib/infrastructure/PoolSelector.js +0 -225
- package/lib/infrastructure/PoolStats.js +0 -244
- package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
- package/lib/infrastructure/ssh-tunnel.js +0 -41
- package/lib/infrastructure/uri-parser.js +0 -36
- package/lib/lock/Lock.js +0 -67
- package/lib/lock/errors.js +0 -28
- package/lib/lock/index.js +0 -13
- package/lib/logger.js +0 -225
- package/lib/model/examples/test.js +0 -311
- package/lib/model/features/defaults.js +0 -161
- package/lib/model/features/populate.js +0 -568
- package/lib/model/features/relations.js +0 -120
- package/lib/model/features/soft-delete.js +0 -349
- package/lib/model/features/version.js +0 -157
- package/lib/model/features/virtuals.js +0 -219
- package/lib/model/index.js +0 -1265
- package/lib/mongodb/common/accessor-helpers.js +0 -59
- package/lib/mongodb/common/agg-pipeline.js +0 -36
- package/lib/mongodb/common/aggregation-validator.js +0 -127
- package/lib/mongodb/common/iid.js +0 -28
- package/lib/mongodb/common/lexicographic-expr.js +0 -53
- package/lib/mongodb/common/shape.js +0 -32
- package/lib/mongodb/common/sort.js +0 -39
- package/lib/mongodb/common/transaction-aware.js +0 -25
- package/lib/mongodb/connect.js +0 -234
- package/lib/mongodb/index.js +0 -639
- package/lib/mongodb/management/admin-ops.js +0 -200
- package/lib/mongodb/management/bookmark-ops.js +0 -167
- package/lib/mongodb/management/cache-ops.js +0 -50
- package/lib/mongodb/management/collection-ops.js +0 -387
- package/lib/mongodb/management/database-ops.js +0 -202
- package/lib/mongodb/management/index-ops.js +0 -475
- package/lib/mongodb/management/index.js +0 -17
- package/lib/mongodb/management/namespace.js +0 -31
- package/lib/mongodb/management/validation-ops.js +0 -268
- package/lib/mongodb/queries/aggregate.js +0 -172
- package/lib/mongodb/queries/chain.js +0 -631
- package/lib/mongodb/queries/count.js +0 -99
- package/lib/mongodb/queries/distinct.js +0 -78
- package/lib/mongodb/queries/find-and-count.js +0 -193
- package/lib/mongodb/queries/find-by-ids.js +0 -236
- package/lib/mongodb/queries/find-one-by-id.js +0 -171
- package/lib/mongodb/queries/find-one.js +0 -71
- package/lib/mongodb/queries/find-page.js +0 -618
- package/lib/mongodb/queries/find.js +0 -171
- package/lib/mongodb/queries/index.js +0 -51
- package/lib/mongodb/queries/watch.js +0 -538
- package/lib/mongodb/writes/common/batch-retry.js +0 -65
- package/lib/mongodb/writes/delete-batch.js +0 -323
- package/lib/mongodb/writes/delete-many.js +0 -181
- package/lib/mongodb/writes/delete-one.js +0 -173
- package/lib/mongodb/writes/find-one-and-delete.js +0 -203
- package/lib/mongodb/writes/find-one-and-replace.js +0 -239
- package/lib/mongodb/writes/find-one-and-update.js +0 -240
- package/lib/mongodb/writes/increment-one.js +0 -259
- package/lib/mongodb/writes/index.js +0 -46
- package/lib/mongodb/writes/insert-batch.js +0 -508
- package/lib/mongodb/writes/insert-many.js +0 -223
- package/lib/mongodb/writes/insert-one.js +0 -169
- package/lib/mongodb/writes/replace-one.js +0 -226
- package/lib/mongodb/writes/result-handler.js +0 -237
- package/lib/mongodb/writes/update-batch.js +0 -416
- package/lib/mongodb/writes/update-many.js +0 -275
- package/lib/mongodb/writes/update-one.js +0 -273
- package/lib/mongodb/writes/upsert-one.js +0 -203
- package/lib/multi-level-cache.js +0 -244
- package/lib/operators.js +0 -330
- package/lib/redis-cache-adapter.js +0 -267
- package/lib/saga/SagaContext.js +0 -67
- package/lib/saga/SagaDefinition.js +0 -32
- package/lib/saga/SagaExecutor.js +0 -201
- package/lib/saga/SagaOrchestrator.js +0 -186
- package/lib/saga/index.js +0 -11
- package/lib/slow-query-log/base-storage.js +0 -70
- package/lib/slow-query-log/batch-queue.js +0 -97
- package/lib/slow-query-log/config-manager.js +0 -196
- package/lib/slow-query-log/index.js +0 -238
- package/lib/slow-query-log/mongodb-storage.js +0 -324
- package/lib/slow-query-log/query-hash.js +0 -39
- package/lib/sync/ChangeStreamSyncManager.js +0 -405
- package/lib/sync/ResumeTokenStore.js +0 -192
- package/lib/sync/SyncConfig.js +0 -127
- package/lib/sync/SyncTarget.js +0 -240
- package/lib/sync/index.js +0 -20
- package/lib/transaction/CacheLockManager.js +0 -162
- package/lib/transaction/DistributedCacheLockManager.js +0 -475
- package/lib/transaction/Transaction.js +0 -315
- package/lib/transaction/TransactionManager.js +0 -267
- package/lib/transaction/index.js +0 -11
- package/lib/utils/objectid-converter.js +0 -632
- package/types/README.md +0 -122
- package/types/base.ts +0 -94
- package/types/batch.ts +0 -187
- package/types/cache.ts +0 -71
- package/types/chain.ts +0 -254
- package/types/collection.ts +0 -357
- package/types/expression.ts +0 -109
- package/types/function-cache.d.ts +0 -135
- package/types/lock.ts +0 -95
- package/types/model/definition.ts +0 -152
- package/types/model/index.ts +0 -10
- package/types/model/instance.ts +0 -121
- package/types/model/relations.ts +0 -121
- package/types/model/virtuals.ts +0 -32
- package/types/monsqlize.ts +0 -245
- package/types/options.ts +0 -192
- package/types/pagination.ts +0 -154
- package/types/pool.ts +0 -125
- package/types/query.ts +0 -71
- package/types/saga.ts +0 -125
- package/types/stream.ts +0 -64
- package/types/sync.ts +0 -79
- package/types/transaction.ts +0 -79
- package/types/write.ts +0 -77
|
@@ -1,531 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 表达式编译器扩展方法 - 所有未支持功能实现
|
|
3
|
-
* v1.1.0 新增44个操作符的完整实现
|
|
4
|
-
*
|
|
5
|
-
* @module ExpressionCompilerExtensions
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 日期扩展函数(8个)
|
|
10
|
-
* DATE_FROM_PARTS, DATE_TO_PARTS, ISO_WEEK, ISO_WEEK_YEAR, ISO_DAY_OF_WEEK, DAY_OF_WEEK, DAY_OF_YEAR, WEEK
|
|
11
|
-
*/
|
|
12
|
-
function _compileDateExtendedFunction(funcName, argsStr, targetDB) {
|
|
13
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
14
|
-
|
|
15
|
-
switch (funcName) {
|
|
16
|
-
case 'DATE_FROM_PARTS': {
|
|
17
|
-
// DATE_FROM_PARTS(year, month, day [, hour, minute, second])
|
|
18
|
-
if (args.length < 3) {
|
|
19
|
-
throw new Error('DATE_FROM_PARTS requires at least 3 arguments: year, month, day');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const result = {
|
|
23
|
-
$dateFromParts: {
|
|
24
|
-
year: this._parseValue(args[0]),
|
|
25
|
-
month: this._parseValue(args[1]),
|
|
26
|
-
day: this._parseValue(args[2])
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
if (args[3]) result.$dateFromParts.hour = this._parseValue(args[3]);
|
|
31
|
-
if (args[4]) result.$dateFromParts.minute = this._parseValue(args[4]);
|
|
32
|
-
if (args[5]) result.$dateFromParts.second = this._parseValue(args[5]);
|
|
33
|
-
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
case 'DATE_TO_PARTS': {
|
|
38
|
-
// DATE_TO_PARTS(date)
|
|
39
|
-
if (args.length !== 1) {
|
|
40
|
-
throw new Error('DATE_TO_PARTS requires 1 argument: date');
|
|
41
|
-
}
|
|
42
|
-
return { $dateToParts: { date: this._parseValue(args[0]) } };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
case 'ISO_WEEK': {
|
|
46
|
-
return { $isoWeek: this._parseValue(args[0]) };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
case 'ISO_WEEK_YEAR': {
|
|
50
|
-
return { $isoWeekYear: this._parseValue(args[0]) };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
case 'ISO_DAY_OF_WEEK': {
|
|
54
|
-
return { $isoDayOfWeek: this._parseValue(args[0]) };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
case 'DAY_OF_WEEK': {
|
|
58
|
-
return { $dayOfWeek: this._parseValue(args[0]) };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
case 'DAY_OF_YEAR': {
|
|
62
|
-
return { $dayOfYear: this._parseValue(args[0]) };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
case 'WEEK': {
|
|
66
|
-
return { $week: this._parseValue(args[0]) };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
default:
|
|
70
|
-
throw new Error(`Unsupported date extended function: ${funcName}`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 类型转换函数(7个)
|
|
76
|
-
* TO_BOOL, TO_DATE, TO_DOUBLE, TO_DECIMAL, TO_LONG, TO_OBJECT_ID, CONVERT
|
|
77
|
-
*/
|
|
78
|
-
function _compileTypeConversionFunction(funcName, argsStr, targetDB) {
|
|
79
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
80
|
-
|
|
81
|
-
switch (funcName) {
|
|
82
|
-
case 'TO_BOOL': {
|
|
83
|
-
return { $toBool: this._parseValue(args[0]) };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
case 'TO_DATE': {
|
|
87
|
-
return { $toDate: this._parseValue(args[0]) };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
case 'TO_DOUBLE': {
|
|
91
|
-
return { $toDouble: this._parseValue(args[0]) };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
case 'TO_DECIMAL': {
|
|
95
|
-
return { $toDecimal: this._parseValue(args[0]) };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
case 'TO_LONG': {
|
|
99
|
-
return { $toLong: this._parseValue(args[0]) };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
case 'TO_OBJECT_ID': {
|
|
103
|
-
return { $toObjectId: this._parseValue(args[0]) };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
case 'CONVERT': {
|
|
107
|
-
// CONVERT(value, toType [, onError, onNull])
|
|
108
|
-
if (args.length < 2) {
|
|
109
|
-
throw new Error('CONVERT requires at least 2 arguments: value, toType');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const result = {
|
|
113
|
-
$convert: {
|
|
114
|
-
input: this._parseValue(args[0]),
|
|
115
|
-
to: args[1].replace(/['"]/g, '')
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
if (args[2]) result.$convert.onError = this._parseValue(args[2]);
|
|
120
|
-
if (args[3]) result.$convert.onNull = this._parseValue(args[3]);
|
|
121
|
-
|
|
122
|
-
return result;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
default:
|
|
126
|
-
throw new Error(`Unsupported type conversion function: ${funcName}`);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 数组扩展函数(4个)
|
|
132
|
-
* REDUCE, ZIP, REVERSE_ARRAY, RANGE
|
|
133
|
-
*/
|
|
134
|
-
function _compileArrayExtendedFunction(funcName, argsStr, targetDB) {
|
|
135
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
136
|
-
|
|
137
|
-
switch (funcName) {
|
|
138
|
-
case 'REDUCE': {
|
|
139
|
-
// REDUCE(array, initialValue, expression)
|
|
140
|
-
if (args.length !== 3) {
|
|
141
|
-
throw new Error('REDUCE requires 3 arguments: array, initialValue, expression');
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// 解析Lambda表达式
|
|
145
|
-
const lambdaMatch = args[2].match(/\((\w+),\s*(\w+)\)\s*=>\s*(.+)/);
|
|
146
|
-
if (!lambdaMatch) {
|
|
147
|
-
throw new Error('REDUCE requires a lambda expression: (accumulator, item) => expression');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const [, accVar, itemVar, expression] = lambdaMatch;
|
|
151
|
-
|
|
152
|
-
// 替换变量为$$value和$$this
|
|
153
|
-
const compiledExpr = expression
|
|
154
|
-
.replace(new RegExp(`\\b${accVar}\\b`, 'g'), '$$value')
|
|
155
|
-
.replace(new RegExp(`\\b${itemVar}\\b`, 'g'), '$$this');
|
|
156
|
-
|
|
157
|
-
return {
|
|
158
|
-
$reduce: {
|
|
159
|
-
input: this._parseValue(args[0]),
|
|
160
|
-
initialValue: this._parseValue(args[1]),
|
|
161
|
-
in: this._compileInnerExpression(compiledExpr, targetDB)
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
case 'ZIP': {
|
|
167
|
-
// ZIP(array1, array2, ...)
|
|
168
|
-
if (args.length < 2) {
|
|
169
|
-
throw new Error('ZIP requires at least 2 arrays');
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return {
|
|
173
|
-
$zip: {
|
|
174
|
-
inputs: args.map(arg => this._parseValue(arg))
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
case 'REVERSE_ARRAY': {
|
|
180
|
-
return { $reverseArray: this._parseValue(args[0]) };
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
case 'RANGE': {
|
|
184
|
-
// RANGE(start, end [, step])
|
|
185
|
-
if (args.length < 2) {
|
|
186
|
-
throw new Error('RANGE requires at least 2 arguments: start, end');
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const result = [
|
|
190
|
-
this._parseValue(args[0]),
|
|
191
|
-
this._parseValue(args[1])
|
|
192
|
-
];
|
|
193
|
-
|
|
194
|
-
if (args[2]) {
|
|
195
|
-
result.push(this._parseValue(args[2]));
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return { $range: result };
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
default:
|
|
202
|
-
throw new Error(`Unsupported array extended function: ${funcName}`);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 字符串扩展函数(3个)
|
|
208
|
-
* STR_LEN_BYTES, STR_LEN_CP, SUBSTR_BYTES
|
|
209
|
-
*/
|
|
210
|
-
function _compileStringExtendedFunction(funcName, argsStr, targetDB) {
|
|
211
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
212
|
-
|
|
213
|
-
switch (funcName) {
|
|
214
|
-
case 'STR_LEN_BYTES': {
|
|
215
|
-
return { $strLenBytes: this._parseValue(args[0]) };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
case 'STR_LEN_CP': {
|
|
219
|
-
return { $strLenCP: this._parseValue(args[0]) };
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
case 'SUBSTR_BYTES': {
|
|
223
|
-
// SUBSTR_BYTES(string, start, length)
|
|
224
|
-
if (args.length !== 3) {
|
|
225
|
-
throw new Error('SUBSTR_BYTES requires 3 arguments: string, start, length');
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return {
|
|
229
|
-
$substrBytes: [
|
|
230
|
-
this._parseValue(args[0]),
|
|
231
|
-
this._parseValue(args[1]),
|
|
232
|
-
this._parseValue(args[2])
|
|
233
|
-
]
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
default:
|
|
238
|
-
throw new Error(`Unsupported string extended function: ${funcName}`);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* 数学扩展函数(2个)
|
|
244
|
-
* LOG, LOG10
|
|
245
|
-
*/
|
|
246
|
-
function _compileMathExtendedFunction(funcName, argsStr, targetDB) {
|
|
247
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
248
|
-
|
|
249
|
-
switch (funcName) {
|
|
250
|
-
case 'LOG': {
|
|
251
|
-
// LOG(number, base)
|
|
252
|
-
if (args.length !== 2) {
|
|
253
|
-
throw new Error('LOG requires 2 arguments: number, base');
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return {
|
|
257
|
-
$log: [
|
|
258
|
-
this._parseValue(args[0]),
|
|
259
|
-
this._parseValue(args[1])
|
|
260
|
-
]
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
case 'LOG10': {
|
|
265
|
-
return { $log10: this._parseValue(args[0]) };
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
default:
|
|
269
|
-
throw new Error(`Unsupported math extended function: ${funcName}`);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* 逻辑扩展函数(2个)
|
|
275
|
-
* ALL_ELEMENTS_TRUE, ANY_ELEMENT_TRUE
|
|
276
|
-
*/
|
|
277
|
-
function _compileLogicalExtendedFunction(funcName, argsStr, targetDB) {
|
|
278
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
279
|
-
|
|
280
|
-
switch (funcName) {
|
|
281
|
-
case 'ALL_ELEMENTS_TRUE': {
|
|
282
|
-
return { $allElementsTrue: [this._parseValue(args[0])] };
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
case 'ANY_ELEMENT_TRUE': {
|
|
286
|
-
return { $anyElementTrue: [this._parseValue(args[0])] };
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
default:
|
|
290
|
-
throw new Error(`Unsupported logical extended function: ${funcName}`);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* 条件扩展函数(2个)
|
|
296
|
-
* COND, IF_NULL
|
|
297
|
-
*/
|
|
298
|
-
function _compileConditionalExtendedFunction(funcName, argsStr, targetDB) {
|
|
299
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
300
|
-
|
|
301
|
-
switch (funcName) {
|
|
302
|
-
case 'COND': {
|
|
303
|
-
// COND(condition, thenValue, elseValue)
|
|
304
|
-
if (args.length !== 3) {
|
|
305
|
-
throw new Error('COND requires 3 arguments: condition, thenValue, elseValue');
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return {
|
|
309
|
-
$cond: {
|
|
310
|
-
if: this._compileInnerExpression(args[0], targetDB),
|
|
311
|
-
then: this._parseValue(args[1]),
|
|
312
|
-
else: this._parseValue(args[2])
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
case 'IF_NULL': {
|
|
318
|
-
// IF_NULL(expression, replacement)
|
|
319
|
-
if (args.length !== 2) {
|
|
320
|
-
throw new Error('IF_NULL requires 2 arguments: expression, replacement');
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return {
|
|
324
|
-
$ifNull: [
|
|
325
|
-
this._parseValue(args[0]),
|
|
326
|
-
this._parseValue(args[1])
|
|
327
|
-
]
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
default:
|
|
332
|
-
throw new Error(`Unsupported conditional extended function: ${funcName}`);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* 对象操作函数(3个)
|
|
338
|
-
* SET_FIELD, UNSET_FIELD, GET_FIELD
|
|
339
|
-
*/
|
|
340
|
-
function _compileObjectOperationFunction(funcName, argsStr, targetDB) {
|
|
341
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
342
|
-
|
|
343
|
-
switch (funcName) {
|
|
344
|
-
case 'SET_FIELD': {
|
|
345
|
-
// SET_FIELD(fieldName, value, input)
|
|
346
|
-
if (args.length !== 3) {
|
|
347
|
-
throw new Error('SET_FIELD requires 3 arguments: fieldName, value, input');
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
return {
|
|
351
|
-
$setField: {
|
|
352
|
-
field: this._parseValue(args[0]),
|
|
353
|
-
input: this._parseValue(args[2]),
|
|
354
|
-
value: this._parseValue(args[1])
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
case 'UNSET_FIELD': {
|
|
360
|
-
// UNSET_FIELD(fieldName, input)
|
|
361
|
-
if (args.length !== 2) {
|
|
362
|
-
throw new Error('UNSET_FIELD requires 2 arguments: fieldName, input');
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return {
|
|
366
|
-
$unsetField: {
|
|
367
|
-
field: this._parseValue(args[0]),
|
|
368
|
-
input: this._parseValue(args[1])
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
case 'GET_FIELD': {
|
|
374
|
-
// GET_FIELD(fieldName, input)
|
|
375
|
-
if (args.length === 1) {
|
|
376
|
-
return { $getField: this._parseValue(args[0]) };
|
|
377
|
-
} else if (args.length === 2) {
|
|
378
|
-
return {
|
|
379
|
-
$getField: {
|
|
380
|
-
field: this._parseValue(args[0]),
|
|
381
|
-
input: this._parseValue(args[1])
|
|
382
|
-
}
|
|
383
|
-
};
|
|
384
|
-
} else {
|
|
385
|
-
throw new Error('GET_FIELD requires 1 or 2 arguments');
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
default:
|
|
390
|
-
throw new Error(`Unsupported object operation function: ${funcName}`);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* 集合操作函数(4个)
|
|
396
|
-
* SET_DIFFERENCE, SET_EQUALS, SET_INTERSECTION, SET_IS_SUBSET
|
|
397
|
-
*/
|
|
398
|
-
function _compileSetOperationFunction(funcName, argsStr, targetDB) {
|
|
399
|
-
const args = this._parseFunctionArgs(argsStr);
|
|
400
|
-
|
|
401
|
-
switch (funcName) {
|
|
402
|
-
case 'SET_DIFFERENCE': {
|
|
403
|
-
// SET_DIFFERENCE(array1, array2)
|
|
404
|
-
if (args.length !== 2) {
|
|
405
|
-
throw new Error('SET_DIFFERENCE requires 2 arguments: array1, array2');
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
return {
|
|
409
|
-
$setDifference: [
|
|
410
|
-
this._parseValue(args[0]),
|
|
411
|
-
this._parseValue(args[1])
|
|
412
|
-
]
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
case 'SET_EQUALS': {
|
|
417
|
-
// SET_EQUALS(array1, array2, ...)
|
|
418
|
-
if (args.length < 2) {
|
|
419
|
-
throw new Error('SET_EQUALS requires at least 2 arrays');
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return {
|
|
423
|
-
$setEquals: args.map(arg => this._parseValue(arg))
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
case 'SET_INTERSECTION': {
|
|
428
|
-
// SET_INTERSECTION(array1, array2, ...)
|
|
429
|
-
if (args.length < 2) {
|
|
430
|
-
throw new Error('SET_INTERSECTION requires at least 2 arrays');
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
return {
|
|
434
|
-
$setIntersection: args.map(arg => this._parseValue(arg))
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
case 'SET_IS_SUBSET': {
|
|
439
|
-
// SET_IS_SUBSET(array1, array2)
|
|
440
|
-
if (args.length !== 2) {
|
|
441
|
-
throw new Error('SET_IS_SUBSET requires 2 arguments: array1, array2');
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
return {
|
|
445
|
-
$setIsSubset: [
|
|
446
|
-
this._parseValue(args[0]),
|
|
447
|
-
this._parseValue(args[1])
|
|
448
|
-
]
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
default:
|
|
453
|
-
throw new Error(`Unsupported set operation function: ${funcName}`);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* 高级操作函数(4个)
|
|
459
|
-
* LET, LITERAL, RAND, SAMPLE_RATE
|
|
460
|
-
*/
|
|
461
|
-
function _compileAdvancedOperationFunction(funcName, argsStr, targetDB) {
|
|
462
|
-
const args = argsStr ? this._parseFunctionArgs(argsStr) : [];
|
|
463
|
-
|
|
464
|
-
switch (funcName) {
|
|
465
|
-
case 'LET': {
|
|
466
|
-
// LET({var1: value1, var2: value2}, expression)
|
|
467
|
-
if (args.length !== 2) {
|
|
468
|
-
throw new Error('LET requires 2 arguments: variables, expression');
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// 解析变量定义
|
|
472
|
-
const varsMatch = args[0].match(/\{(.+)\}/);
|
|
473
|
-
if (!varsMatch) {
|
|
474
|
-
throw new Error('LET requires an object literal for variables');
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
const varsStr = varsMatch[1];
|
|
478
|
-
const varPairs = varsStr.split(',').map(pair => {
|
|
479
|
-
const [key, value] = pair.split(':').map(s => s.trim());
|
|
480
|
-
return [key, value];
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
const vars = {};
|
|
484
|
-
varPairs.forEach(([key, value]) => {
|
|
485
|
-
vars[key] = this._parseValue(value);
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
return {
|
|
489
|
-
$let: {
|
|
490
|
-
vars: vars,
|
|
491
|
-
in: this._compileInnerExpression(args[1], targetDB)
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
case 'LITERAL': {
|
|
497
|
-
return { $literal: this._parseValue(args[0]) };
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
case 'RAND': {
|
|
501
|
-
return { $rand: {} };
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
case 'SAMPLE_RATE': {
|
|
505
|
-
// SAMPLE_RATE(rate)
|
|
506
|
-
if (args.length !== 1) {
|
|
507
|
-
throw new Error('SAMPLE_RATE requires 1 argument: rate');
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
return { $sampleRate: this._parseValue(args[0]) };
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
default:
|
|
514
|
-
throw new Error(`Unsupported advanced operation function: ${funcName}`);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
// 导出所有扩展方法
|
|
519
|
-
module.exports = {
|
|
520
|
-
_compileDateExtendedFunction,
|
|
521
|
-
_compileTypeConversionFunction,
|
|
522
|
-
_compileArrayExtendedFunction,
|
|
523
|
-
_compileStringExtendedFunction,
|
|
524
|
-
_compileMathExtendedFunction,
|
|
525
|
-
_compileLogicalExtendedFunction,
|
|
526
|
-
_compileConditionalExtendedFunction,
|
|
527
|
-
_compileObjectOperationFunction,
|
|
528
|
-
_compileSetOperationFunction,
|
|
529
|
-
_compileAdvancedOperationFunction
|
|
530
|
-
};
|
|
531
|
-
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 表达式检测器
|
|
3
|
-
* 检测对象是否为表达式对象
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 检测对象是否为表达式对象
|
|
8
|
-
* @param {*} obj - 要检测的对象
|
|
9
|
-
* @returns {boolean} 是否为表达式对象
|
|
10
|
-
*/
|
|
11
|
-
function isExpressionObject(obj) {
|
|
12
|
-
if (!obj || typeof obj !== 'object') {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return (
|
|
17
|
-
obj.__expr__ !== undefined &&
|
|
18
|
-
typeof obj.__expr__ === 'string' &&
|
|
19
|
-
obj.__compiled__ !== undefined &&
|
|
20
|
-
typeof obj.__compiled__ === 'boolean'
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 递归检测管道中是否包含表达式对象
|
|
26
|
-
* @param {Array} pipeline - 聚合管道
|
|
27
|
-
* @returns {boolean} 是否包含表达式对象
|
|
28
|
-
*/
|
|
29
|
-
function hasExpressionInPipeline(pipeline) {
|
|
30
|
-
if (!Array.isArray(pipeline)) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
for (const stage of pipeline) {
|
|
35
|
-
if (hasExpressionInObject(stage)) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 递归检测对象中是否包含表达式对象
|
|
45
|
-
* @param {*} obj - 要检测的对象
|
|
46
|
-
* @returns {boolean} 是否包含表达式对象
|
|
47
|
-
*/
|
|
48
|
-
function hasExpressionInObject(obj) {
|
|
49
|
-
if (!obj || typeof obj !== 'object') {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// 检查当前对象
|
|
54
|
-
if (isExpressionObject(obj)) {
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 递归检查对象属性
|
|
59
|
-
if (Array.isArray(obj)) {
|
|
60
|
-
for (const item of obj) {
|
|
61
|
-
if (hasExpressionInObject(item)) {
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
} else {
|
|
66
|
-
for (const key in obj) {
|
|
67
|
-
if (obj.hasOwnProperty(key)) {
|
|
68
|
-
if (hasExpressionInObject(obj[key])) {
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
module.exports = {
|
|
79
|
-
isExpressionObject,
|
|
80
|
-
hasExpressionInPipeline,
|
|
81
|
-
hasExpressionInObject
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 表达式工厂函数
|
|
3
|
-
* 创建表达式对象,用于标记需要编译的表达式
|
|
4
|
-
*
|
|
5
|
-
* @param {string} expression - 表达式字符串
|
|
6
|
-
* @returns {Object} 表达式对象
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* const expr = $("amount > 100");
|
|
10
|
-
* // 返回: { __expr__: "amount > 100", __compiled__: false }
|
|
11
|
-
*/
|
|
12
|
-
function createExpression(expression) {
|
|
13
|
-
if (typeof expression !== 'string') {
|
|
14
|
-
throw new TypeError('Expression must be a string');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (!expression || expression.trim().length === 0) {
|
|
18
|
-
throw new Error('Expression cannot be empty');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
__expr__: expression,
|
|
23
|
-
__compiled__: false
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = createExpression;
|
|
28
|
-
|
|
29
|
-
|
package/lib/expression/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 表达式模块主入口
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const createExpression = require('./factory');
|
|
6
|
-
const { isExpressionObject, hasExpressionInPipeline, hasExpressionInObject } = require('./detector');
|
|
7
|
-
const ExpressionCompiler = require('./compiler/ExpressionCompiler');
|
|
8
|
-
|
|
9
|
-
// 导出工厂函数为默认导出(用于 $ 函数)
|
|
10
|
-
module.exports = createExpression;
|
|
11
|
-
|
|
12
|
-
// 导出其他功能
|
|
13
|
-
module.exports.createExpression = createExpression;
|
|
14
|
-
module.exports.isExpressionObject = isExpressionObject;
|
|
15
|
-
module.exports.hasExpressionInPipeline = hasExpressionInPipeline;
|
|
16
|
-
module.exports.hasExpressionInObject = hasExpressionInObject;
|
|
17
|
-
module.exports.ExpressionCompiler = ExpressionCompiler;
|
|
18
|
-
|
|
19
|
-
|