proto.io 0.0.212 → 0.0.214

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 (44) hide show
  1. package/dist/adapters/file/aliyun-oss.d.ts +3 -3
  2. package/dist/adapters/file/database.d.ts +2 -2
  3. package/dist/adapters/file/database.js +2 -2
  4. package/dist/adapters/file/database.js.map +1 -1
  5. package/dist/adapters/file/database.mjs +1 -1
  6. package/dist/adapters/file/filesystem.d.ts +3 -3
  7. package/dist/adapters/file/google-cloud-storage.d.ts +3 -3
  8. package/dist/adapters/storage/progres.d.ts +1 -1
  9. package/dist/adapters/storage/progres.js +117 -84
  10. package/dist/adapters/storage/progres.js.map +1 -1
  11. package/dist/adapters/storage/progres.mjs +79 -46
  12. package/dist/adapters/storage/progres.mjs.map +1 -1
  13. package/dist/client.d.ts +3 -3
  14. package/dist/index.d.ts +3 -3
  15. package/dist/index.js +42 -42
  16. package/dist/index.js.map +1 -1
  17. package/dist/index.mjs +2 -2
  18. package/dist/internals/{base-CZGalGrd.d.ts → base-E1b8J-Fs.d.ts} +2 -2
  19. package/dist/internals/base-E1b8J-Fs.d.ts.map +1 -0
  20. package/dist/internals/{chunk-BsT9SYny.d.ts → chunk-D1bD7otk.d.ts} +3 -3
  21. package/dist/internals/chunk-D1bD7otk.d.ts.map +1 -0
  22. package/dist/internals/{index-Boxwkqe0.d.ts → index-CbjY-gJ7.d.ts} +6 -8
  23. package/dist/internals/index-CbjY-gJ7.d.ts.map +1 -0
  24. package/dist/internals/{index-NF-U_3zG.d.ts → index-SSEdPyhp.d.ts} +2 -2
  25. package/dist/internals/index-SSEdPyhp.d.ts.map +1 -0
  26. package/dist/internals/random-CYjWDvex.mjs +37 -0
  27. package/dist/internals/random-CYjWDvex.mjs.map +1 -0
  28. package/dist/internals/random-nkOQ9U6S.js +39 -0
  29. package/dist/internals/random-nkOQ9U6S.js.map +1 -0
  30. package/dist/internals/{index-D8O7SinR.js → validator-DoRPoIs2.js} +586 -2
  31. package/dist/internals/validator-DoRPoIs2.js.map +1 -0
  32. package/dist/internals/{index-DG9HHO_U.mjs → validator-Dxmbwa3P.mjs} +583 -3
  33. package/dist/internals/validator-Dxmbwa3P.mjs.map +1 -0
  34. package/package.json +1 -1
  35. package/dist/internals/base-CZGalGrd.d.ts.map +0 -1
  36. package/dist/internals/chunk-BsT9SYny.d.ts.map +0 -1
  37. package/dist/internals/index-Boxwkqe0.d.ts.map +0 -1
  38. package/dist/internals/index-D8O7SinR.js.map +0 -1
  39. package/dist/internals/index-DG9HHO_U.mjs.map +0 -1
  40. package/dist/internals/index-NF-U_3zG.d.ts.map +0 -1
  41. package/dist/internals/random-CufRbivU.mjs +0 -526
  42. package/dist/internals/random-CufRbivU.mjs.map +0 -1
  43. package/dist/internals/random-DzvxbWAc.js +0 -532
  44. package/dist/internals/random-DzvxbWAc.js.map +0 -1
