proto.io 0.0.227 → 0.0.229
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 +1017 -0
- package/dist/adapters/file/aliyun-oss.d.mts +26 -0
- package/dist/adapters/file/aliyun-oss.d.mts.map +1 -0
- package/dist/adapters/file/aliyun-oss.d.ts +3 -3
- package/dist/adapters/file/database.d.mts +23 -0
- package/dist/adapters/file/database.d.mts.map +1 -0
- package/dist/adapters/file/database.d.ts +2 -2
- package/dist/adapters/file/database.js +1 -1
- package/dist/adapters/file/database.mjs +1 -1
- package/dist/adapters/file/filesystem.d.mts +25 -0
- package/dist/adapters/file/filesystem.d.mts.map +1 -0
- package/dist/adapters/file/filesystem.d.ts +3 -3
- package/dist/adapters/file/google-cloud-storage.d.mts +29 -0
- package/dist/adapters/file/google-cloud-storage.d.mts.map +1 -0
- package/dist/adapters/file/google-cloud-storage.d.ts +3 -3
- package/dist/adapters/storage/postgres.d.mts +299 -0
- package/dist/adapters/storage/postgres.d.mts.map +1 -0
- package/dist/adapters/storage/postgres.d.ts +5 -1
- package/dist/adapters/storage/postgres.d.ts.map +1 -1
- package/dist/adapters/storage/postgres.js +182 -74
- package/dist/adapters/storage/postgres.js.map +1 -1
- package/dist/adapters/storage/postgres.mjs +182 -74
- package/dist/adapters/storage/postgres.mjs.map +1 -1
- package/dist/client.d.mts +16 -0
- package/dist/client.d.mts.map +1 -0
- package/dist/client.d.ts +3 -3
- package/dist/client.js +1 -1
- package/dist/client.mjs +2 -2
- package/dist/index.d.mts +151 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +68 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -26
- package/dist/index.mjs.map +1 -1
- package/dist/internals/{base-DSo02iAX.d.ts → base-Bhrj5Pq1.d.ts} +2 -2
- package/dist/internals/{base-DSo02iAX.d.ts.map → base-Bhrj5Pq1.d.ts.map} +1 -1
- package/dist/internals/base-CiZHXD0o.d.mts +27 -0
- package/dist/internals/base-CiZHXD0o.d.mts.map +1 -0
- package/dist/internals/chunk-Cp2QN7ug.d.mts +17 -0
- package/dist/internals/chunk-Cp2QN7ug.d.mts.map +1 -0
- package/dist/internals/{chunk-BhwfdCdq.d.ts → chunk-o7lWIP-f.d.ts} +3 -3
- package/dist/internals/{chunk-BhwfdCdq.d.ts.map → chunk-o7lWIP-f.d.ts.map} +1 -1
- package/dist/internals/{index-vOFh8pVc.js → index-B0TO6h9r.js} +8 -1
- package/dist/internals/index-B0TO6h9r.js.map +1 -0
- package/dist/internals/{index-Cj45GkKv.d.ts → index-B710pfTH.d.ts} +2 -2
- package/dist/internals/{index-Cj45GkKv.d.ts.map → index-B710pfTH.d.ts.map} +1 -1
- package/dist/internals/{index-BWZIV3_T.mjs → index-DG2-4tQ1.mjs} +8 -1
- package/dist/internals/index-DG2-4tQ1.mjs.map +1 -0
- package/dist/internals/index-DwjvuRyl.d.mts +92 -0
- package/dist/internals/index-DwjvuRyl.d.mts.map +1 -0
- package/dist/internals/index-OwgXw07h.d.mts +2107 -0
- package/dist/internals/index-OwgXw07h.d.mts.map +1 -0
- package/dist/internals/{index-1ZK5N4yb.d.ts → index-OwgXw07h.d.ts} +49 -7
- package/dist/internals/index-OwgXw07h.d.ts.map +1 -0
- package/dist/internals/{validator-Bc1jRJfA.js → validator-CFlx3oyq.js} +33 -1
- package/dist/internals/validator-CFlx3oyq.js.map +1 -0
- package/dist/internals/{validator-Boj1PUjM.mjs → validator-DubDY921.mjs} +32 -2
- package/dist/internals/validator-DubDY921.mjs.map +1 -0
- package/package.json +7 -19
- package/dist/internals/index-1ZK5N4yb.d.ts.map +0 -1
- package/dist/internals/index-BWZIV3_T.mjs.map +0 -1
- package/dist/internals/index-vOFh8pVc.js.map +0 -1
- package/dist/internals/validator-Bc1jRJfA.js.map +0 -1
- package/dist/internals/validator-Boj1PUjM.mjs.map +0 -1
|
@@ -5,7 +5,7 @@ import QueryStream from 'pg-query-stream';
|
|
|
5
5
|
import { asyncStream, IteratorPool } from '@o2ter/utils-js';
|
|
6
6
|
import Decimal from 'decimal.js';
|
|
7
7
|
import { escapeIdentifier, escapeLiteral } from 'pg/lib/utils';
|
|
8
|
-
import { r as resolveColumn,
|
|
8
|
+
import { r as resolveColumn, c as resolveDataType$1, e as QueryCoditionalSelector, f as QueryFieldSelector, g as QueryExpressionSelector, h as QueryArrayExpression, i as QueryValueExpression, j as QueryKeyExpression, k as QueryCoditionalExpression, l as QueryComparisonExpression, m as QueryNotExpression, n as QueryDistanceExpression, o as QueryZeroParamExpression, p as QueryUnaryExpression, q as QueryBinaryExpression, s as QueryListExpression, t as QueryTernaryExpression, u as QueryCondExpression, Q as QueryValidator, v as QueryGroupAccumulator, w as QueryZeroParamAccumulator, x as QueryUnaryAccumulator, y as QueryPercentileAccumulator, d as QuerySelector, F as FieldSelectorExpression } from '../../internals/validator-DubDY921.mjs';
|
|
9
9
|
import '@o2ter/crypto-js';
|
|
10
10
|
import { d as PROTO_EVENT } from '../../internals/const-68KwNmag.mjs';
|
|
11
11
|
import { g as generateId } from '../../internals/random-CYjWDvex.mjs';
|
|
@@ -437,6 +437,15 @@ class QueryCompiler {
|
|
|
437
437
|
_selectPopulateMap(context) {
|
|
438
438
|
return _.map(context.populates, (populate, field) => this.dialect.selectPopulate(this, context, populate, field));
|
|
439
439
|
}
|
|
440
|
+
groupFind(query, accumulators) {
|
|
441
|
+
return this._modifyQuery(query, (fetchName, context) => {
|
|
442
|
+
const columns = _.map(accumulators, (expr, field) => {
|
|
443
|
+
const aggSQL = this.dialect.encodeAccumulatorColumn(this, { ...context, name: fetchName, className: query.className }, expr, fetchName);
|
|
444
|
+
return sql `${aggSQL} AS ${{ identifier: field }}`;
|
|
445
|
+
});
|
|
446
|
+
return sql `SELECT ${{ literal: columns, separator: ', ' }} FROM ${{ identifier: fetchName }}`;
|
|
447
|
+
});
|
|
448
|
+
}
|
|
440
449
|
insert(options, values) {
|
|
441
450
|
const _values = _.map(values, attr => ({
|
|
442
451
|
..._defaultInsertOpts(options),
|
|
@@ -1442,6 +1451,30 @@ class SqlStorage {
|
|
|
1442
1451
|
}
|
|
1443
1452
|
})();
|
|
1444
1453
|
}
|
|
1454
|
+
async groupFind(query, accumulators) {
|
|
1455
|
+
const compiler = this._makeCompiler(true, query.extraFilter);
|
|
1456
|
+
const [result] = await this.query(compiler.groupFind(query, accumulators));
|
|
1457
|
+
if (!result)
|
|
1458
|
+
return result;
|
|
1459
|
+
// Decode accumulator results based on their types
|
|
1460
|
+
const decoded = {};
|
|
1461
|
+
for (const [key, accumulator] of _.toPairs(accumulators)) {
|
|
1462
|
+
const evalType = accumulator.evalType(this.schema, query.className);
|
|
1463
|
+
if (evalType === 'array') {
|
|
1464
|
+
// For $group operators, result is already parsed JSONB array
|
|
1465
|
+
decoded[key] = result[key];
|
|
1466
|
+
}
|
|
1467
|
+
else if (evalType && isPrimitive(evalType)) {
|
|
1468
|
+
// Extract primitive type from PrimitiveType
|
|
1469
|
+
const primitiveType = _.isString(evalType) ? evalType : evalType.type;
|
|
1470
|
+
decoded[key] = this.dialect.decodeType(primitiveType, result[key]);
|
|
1471
|
+
}
|
|
1472
|
+
else {
|
|
1473
|
+
decoded[key] = result[key];
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
return decoded;
|
|
1477
|
+
}
|
|
1445
1478
|
refs(object, classNames, roles) {
|
|
1446
1479
|
const self = this;
|
|
1447
1480
|
const query = sql `
|
|
@@ -2211,6 +2244,152 @@ const updateOperation = (paths, dataType, operation) => {
|
|
|
2211
2244
|
throw Error('Invalid update operation');
|
|
2212
2245
|
};
|
|
2213
2246
|
|
|
2247
|
+
//
|
|
2248
|
+
// accumulators.ts
|
|
2249
|
+
//
|
|
2250
|
+
// The MIT License
|
|
2251
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
2252
|
+
//
|
|
2253
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
2254
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
2255
|
+
// in the Software without restriction, including without limitation the rights
|
|
2256
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
2257
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
2258
|
+
// furnished to do so, subject to the following conditions:
|
|
2259
|
+
//
|
|
2260
|
+
// The above copyright notice and this permission notice shall be included in
|
|
2261
|
+
// all copies or substantial portions of the Software.
|
|
2262
|
+
//
|
|
2263
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
2264
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
2265
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
2266
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
2267
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
2268
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
2269
|
+
// THE SOFTWARE.
|
|
2270
|
+
//
|
|
2271
|
+
const encodeColumn = (compiler, context, expr) => {
|
|
2272
|
+
if (expr instanceof QueryZeroParamAccumulator) {
|
|
2273
|
+
switch (expr.type) {
|
|
2274
|
+
case '$count':
|
|
2275
|
+
return sql `COUNT(*)`;
|
|
2276
|
+
default:
|
|
2277
|
+
throw Error('Invalid expression');
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
else if (expr instanceof QueryUnaryAccumulator) {
|
|
2281
|
+
if (!expr.expr)
|
|
2282
|
+
throw Error('Invalid expression');
|
|
2283
|
+
const { sql: value } = encodeTypedQueryExpression(compiler, context, expr.expr) ?? {};
|
|
2284
|
+
if (!value)
|
|
2285
|
+
throw Error('Invalid expression');
|
|
2286
|
+
switch (expr.type) {
|
|
2287
|
+
case '$most':
|
|
2288
|
+
return sql `MODE() WITHIN GROUP (ORDER BY (${value}))`;
|
|
2289
|
+
default:
|
|
2290
|
+
{
|
|
2291
|
+
const op = {
|
|
2292
|
+
'$max': 'MAX',
|
|
2293
|
+
'$min': 'MIN',
|
|
2294
|
+
'$avg': 'AVG',
|
|
2295
|
+
'$sum': 'SUM',
|
|
2296
|
+
'$stdDevPop': 'STDDEV_POP',
|
|
2297
|
+
'$stdDevSamp': 'STDDEV_SAMP',
|
|
2298
|
+
'$varPop': 'VAR_POP',
|
|
2299
|
+
'$varSamp': 'VAR_SAMP',
|
|
2300
|
+
}[expr.type];
|
|
2301
|
+
return sql `${{ literal: op }}(${value})`;
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
else if (expr instanceof QueryPercentileAccumulator) {
|
|
2306
|
+
const op = {
|
|
2307
|
+
'discrete': 'PERCENTILE_DISC',
|
|
2308
|
+
'continuous': 'PERCENTILE_CONT',
|
|
2309
|
+
}[expr.mode];
|
|
2310
|
+
if (!expr.input)
|
|
2311
|
+
throw Error('Invalid expression');
|
|
2312
|
+
const { sql: value } = encodeTypedQueryExpression(compiler, context, expr.input) ?? {};
|
|
2313
|
+
if (!value)
|
|
2314
|
+
throw Error('Invalid expression');
|
|
2315
|
+
return sql `${{ literal: op }}(${{ value: expr.p }}) WITHIN GROUP (ORDER BY (${value}))`;
|
|
2316
|
+
}
|
|
2317
|
+
else {
|
|
2318
|
+
throw Error('Invalid expression');
|
|
2319
|
+
}
|
|
2320
|
+
};
|
|
2321
|
+
const encodeAccumulatorExpression = (compiler, parent, populate, field, expr, aliasName) => {
|
|
2322
|
+
if (expr instanceof QueryGroupAccumulator) {
|
|
2323
|
+
if (!expr.key)
|
|
2324
|
+
throw Error('Invalid expression');
|
|
2325
|
+
const { sql: keyValue } = encodeTypedQueryExpression(compiler, populate, expr.key) ?? {};
|
|
2326
|
+
if (!keyValue)
|
|
2327
|
+
throw Error('Invalid expression');
|
|
2328
|
+
const aggSQL = encodeColumn(compiler, populate, expr.value);
|
|
2329
|
+
return sql `
|
|
2330
|
+
(
|
|
2331
|
+
SELECT COALESCE(
|
|
2332
|
+
jsonb_agg(
|
|
2333
|
+
jsonb_build_object('key', grouped_key, 'value', grouped_value)
|
|
2334
|
+
),
|
|
2335
|
+
'[]'::jsonb
|
|
2336
|
+
) FROM (
|
|
2337
|
+
SELECT ${keyValue} AS grouped_key, ${aggSQL} AS grouped_value
|
|
2338
|
+
FROM (
|
|
2339
|
+
${_selectRelationPopulate(compiler, parent, populate, field, false)}
|
|
2340
|
+
) ${{ identifier: populate.name }}
|
|
2341
|
+
GROUP BY grouped_key
|
|
2342
|
+
) AS _grouped
|
|
2343
|
+
) AS ${{ identifier: aliasName }}
|
|
2344
|
+
`;
|
|
2345
|
+
}
|
|
2346
|
+
const aggSQL = encodeColumn(compiler, populate, expr);
|
|
2347
|
+
return sql `
|
|
2348
|
+
(
|
|
2349
|
+
SELECT ${aggSQL} FROM (
|
|
2350
|
+
${_selectRelationPopulate(compiler, parent, populate, field, false)}
|
|
2351
|
+
) ${{ identifier: populate.name }}
|
|
2352
|
+
) AS ${{ identifier: aliasName }}
|
|
2353
|
+
`;
|
|
2354
|
+
};
|
|
2355
|
+
const encodeAccumulatorColumn = (compiler, context, expr, fetchName) => {
|
|
2356
|
+
if (expr instanceof QueryGroupAccumulator) {
|
|
2357
|
+
if (!expr.key)
|
|
2358
|
+
throw Error('Invalid expression');
|
|
2359
|
+
// Try to get typed expression first
|
|
2360
|
+
let keyValue;
|
|
2361
|
+
const typedResult = encodeTypedQueryExpression(compiler, context, expr.key);
|
|
2362
|
+
if (typedResult?.sql) {
|
|
2363
|
+
keyValue = typedResult.sql;
|
|
2364
|
+
}
|
|
2365
|
+
else if (expr.key instanceof QueryKeyExpression) {
|
|
2366
|
+
// For non-primitive types (like arrays), use raw element
|
|
2367
|
+
const { element } = fetchElement(compiler, context, expr.key.key);
|
|
2368
|
+
keyValue = element;
|
|
2369
|
+
}
|
|
2370
|
+
else {
|
|
2371
|
+
throw Error('Invalid expression');
|
|
2372
|
+
}
|
|
2373
|
+
const aggSQL = encodeColumn(compiler, context, expr.value);
|
|
2374
|
+
return sql `
|
|
2375
|
+
(
|
|
2376
|
+
SELECT COALESCE(
|
|
2377
|
+
jsonb_agg(
|
|
2378
|
+
jsonb_build_object('key', grouped_key, 'value', grouped_value)
|
|
2379
|
+
),
|
|
2380
|
+
'[]'::jsonb
|
|
2381
|
+
)
|
|
2382
|
+
FROM (
|
|
2383
|
+
SELECT ${keyValue} AS grouped_key, ${aggSQL} AS grouped_value
|
|
2384
|
+
FROM ${{ identifier: fetchName }}
|
|
2385
|
+
GROUP BY grouped_key
|
|
2386
|
+
) AS _grouped
|
|
2387
|
+
)
|
|
2388
|
+
`;
|
|
2389
|
+
}
|
|
2390
|
+
return encodeColumn(compiler, context, expr);
|
|
2391
|
+
};
|
|
2392
|
+
|
|
2214
2393
|
//
|
|
2215
2394
|
// populate.ts
|
|
2216
2395
|
//
|
|
@@ -2313,79 +2492,7 @@ const selectPopulate = (compiler, parent, populate, field) => {
|
|
|
2313
2492
|
];
|
|
2314
2493
|
if (!_.isEmpty(groupMatches?.[field])) {
|
|
2315
2494
|
for (const [key, expr] of _.entries(groupMatches[field])) {
|
|
2316
|
-
|
|
2317
|
-
switch (expr.type) {
|
|
2318
|
-
case '$count':
|
|
2319
|
-
columns.push(sql `
|
|
2320
|
-
(
|
|
2321
|
-
SELECT COUNT(*) FROM (
|
|
2322
|
-
${_selectRelationPopulate(compiler, parent, populate, field, false)}
|
|
2323
|
-
) ${{ identifier: populate.name }}
|
|
2324
|
-
) AS ${{ identifier: `${field}.${key}` }}
|
|
2325
|
-
`);
|
|
2326
|
-
break;
|
|
2327
|
-
}
|
|
2328
|
-
}
|
|
2329
|
-
else if (expr instanceof QueryUnaryAccumulator) {
|
|
2330
|
-
if (!expr.expr)
|
|
2331
|
-
throw Error('Invalid expression');
|
|
2332
|
-
const { sql: value } = encodeTypedQueryExpression(compiler, populate, expr.expr) ?? {};
|
|
2333
|
-
if (!value)
|
|
2334
|
-
throw Error('Invalid expression');
|
|
2335
|
-
switch (expr.type) {
|
|
2336
|
-
case '$most':
|
|
2337
|
-
columns.push(sql `
|
|
2338
|
-
(
|
|
2339
|
-
SELECT MODE() WITHIN GROUP (ORDER BY (${value})) FROM (
|
|
2340
|
-
${_selectRelationPopulate(compiler, parent, populate, field, false)}
|
|
2341
|
-
) ${{ identifier: populate.name }}
|
|
2342
|
-
) AS ${{ identifier: `${field}.${key}` }}
|
|
2343
|
-
`);
|
|
2344
|
-
break;
|
|
2345
|
-
default:
|
|
2346
|
-
{
|
|
2347
|
-
const op = {
|
|
2348
|
-
'$max': 'MAX',
|
|
2349
|
-
'$min': 'MIN',
|
|
2350
|
-
'$avg': 'AVG',
|
|
2351
|
-
'$sum': 'SUM',
|
|
2352
|
-
'$stdDevPop': 'STDDEV_POP',
|
|
2353
|
-
'$stdDevSamp': 'STDDEV_SAMP',
|
|
2354
|
-
'$varPop': 'VAR_POP',
|
|
2355
|
-
'$varSamp': 'VAR_SAMP',
|
|
2356
|
-
}[expr.type];
|
|
2357
|
-
columns.push(sql `
|
|
2358
|
-
(
|
|
2359
|
-
SELECT ${{ literal: op }}(${value}) FROM (
|
|
2360
|
-
${_selectRelationPopulate(compiler, parent, populate, field, false)}
|
|
2361
|
-
) ${{ identifier: populate.name }}
|
|
2362
|
-
) AS ${{ identifier: `${field}.${key}` }}
|
|
2363
|
-
`);
|
|
2364
|
-
}
|
|
2365
|
-
break;
|
|
2366
|
-
}
|
|
2367
|
-
}
|
|
2368
|
-
else if (expr instanceof QueryPercentileAccumulator) {
|
|
2369
|
-
const op = {
|
|
2370
|
-
'discrete': 'PERCENTILE_DISC',
|
|
2371
|
-
'continuous': 'PERCENTILE_CONT',
|
|
2372
|
-
}[expr.mode];
|
|
2373
|
-
if (!expr.input)
|
|
2374
|
-
throw Error('Invalid expression');
|
|
2375
|
-
const { sql: value } = encodeTypedQueryExpression(compiler, populate, expr.input) ?? {};
|
|
2376
|
-
if (!value)
|
|
2377
|
-
throw Error('Invalid expression');
|
|
2378
|
-
columns.push(sql `
|
|
2379
|
-
(
|
|
2380
|
-
SELECT ${{ literal: op }}(${{ value: expr.p }}) WITHIN GROUP (ORDER BY (${value})) FROM (
|
|
2381
|
-
${_selectRelationPopulate(compiler, parent, populate, field, false)}
|
|
2382
|
-
) ${{ identifier: populate.name }}
|
|
2383
|
-
) AS ${{ identifier: `${field}.${key}` }}
|
|
2384
|
-
`);
|
|
2385
|
-
}
|
|
2386
|
-
else {
|
|
2387
|
-
throw Error('Invalid expression');
|
|
2388
|
-
}
|
|
2495
|
+
columns.push(encodeAccumulatorExpression(compiler, parent, populate, field, expr, `${field}.${key}`));
|
|
2389
2496
|
}
|
|
2390
2497
|
}
|
|
2391
2498
|
else {
|
|
@@ -3066,6 +3173,7 @@ const PostgresDialect = {
|
|
|
3066
3173
|
encodePopulate,
|
|
3067
3174
|
encodeRelation,
|
|
3068
3175
|
encodeSortKey,
|
|
3176
|
+
encodeAccumulatorColumn,
|
|
3069
3177
|
random,
|
|
3070
3178
|
};
|
|
3071
3179
|
|