proto.io 0.0.228 → 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.
Files changed (65) hide show
  1. package/README.md +1017 -0
  2. package/dist/adapters/file/aliyun-oss.d.mts +26 -0
  3. package/dist/adapters/file/aliyun-oss.d.mts.map +1 -0
  4. package/dist/adapters/file/aliyun-oss.d.ts +3 -3
  5. package/dist/adapters/file/database.d.mts +23 -0
  6. package/dist/adapters/file/database.d.mts.map +1 -0
  7. package/dist/adapters/file/database.d.ts +2 -2
  8. package/dist/adapters/file/database.js +1 -1
  9. package/dist/adapters/file/database.mjs +1 -1
  10. package/dist/adapters/file/filesystem.d.mts +25 -0
  11. package/dist/adapters/file/filesystem.d.mts.map +1 -0
  12. package/dist/adapters/file/filesystem.d.ts +3 -3
  13. package/dist/adapters/file/google-cloud-storage.d.mts +29 -0
  14. package/dist/adapters/file/google-cloud-storage.d.mts.map +1 -0
  15. package/dist/adapters/file/google-cloud-storage.d.ts +3 -3
  16. package/dist/adapters/storage/postgres.d.mts +299 -0
  17. package/dist/adapters/storage/postgres.d.mts.map +1 -0
  18. package/dist/adapters/storage/postgres.d.ts +5 -1
  19. package/dist/adapters/storage/postgres.d.ts.map +1 -1
  20. package/dist/adapters/storage/postgres.js +182 -74
  21. package/dist/adapters/storage/postgres.js.map +1 -1
  22. package/dist/adapters/storage/postgres.mjs +182 -74
  23. package/dist/adapters/storage/postgres.mjs.map +1 -1
  24. package/dist/client.d.mts +16 -0
  25. package/dist/client.d.mts.map +1 -0
  26. package/dist/client.d.ts +3 -3
  27. package/dist/client.js +1 -1
  28. package/dist/client.mjs +2 -2
  29. package/dist/index.d.mts +151 -0
  30. package/dist/index.d.mts.map +1 -0
  31. package/dist/index.d.ts +3 -3
  32. package/dist/index.js +50 -7
  33. package/dist/index.js.map +1 -1
  34. package/dist/index.mjs +51 -8
  35. package/dist/index.mjs.map +1 -1
  36. package/dist/internals/{base-CW4QHAo3.d.ts → base-Bhrj5Pq1.d.ts} +2 -2
  37. package/dist/internals/{base-CW4QHAo3.d.ts.map → base-Bhrj5Pq1.d.ts.map} +1 -1
  38. package/dist/internals/base-CiZHXD0o.d.mts +27 -0
  39. package/dist/internals/base-CiZHXD0o.d.mts.map +1 -0
  40. package/dist/internals/chunk-Cp2QN7ug.d.mts +17 -0
  41. package/dist/internals/chunk-Cp2QN7ug.d.mts.map +1 -0
  42. package/dist/internals/{chunk-DPgxK2_o.d.ts → chunk-o7lWIP-f.d.ts} +3 -3
  43. package/dist/internals/{chunk-DPgxK2_o.d.ts.map → chunk-o7lWIP-f.d.ts.map} +1 -1
  44. package/dist/internals/{index-vOFh8pVc.js → index-B0TO6h9r.js} +8 -1
  45. package/dist/internals/index-B0TO6h9r.js.map +1 -0
  46. package/dist/internals/{index-CywcwPk-.d.ts → index-B710pfTH.d.ts} +2 -2
  47. package/dist/internals/{index-CywcwPk-.d.ts.map → index-B710pfTH.d.ts.map} +1 -1
  48. package/dist/internals/{index-BWZIV3_T.mjs → index-DG2-4tQ1.mjs} +8 -1
  49. package/dist/internals/index-DG2-4tQ1.mjs.map +1 -0
  50. package/dist/internals/index-DwjvuRyl.d.mts +92 -0
  51. package/dist/internals/index-DwjvuRyl.d.mts.map +1 -0
  52. package/dist/internals/index-OwgXw07h.d.mts +2107 -0
  53. package/dist/internals/index-OwgXw07h.d.mts.map +1 -0
  54. package/dist/internals/{index-h4KGKuhq.d.ts → index-OwgXw07h.d.ts} +45 -3
  55. package/dist/internals/index-OwgXw07h.d.ts.map +1 -0
  56. package/dist/internals/{validator-Bc1jRJfA.js → validator-CFlx3oyq.js} +33 -1
  57. package/dist/internals/validator-CFlx3oyq.js.map +1 -0
  58. package/dist/internals/{validator-Boj1PUjM.mjs → validator-DubDY921.mjs} +32 -2
  59. package/dist/internals/validator-DubDY921.mjs.map +1 -0
  60. package/package.json +7 -19
  61. package/dist/internals/index-BWZIV3_T.mjs.map +0 -1
  62. package/dist/internals/index-h4KGKuhq.d.ts.map +0 -1
  63. package/dist/internals/index-vOFh8pVc.js.map +0 -1
  64. package/dist/internals/validator-Bc1jRJfA.js.map +0 -1
  65. package/dist/internals/validator-Boj1PUjM.mjs.map +0 -1