@@ -5,10 +5,10 @@ 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 { escapeLiteral, escapeIdentifier } from 'pg/lib/utils';
8
- import { a as QueryCoditionalSelector, b as QueryFieldSelector, c as QueryExpressionSelector, d as QueryDistanceExpression, e as QueryKeyExpression, f as QueryValueExpression, g as QueryCoditionalExpression, h as QueryComparisonExpression, i as QueryNotExpression, j as QueryArrayExpression, Q as QuerySelector, F as FieldSelectorExpression } from '../../internals/index-DG9HHO_U.mjs';
8
+ import { r as resolveColumn, a as resolveDataType$1, c as QueryCoditionalSelector, d as QueryFieldSelector, e as QueryExpressionSelector, Q as QueryValidator, f as QueryDistanceExpression, g as QueryKeyExpression, h as QueryValueExpression, i as QueryCoditionalExpression, j as QueryComparisonExpression, k as QueryNotExpression, l as QueryArrayExpression, m as QueryNoParamAccumulator, n as QueryExprAccumulator, b as QuerySelector, F as FieldSelectorExpression } from '../../internals/validator-Dxmbwa3P.mjs';
9
9
  import '@o2ter/crypto-js';
10
- import { r as resolveColumn, a as resolveDataType$1, g as generateId, b as accumulatorKeyTypes, Q as QueryValidator } from '../../internals/random-CufRbivU.mjs';
11
10
  import { c as PROTO_EVENT } from '../../internals/const-Dkp7Nsv5.mjs';
11
+ import { g as generateId } from '../../internals/random-CYjWDvex.mjs';
12
12
  import { P as PVK } from '../../internals/private-CNw40LZ7.mjs';
13
13
 
14
14
  //
@@ -305,7 +305,11 @@ class QueryCompiler {
305
305
  ) AS ${{ identifier: fetchName }}
306
306
  ${!_.isEmpty(filter) ? sql `WHERE ${{ literal: _.map(filter, x => sql `(${x})`), separator: ' AND ' }}` : sql ``}
307
307
  ${_options?.sort ? _options?.sort : sql ``}
308
- ${!_.isEmpty(query.sort) ? sql `ORDER BY ${this._encodeSort(query.sort, { className: query.className, name: fetchName })}` : sql ``}
308
+ ${!_.isEmpty(query.sort) ? sql `ORDER BY ${this._encodeSort(query.sort, {
309
+ name: fetchName,
310
+ className: query.className,
311
+ groupMatches: query.groupMatches,
312
+ })}` : sql ``}
309
313
  ${query.limit ? sql `LIMIT ${{ literal: `${query.limit}` }}` : sql ``}
310
314
  ${query.skip ? sql `OFFSET ${{ literal: `${query.skip}` }}` : sql ``}
311
315
  `,
@@ -664,14 +668,19 @@ class SqlStorage {
664
668
  const count = parseInt(_count);
665
669
  return _.isFinite(count) ? count : 0;
666
670
  }
667
- _matchesType(options) {
671
+ _matchesType(compiler, options) {
668
672
  const types = {};
669
673
  for (const [key, match] of _.entries(options.matches)) {
670
- types[key] = this._matchesType(match);
674
+ const type = resolveDataType$1(compiler.schema, options.className, key);
675
+ if (_.isNil(type))
676
+ continue;
677
+ if (isPointer(type) || isRelation(type)) {
678
+ types[key] = this._matchesType(compiler, { className: type.target, ...match });
679
+ }
671
680
  }
672
681
  for (const [key, group] of _.entries(options.groupMatches)) {
673
682
  for (const [field, expr] of _.entries(group)) {
674
- _.set(types, `${key}.${field}`, accumulatorKeyTypes[expr.type]);
683
+ _.set(types, `${key}.${field}`, expr.evalType(compiler.schema, options.className));
675
684
  }
676
685
  }
677
686
  return types;
@@ -679,24 +688,26 @@ class SqlStorage {
679
688
  find(query) {
680
689
  const self = this;
681
690
  const compiler = self._makeCompiler(false, query.extraFilter);
691
+ const _matchesType = self._matchesType(compiler, query);
682
692
  const _query = compiler._selectQuery(query);
683
693
  return (async function* () {
684
694
  const objects = self.query(_query);
685
695
  for await (const object of objects) {
686
- yield self._decodeObject(query.className, object, self._matchesType(query));
696
+ yield self._decodeObject(query.className, object, _matchesType);
687
697
  }
688
698
  })();
689
699
  }
690
700
  random(query, opts) {
691
701
  const self = this;
692
702
  const compiler = self._makeCompiler(false, query.extraFilter);
703
+ const _matchesType = self._matchesType(compiler, query);
693
704
  const _query = compiler._selectQuery({ ...query, sort: {} }, {
694
705
  sort: sql `ORDER BY ${self.dialect.random(opts ?? {})}`,
695
706
  });
696
707
  return (async function* () {
697
708
  const objects = self.query(_query);
698
709
  for await (const object of objects) {
699
- yield self._decodeObject(query.className, object, self._matchesType(query));
710
+ yield self._decodeObject(query.className, object, _matchesType);
700
711
  }
701
712
  })();
702
713
  }
@@ -717,6 +728,7 @@ class SqlStorage {
717
728
  nonrefs(query) {
718
729
  const self = this;
719
730
  const compiler = self._makeCompiler(false, query.extraFilter);
731
+ const _matchesType = self._matchesType(compiler, query);
720
732
  const _query = compiler._selectQuery(query, ({ fetchName }) => ({
721
733
  extraFilter: sql `
722
734
  NOT EXISTS (${this._refs(this.schema, query.className, ['_id'], sql `(${{ quote: query.className + '$' }} || ${{ identifier: fetchName }}.${{ identifier: '_id' }})`)})
@@ -725,29 +737,33 @@ class SqlStorage {
725
737
  return (async function* () {
726
738
  const objects = self.query(_query);
727
739
  for await (const object of objects) {
728
- yield self._decodeObject(query.className, object, self._matchesType(query));
740
+ yield self._decodeObject(query.className, object, _matchesType);
729
741
  }
730
742
  })();
731
743
  }
732
744
  async insert(options, values) {
733
745
  const compiler = this._makeCompiler(true);
746
+ const _matchesType = this._matchesType(compiler, options);
734
747
  const result = await this.query(compiler.insert(options, values));
735
- return _.map(result, x => this._decodeObject(options.className, x, this._matchesType(options)));
748
+ return _.map(result, x => this._decodeObject(options.className, x, _matchesType));
736
749
  }
737
750
  async update(query, update) {
738
751
  const compiler = this._makeCompiler(true, query.extraFilter);
752
+ const _matchesType = this._matchesType(compiler, query);
739
753
  const updated = await this.query(compiler.update(query, update));
740
- return _.map(updated, x => this._decodeObject(query.className, x, this._matchesType(query)));
754
+ return _.map(updated, x => this._decodeObject(query.className, x, _matchesType));
741
755
  }
742
756
  async upsert(query, update, setOnInsert) {
743
757
  const compiler = this._makeCompiler(true, query.extraFilter);
758
+ const _matchesType = this._matchesType(compiler, query);
744
759
  const upserted = await this.query(compiler.upsert(query, update, setOnInsert));
745
- return _.map(upserted, x => this._decodeObject(query.className, x, this._matchesType(query)));
760
+ return _.map(upserted, x => this._decodeObject(query.className, x, _matchesType));
746
761
  }
747
762
  async delete(query) {
748
763
  const compiler = this._makeCompiler(true, query.extraFilter);
764
+ const _matchesType = this._matchesType(compiler, query);
749
765
  const deleted = await this.query(compiler.delete(query));
750
- return _.map(deleted, x => this._decodeObject(query.className, x, this._matchesType(query)));
766
+ return _.map(deleted, x => this._decodeObject(query.className, x, _matchesType));
751
767
  }
752
768
  }
753
769
 