@@ -9,7 +9,7 @@ var QueryStream = require('pg-query-stream');
9
9
  var utilsJs = require('@o2ter/utils-js');
10
10
  var Decimal = require('decimal.js');
11
11
  var utils = require('pg/lib/utils');
12
- var validator = require('../../internals/validator-Bc1jRJfA.js');
12
+ var validator = require('../../internals/validator-CFlx3oyq.js');
13
13
  require('@o2ter/crypto-js');
14
14
  var _const = require('../../internals/const-BCYpEfJg.js');
15
15
  var random$1 = require('../../internals/random-nkOQ9U6S.js');
@@ -441,6 +441,15 @@ class QueryCompiler {
441
441
  _selectPopulateMap(context) {
442
442
  return _.map(context.populates, (populate, field) => this.dialect.selectPopulate(this, context, populate, field));
443
443
  }
444
+ groupFind(query, accumulators) {
445
+ return this._modifyQuery(query, (fetchName, context) => {
446
+ const columns = _.map(accumulators, (expr, field) => {
447
+ const aggSQL = this.dialect.encodeAccumulatorColumn(this, { ...context, name: fetchName, className: query.className }, expr, fetchName);
448
+ return sql `${aggSQL} AS ${{ identifier: field }}`;
449
+ });
450
+ return sql `SELECT ${{ literal: columns, separator: ', ' }} FROM ${{ identifier: fetchName }}`;
451
+ });
452
+ }
444
453
  insert(options, values) {
445
454
  const _values = _.map(values, attr => ({
446
455
  ..._defaultInsertOpts(options),
@@ -1446,6 +1455,30 @@ class SqlStorage {
1446
1455
  }
1447
1456
  })();
1448
1457
  }
1458
+ async groupFind(query, accumulators) {
1459
+ const compiler = this._makeCompiler(true, query.extraFilter);
1460
+ const [result] = await this.query(compiler.groupFind(query, accumulators));
1461
+ if (!result)
1462
+ return result;
1463
+ // Decode accumulator results based on their types
1464
+ const decoded = {};
1465
+ for (const [key, accumulator] of _.toPairs(accumulators)) {
1466
+ const evalType = accumulator.evalType(this.schema, query.className);
1467
+ if (evalType === 'array') {
1468
+ // For $group operators, result is already parsed JSONB array
1469
+ decoded[key] = result[key];
1470
+ }
1471
+ else if (evalType && index.isPrimitive(evalType)) {
1472
+ // Extract primitive type from PrimitiveType
1473
+ const primitiveType = _.isString(evalType) ? evalType : evalType.type;
1474
+ decoded[key] = this.dialect.decodeType(primitiveType, result[key]);
1475
+ }
1476
+ else {
1477
+ decoded[key] = result[key];
1478
+ }
1479
+ }
1480
+ return decoded;
1481
+ }
1449
1482
  refs(object, classNames, roles) {
1450
1483
  const self = this;
1451
1484
  const query = sql `
@@ -2215,6 +2248,152 @@ const updateOperation = (paths, dataType, operation) => {
2215
2248
  throw Error('Invalid update operation');
2216
2249
  };
2217
2250
 
2251
+ //
2252
+ // accumulators.ts
2253
+ //
2254
+ // The MIT License
2255
+ // Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
2256
+ //
2257
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
2258
+ // of this software and associated documentation files (the "Software"), to deal
2259
+ // in the Software without restriction, including without limitation the rights
2260
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2261
+ // copies of the Software, and to permit persons to whom the Software is
2262
+ // furnished to do so, subject to the following conditions:
2263
+ //
2264
+ // The above copyright notice and this permission notice shall be included in
2265
+ // all copies or substantial portions of the Software.
2266
+ //
2267
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2268
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2269
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2270
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2271
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2272
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2273
+ // THE SOFTWARE.
2274
+ //
2275
+ const encodeColumn = (compiler, context, expr) => {
2276
+ if (expr instanceof validator.QueryZeroParamAccumulator) {
2277
+ switch (expr.type) {
2278
+ case '$count':
2279
+ return sql `COUNT(*)`;
2280
+ default:
2281
+ throw Error('Invalid expression');
2282
+ }
2283
+ }
2284
+ else if (expr instanceof validator.QueryUnaryAccumulator) {
2285
+ if (!expr.expr)
2286
+ throw Error('Invalid expression');
2287
+ const { sql: value } = encodeTypedQueryExpression(compiler, context, expr.expr) ?? {};
2288
+ if (!value)
2289
+ throw Error('Invalid expression');
2290
+ switch (expr.type) {
2291
+ case '$most':
2292
+ return sql `MODE() WITHIN GROUP (ORDER BY (${value}))`;
2293
+ default:
2294
+ {
2295
+ const op = {
2296
+ '$max': 'MAX',
2297
+ '$min': 'MIN',
2298
+ '$avg': 'AVG',
2299
+ '$sum': 'SUM',
2300
+ '$stdDevPop': 'STDDEV_POP',
2301
+ '$stdDevSamp': 'STDDEV_SAMP',
2302
+ '$varPop': 'VAR_POP',
2303
+ '$varSamp': 'VAR_SAMP',
2304
+ }[expr.type];
2305
+ return sql `${{ literal: op }}(${value})`;
2306
+ }
2307
+ }
2308
+ }
2309
+ else if (expr instanceof validator.QueryPercentileAccumulator) {
2310
+ const op = {
2311
+ 'discrete': 'PERCENTILE_DISC',
2312
+ 'continuous': 'PERCENTILE_CONT',
2313
+ }[expr.mode];
2314
+ if (!expr.input)
2315
+ throw Error('Invalid expression');
2316
+ const { sql: value } = encodeTypedQueryExpression(compiler, context, expr.input) ?? {};
2317
+ if (!value)
2318
+ throw Error('Invalid expression');
2319
+ return sql `${{ literal: op }}(${{ value: expr.p }}) WITHIN GROUP (ORDER BY (${value}))`;
2320
+ }
2321
+ else {
2322
+ throw Error('Invalid expression');
2323
+ }
2324
+ };
2325
+ const encodeAccumulatorExpression = (compiler, parent, populate, field, expr, aliasName) => {
2326
+ if (expr instanceof validator.QueryGroupAccumulator) {
2327
+ if (!expr.key)
2328
+ throw Error('Invalid expression');
2329
+ const { sql: keyValue } = encodeTypedQueryExpression(compiler, populate, expr.key) ?? {};
2330
+ if (!keyValue)
2331
+ throw Error('Invalid expression');
2332
+ const aggSQL = encodeColumn(compiler, populate, expr.value);
2333
+ return sql `
2334
+ (
2335
+ SELECT COALESCE(
2336
+ jsonb_agg(
2337
+ jsonb_build_object('key', grouped_key, 'value', grouped_value)
2338
+ ),
2339
+ '[]'::jsonb
2340
+ ) FROM (
2341
+ SELECT ${keyValue} AS grouped_key, ${aggSQL} AS grouped_value
2342
+ FROM (
2343
+ ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2344
+ ) ${{ identifier: populate.name }}
2345
+ GROUP BY grouped_key
2346
+ ) AS _grouped
2347
+ ) AS ${{ identifier: aliasName }}
2348
+ `;
2349
+ }
2350
+ const aggSQL = encodeColumn(compiler, populate, expr);
2351
+ return sql `
2352
+ (
2353
+ SELECT ${aggSQL} FROM (
2354
+ ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2355
+ ) ${{ identifier: populate.name }}
2356
+ ) AS ${{ identifier: aliasName }}
2357
+ `;
2358
+ };
2359
+ const encodeAccumulatorColumn = (compiler, context, expr, fetchName) => {
2360
+ if (expr instanceof validator.QueryGroupAccumulator) {
2361
+ if (!expr.key)
2362
+ throw Error('Invalid expression');
2363
+ // Try to get typed expression first
2364
+ let keyValue;
2365
+ const typedResult = encodeTypedQueryExpression(compiler, context, expr.key);
2366
+ if (typedResult?.sql) {
2367
+ keyValue = typedResult.sql;
2368
+ }
2369
+ else if (expr.key instanceof validator.QueryKeyExpression) {
2370
+ // For non-primitive types (like arrays), use raw element
2371
+ const { element } = fetchElement(compiler, context, expr.key.key);
2372
+ keyValue = element;
2373
+ }
2374
+ else {
2375
+ throw Error('Invalid expression');
2376
+ }
2377
+ const aggSQL = encodeColumn(compiler, context, expr.value);
2378
+ return sql `
2379
+ (
2380
+ SELECT COALESCE(
2381
+ jsonb_agg(
2382
+ jsonb_build_object('key', grouped_key, 'value', grouped_value)
2383
+ ),
2384
+ '[]'::jsonb
2385
+ )
2386
+ FROM (
2387
+ SELECT ${keyValue} AS grouped_key, ${aggSQL} AS grouped_value
2388
+ FROM ${{ identifier: fetchName }}
2389
+ GROUP BY grouped_key
2390
+ ) AS _grouped
2391
+ )
2392
+ `;
2393
+ }
2394
+ return encodeColumn(compiler, context, expr);
2395
+ };
2396
+
2218
2397
  //
2219
2398
  // populate.ts
2220
2399
  //
@@ -2317,79 +2496,7 @@ const selectPopulate = (compiler, parent, populate, field) => {
2317
2496
  ];
2318
2497
  if (!_.isEmpty(groupMatches?.[field])) {
2319
2498
  for (const [key, expr] of _.entries(groupMatches[field])) {
2320
- if (expr instanceof validator.QueryZeroParamAccumulator) {
2321
- switch (expr.type) {
2322
- case '$count':
2323
- columns.push(sql `
2324
- (
2325
- SELECT COUNT(*) FROM (
2326
- ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2327
- ) ${{ identifier: populate.name }}
2328
- ) AS ${{ identifier: `${field}.${key}` }}
2329
- `);
2330
- break;
2331
- }
2332
- }
2333
- else if (expr instanceof validator.QueryUnaryAccumulator) {
2334
- if (!expr.expr)
2335
- throw Error('Invalid expression');
2336
- const { sql: value } = encodeTypedQueryExpression(compiler, populate, expr.expr) ?? {};
2337
- if (!value)
2338
- throw Error('Invalid expression');
2339
- switch (expr.type) {
2340
- case '$most':
2341
- columns.push(sql `
2342
- (
2343
- SELECT MODE() WITHIN GROUP (ORDER BY (${value})) FROM (
2344
- ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2345
- ) ${{ identifier: populate.name }}
2346
- ) AS ${{ identifier: `${field}.${key}` }}
2347
- `);
2348
- break;
2349
- default:
2350
- {
2351
- const op = {
2352
- '$max': 'MAX',
2353
- '$min': 'MIN',
2354
- '$avg': 'AVG',
2355
- '$sum': 'SUM',
2356
- '$stdDevPop': 'STDDEV_POP',
2357
- '$stdDevSamp': 'STDDEV_SAMP',
2358
- '$varPop': 'VAR_POP',
2359
- '$varSamp': 'VAR_SAMP',
2360
- }[expr.type];
2361
- columns.push(sql `
2362
- (
2363
- SELECT ${{ literal: op }}(${value}) FROM (
2364
- ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2365
- ) ${{ identifier: populate.name }}
2366
- ) AS ${{ identifier: `${field}.${key}` }}
2367
- `);
2368
- }
2369
- break;
2370
- }
2371
- }
2372
- else if (expr instanceof validator.QueryPercentileAccumulator) {
2373
- const op = {
2374
- 'discrete': 'PERCENTILE_DISC',
2375
- 'continuous': 'PERCENTILE_CONT',
2376
- }[expr.mode];
2377
- if (!expr.input)
2378
- throw Error('Invalid expression');
2379
- const { sql: value } = encodeTypedQueryExpression(compiler, populate, expr.input) ?? {};
2380
- if (!value)
2381
- throw Error('Invalid expression');
2382
- columns.push(sql `
2383
- (
2384
- SELECT ${{ literal: op }}(${{ value: expr.p }}) WITHIN GROUP (ORDER BY (${value})) FROM (
2385
- ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2386
- ) ${{ identifier: populate.name }}
2387
- ) AS ${{ identifier: `${field}.${key}` }}
2388
- `);
2389
- }
2390
- else {
2391
- throw Error('Invalid expression');
2392
- }
2499
+ columns.push(encodeAccumulatorExpression(compiler, parent, populate, field, expr, `${field}.${key}`));
2393
2500
  }
2394
2501
  }
2395
2502
  else {
@@ -3070,6 +3177,7 @@ const PostgresDialect = {
3070
3177
  encodePopulate,
3071
3178
  encodeRelation,
3072
3179
  encodeSortKey,
3180
+ encodeAccumulatorColumn,
3073
3181
  random,
3074
3182
  };
3075
3183