@@ -775,7 +791,7 @@ class SqlStorage {
775
791
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
776
792
  // THE SOFTWARE.
777
793
  //
778
- const _fetchElement = (parent, colname, subpath, dataType) => {
794
+ const _fetchElement = (compiler, parent, colname, subpath, dataType) => {
779
795
  const element = sql `${{ identifier: parent.name }}.${{ identifier: parent.name.startsWith('_doller_expr_$') ? '$' : colname }}`;
780
796
  if (!parent.className) {
781
797
  if (colname !== '$') {
@@ -798,7 +814,7 @@ const _fetchElement = (parent, colname, subpath, dataType) => {
798
814
  return {
799
815
  element: sql `${{ identifier: parent.name }}.${{ identifier: `${colname}.${subpath[0]}` }}`,
800
816
  json: false,
801
- dataType: accumulatorKeyTypes[match.type],
817
+ dataType: match.evalType(compiler.schema, dataType.target),
802
818
  };
803
819
  }
804
820
  else if (dataType && _isTypeof(dataType, ['array', 'string[]', 'relation'])) {
@@ -861,7 +877,7 @@ const _resolvePopulate = (path, populates) => {
861
877
  const fetchElement = (compiler, parent, field) => {
862
878
  if (parent.className) {
863
879
  const { dataType, colname, subpath } = resolvePaths(compiler, parent.className, _.toPath(field));
864
- const { element, json, dataType: _dataType } = _fetchElement(parent, colname, subpath, dataType);
880
+ const { element, json, dataType: _dataType } = _fetchElement(compiler, parent, colname, subpath, dataType);
865
881
  if (isPointer(dataType))
866
882
  return { element: sql `${{ identifier: parent.name }}.${{ identifier: `${colname}._id` }}`, dataType };
867
883
  const populate = isRelation(dataType) && _resolvePopulate(_.toPath(colname), parent.populates);
@@ -893,7 +909,7 @@ const fetchElement = (compiler, parent, field) => {
893
909
  }
894
910
  }
895
911
  const [colname, ...subpath] = _.toPath(field);
896
- const { element } = _fetchElement(parent, colname, subpath);
912
+ const { element } = _fetchElement(compiler, parent, colname, subpath);
897
913
  return { element, dataType: null, relation: null };
898
914
  };
899
915
 
@@ -1681,7 +1697,19 @@ const encodeTypedQueryExpression = (compiler, parent, expr) => {
1681
1697
  if (expr instanceof QueryKeyExpression) {
1682
1698
  const { element, dataType } = fetchElement(compiler, parent, expr.key);
1683
1699
  const _dataType = dataType ? _typeof(dataType) : null;
1684
- if (_dataType && _PrimitiveValue.includes(_dataType)) {
1700
+ if (_dataType === 'number') {
1701
+ return [
1702
+ { type: 'number', sql: element },
1703
+ { type: 'decimal', sql: sql `CAST((${element}) AS DECIMAL)` },
1704
+ ];
1705
+ }
1706
+ else if (_dataType === 'decimal') {
1707
+ return [
1708
+ { type: 'decimal', sql: element },
1709
+ { type: 'number', sql: element },
1710
+ ];
1711
+ }
1712
+ else if (_dataType && _PrimitiveValue.includes(_dataType)) {
1685
1713
  return [{ type: _dataType, sql: element }];
1686
1714
  }
1687
1715
  }
@@ -1933,7 +1961,7 @@ const _selectRelationPopulate = (compiler, parent, populate, field, encode) => {
1933
1961
  isRelation(type) && sql `${{ identifier: populate.name }}.${{ identifier: `$${path}` }}`,
1934
1962
  ]))}
1935
1963
  FROM ${{ identifier: populate.name }} WHERE ${cond}
1936
- ${!_.isEmpty(populate.sort) ? sql `ORDER BY ${compiler._encodeSort(populate.sort, { className: populate.className, name: populate.name })}` : sql ``}
1964
+ ${!_.isEmpty(populate.sort) ? sql `ORDER BY ${compiler._encodeSort(populate.sort, populate)}` : sql ``}
1937
1965
  ${populate.limit ? sql `LIMIT ${{ literal: `${populate.limit}` }}` : sql ``}
1938
1966
  ${populate.skip ? sql `OFFSET ${{ literal: `${populate.skip}` }}` : sql ``}
1939
1967
  ${compiler.selectLock ? compiler.isUpdate ? sql `FOR UPDATE NOWAIT` : sql `FOR SHARE NOWAIT` : sql ``}
@@ -1946,39 +1974,44 @@ const selectPopulate = (compiler, parent, populate, field) => {
1946
1974
  sql `${{ identifier: parent.name }}.${{ identifier: field }} AS ${{ identifier: `$${field}` }}`,
1947
1975
  ];
1948
1976
  if (!_.isEmpty(groupMatches?.[field])) {
1949
- for (const [key, { type, expr }] of _.entries(groupMatches[field])) {
1950
- switch (type) {
1951
- case '$count':
1952
- columns.push(sql `
1953
- (
1954
- SELECT COUNT(*) FROM (
1955
- ${_selectRelationPopulate(compiler, parent, populate, field, false)}
1956
- ) ${{ identifier: populate.name }}
1957
- ) AS ${{ identifier: `${field}.${key}` }}
1958
- `);
1959
- break;
1960
- case '$avg':
1961
- case '$sum':
1962
- {
1963
- const op = {
1964
- '$avg': 'AVG',
1965
- '$sum': 'SUM',
1966
- }[type];
1967
- if (!expr)
1968
- throw Error('Invalid expression');
1969
- const exprs = encodeTypedQueryExpression(compiler, populate, expr);
1970
- const { sql: value } = (_.includes(['$avg'], type) ? _.find(exprs, e => e.type === 'number') : _.first(exprs)) ?? {};
1971
- if (!value)
1972
- throw Error('Invalid expression');
1977
+ for (const [key, expr] of _.entries(groupMatches[field])) {
1978
+ if (expr instanceof QueryNoParamAccumulator) {
1979
+ switch (expr.type) {
1980
+ case '$count':
1973
1981
  columns.push(sql `
1974
1982
  (
1975
- SELECT ${{ literal: op }}(${value}) FROM (
1983
+ SELECT COUNT(*) FROM (
1976
1984
  ${_selectRelationPopulate(compiler, parent, populate, field, false)}
1977
1985
  ) ${{ identifier: populate.name }}
1978
1986
  ) AS ${{ identifier: `${field}.${key}` }}
1979
1987
  `);
1980
- }
1981
- break;
1988
+ break;
1989
+ }
1990
+ }
1991
+ else if (expr instanceof QueryExprAccumulator) {
1992
+ const op = {
1993
+ '$max': 'MAX',
1994
+ '$min': 'MIN',
1995
+ '$avg': 'AVG',
1996
+ '$sum': 'SUM',
1997
+ '$stdDevPop': 'STDDEV_POP',
1998
+ '$stdDevSamp': 'STDDEV_SAMP',
1999
+ '$varPop': 'VAR_POP',
2000
+ '$varSamp': 'VAR_SAMP',
2001
+ }[expr.type];
2002
+ if (!expr.expr)
2003
+ throw Error('Invalid expression');
2004
+ const exprs = encodeTypedQueryExpression(compiler, populate, expr.expr);
2005
+ const value = _.first(exprs)?.sql;
2006
+ if (!value)
2007
+ throw Error('Invalid expression');
2008
+ columns.push(sql `
2009
+ (
2010
+ SELECT ${{ literal: op }}(${value}) FROM (
2011
+ ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2012
+ ) ${{ identifier: populate.name }}
2013
+ ) AS ${{ identifier: `${field}.${key}` }}
2014
+ `);
1982
2015
  }
1983
2016
  }
1984
2017
  }