@nocobase/database 0.9.3-alpha.1 → 0.9.4-alpha.1

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 (59) hide show
  1. package/lib/collection.d.ts +9 -9
  2. package/lib/collection.js +100 -96
  3. package/lib/database.d.ts +4 -4
  4. package/lib/database.js +25 -53
  5. package/lib/eager-loading/eager-loading-tree.d.ts +23 -0
  6. package/lib/eager-loading/eager-loading-tree.js +338 -0
  7. package/lib/filter-parser.d.ts +1 -7
  8. package/lib/filter-parser.js +27 -7
  9. package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +5 -0
  10. package/lib/listeners/append-child-collection-name-after-repository-find.js +40 -0
  11. package/lib/listeners/index.js +2 -0
  12. package/lib/mock-database.js +3 -1
  13. package/lib/operators/string.js +1 -1
  14. package/lib/options-parser.js +4 -0
  15. package/lib/query-interface/postgres-query-interface.js +2 -2
  16. package/lib/relation-repository/belongs-to-many-repository.d.ts +2 -1
  17. package/lib/relation-repository/belongs-to-many-repository.js +58 -37
  18. package/lib/relation-repository/hasmany-repository.d.ts +2 -1
  19. package/lib/relation-repository/hasmany-repository.js +31 -16
  20. package/lib/relation-repository/multiple-relation-repository.js +8 -26
  21. package/lib/relation-repository/relation-repository.d.ts +1 -7
  22. package/lib/relation-repository/single-relation-repository.d.ts +1 -1
  23. package/lib/relation-repository/single-relation-repository.js +10 -16
  24. package/lib/repository.d.ts +11 -8
  25. package/lib/repository.js +104 -89
  26. package/lib/sql-parser/postgres.js +41 -0
  27. package/lib/update-guard.d.ts +1 -1
  28. package/lib/update-guard.js +16 -13
  29. package/lib/utils.d.ts +0 -7
  30. package/lib/utils.js +0 -76
  31. package/package.json +4 -4
  32. package/src/__tests__/eager-loading/eager-loading-tree.test.ts +393 -0
  33. package/src/__tests__/migrator.test.ts +4 -0
  34. package/src/__tests__/relation-repository/hasone-repository.test.ts +1 -0
  35. package/src/__tests__/repository/aggregation.test.ts +297 -0
  36. package/src/__tests__/repository/count.test.ts +1 -1
  37. package/src/__tests__/repository/find.test.ts +10 -1
  38. package/src/__tests__/repository.test.ts +30 -0
  39. package/src/__tests__/update-guard.test.ts +13 -0
  40. package/src/collection.ts +74 -66
  41. package/src/database.ts +26 -42
  42. package/src/eager-loading/eager-loading-tree.ts +304 -0
  43. package/src/filter-parser.ts +16 -2
  44. package/src/listeners/adjacency-list.ts +1 -3
  45. package/src/listeners/append-child-collection-name-after-repository-find.ts +31 -0
  46. package/src/listeners/index.ts +2 -0
  47. package/src/mock-database.ts +3 -1
  48. package/src/operators/notIn.ts +1 -0
  49. package/src/operators/string.ts +1 -1
  50. package/src/options-parser.ts +5 -0
  51. package/src/query-interface/postgres-query-interface.ts +1 -1
  52. package/src/relation-repository/belongs-to-many-repository.ts +33 -1
  53. package/src/relation-repository/hasmany-repository.ts +17 -0
  54. package/src/relation-repository/multiple-relation-repository.ts +14 -19
  55. package/src/relation-repository/single-relation-repository.ts +13 -15
  56. package/src/repository.ts +79 -36
  57. package/src/sql-parser/postgres.js +25505 -0
  58. package/src/update-guard.ts +21 -16
  59. package/src/utils.ts +0 -61
@@ -43,53 +43,74 @@ var __decorate = void 0 && (void 0).__decorate || function (decorators, target,
43
43
  return c > 3 && r && Object.defineProperty(target, key, r), r;
44
44
  };
45
45
  class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelationRepository {
46
- create(options) {
46
+ aggregate(options) {
47
47
  var _this = this;
48
+ return _asyncToGenerator(function* () {
49
+ const targetRepository = _this.targetCollection.repository;
50
+ const sourceModel = yield _this.getSourceModel();
51
+ const association = _this.association;
52
+ return yield targetRepository.aggregate(_objectSpread(_objectSpread({}, options), {}, {
53
+ optionsTransformer: modelOptions => {
54
+ modelOptions.include = modelOptions.include || [];
55
+ const throughWhere = {};
56
+ throughWhere[association.foreignKey] = sourceModel.get(association.sourceKey);
57
+ modelOptions.include.push({
58
+ association: association.oneFromTarget,
59
+ required: true,
60
+ attributes: [],
61
+ where: throughWhere
62
+ });
63
+ }
64
+ }));
65
+ })();
66
+ }
67
+ create(options) {
68
+ var _this2 = this;
48
69
  return _asyncToGenerator(function* () {
49
70
  if (Array.isArray(options.values)) {
50
- return Promise.all(options.values.map(record => _this.create(_objectSpread(_objectSpread({}, options), {}, {
71
+ return Promise.all(options.values.map(record => _this2.create(_objectSpread(_objectSpread({}, options), {}, {
51
72
  values: record
52
73
  }))));
53
74
  }
54
- const transaction = yield _this.getTransaction(options);
55
- const createAccessor = _this.accessors().create;
75
+ const transaction = yield _this2.getTransaction(options);
76
+ const createAccessor = _this2.accessors().create;
56
77
  const values = options.values || {};
57
- const sourceModel = yield _this.getSourceModel(transaction);
78
+ const sourceModel = yield _this2.getSourceModel(transaction);
58
79
  const createOptions = _objectSpread(_objectSpread({}, options), {}, {
59
- through: values[_this.throughName()],
80
+ through: values[_this2.throughName()],
60
81
  transaction
61
82
  });
62
83
  return sourceModel[createAccessor](values, createOptions);
63
84
  })();
64
85
  }
65
86
  destroy(options) {
66
- var _this2 = this;
87
+ var _this3 = this;
67
88
  return _asyncToGenerator(function* () {
68
- const transaction = yield _this2.getTransaction(options);
69
- const association = _this2.association;
70
- const throughModel = _this2.throughModel();
89
+ const transaction = yield _this3.getTransaction(options);
90
+ const association = _this3.association;
91
+ const throughModel = _this3.throughModel();
71
92
  const instancesToIds = instances => {
72
- return instances.map(instance => instance.get(_this2.targetKey()));
93
+ return instances.map(instance => instance.get(_this3.targetKey()));
73
94
  };
74
95
  // Through Table
75
96
  const throughTableWhere = [{
76
- [throughModel.rawAttributes[association.foreignKey].field]: _this2.sourceKeyValue
97
+ [throughModel.rawAttributes[association.foreignKey].field]: _this3.sourceKeyValue
77
98
  }];
78
99
  let ids;
79
100
  if (options && options['filter']) {
80
- const instances = yield _this2.find({
101
+ const instances = yield _this3.find({
81
102
  filter: options['filter'],
82
103
  transaction
83
104
  });
84
105
  ids = instancesToIds(instances);
85
106
  }
86
107
  if (options && options['filterByTk']) {
87
- const instances = _this2.association.toInstanceArray(options['filterByTk']);
108
+ const instances = _this3.association.toInstanceArray(options['filterByTk']);
88
109
  ids = ids ? _lodash().default.intersection(ids, instancesToIds(instances)) : instancesToIds(instances);
89
110
  }
90
111
  if (options && !options['filterByTk'] && !options['filter']) {
91
- const sourceModel = yield _this2.getSourceModel(transaction);
92
- const instances = yield sourceModel[_this2.accessors().get]({
112
+ const sourceModel = yield _this3.getSourceModel(transaction);
113
+ const instances = yield sourceModel[_this3.accessors().get]({
93
114
  transaction
94
115
  });
95
116
  ids = instancesToIds(instances);
@@ -100,13 +121,13 @@ class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelati
100
121
  }
101
122
  });
102
123
  // delete through table data
103
- yield _this2.throughModel().destroy({
124
+ yield _this3.throughModel().destroy({
104
125
  where: throughTableWhere,
105
126
  transaction
106
127
  });
107
- yield _this2.targetModel.destroy({
128
+ yield _this3.targetModel.destroy({
108
129
  where: {
109
- [_this2.targetKey()]: {
130
+ [_this3.targetKey()]: {
110
131
  [_sequelize().Op.in]: ids
111
132
  }
112
133
  },
@@ -116,10 +137,10 @@ class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelati
116
137
  })();
117
138
  }
118
139
  setTargets(call, options) {
119
- var _this3 = this;
140
+ var _this4 = this;
120
141
  return _asyncToGenerator(function* () {
121
142
  let handleKeys;
122
- const transaction = yield _this3.getTransaction(options, false);
143
+ const transaction = yield _this4.getTransaction(options, false);
123
144
  if (_lodash().default.isPlainObject(options)) {
124
145
  options = options.tk || [];
125
146
  }
@@ -131,7 +152,7 @@ class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelati
131
152
  } else {
132
153
  handleKeys = options;
133
154
  }
134
- const sourceModel = yield _this3.getSourceModel(transaction);
155
+ const sourceModel = yield _this4.getSourceModel(transaction);
135
156
  const setObj = handleKeys.reduce((carry, item) => {
136
157
  if (Array.isArray(item)) {
137
158
  carry[item[0]] = item[1];
@@ -141,14 +162,14 @@ class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelati
141
162
  return carry;
142
163
  }, {});
143
164
  const targetKeys = Object.keys(setObj);
144
- const association = _this3.association;
145
- const targetObjects = yield _this3.targetModel.findAll({
165
+ const association = _this4.association;
166
+ const targetObjects = yield _this4.targetModel.findAll({
146
167
  where: {
147
168
  [association['targetKey']]: targetKeys
148
169
  },
149
170
  transaction
150
171
  });
151
- yield sourceModel[_this3.accessors()[call]](targetObjects, {
172
+ yield sourceModel[_this4.accessors()[call]](targetObjects, {
152
173
  transaction
153
174
  });
154
175
  for (var _i = 0, _Object$entries = Object.entries(setObj); _i < _Object$entries.length; _i++) {
@@ -156,40 +177,40 @@ class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelati
156
177
  id = _Object$entries$_i[0],
157
178
  throughValues = _Object$entries$_i[1];
158
179
  if (typeof throughValues === 'object') {
159
- const instance = yield _this3.targetModel.findByPk(id, {
180
+ const instance = yield _this4.targetModel.findByPk(id, {
160
181
  transaction
161
182
  });
162
- yield (0, _updateAssociations.updateThroughTableValue)(instance, _this3.throughName(), throughValues, sourceModel, transaction);
183
+ yield (0, _updateAssociations.updateThroughTableValue)(instance, _this4.throughName(), throughValues, sourceModel, transaction);
163
184
  }
164
185
  }
165
186
  })();
166
187
  }
167
188
  add(options) {
168
- var _this4 = this;
189
+ var _this5 = this;
169
190
  return _asyncToGenerator(function* () {
170
- yield _this4.setTargets('add', options);
191
+ yield _this5.setTargets('add', options);
171
192
  })();
172
193
  }
173
194
  set(options) {
174
- var _this5 = this;
195
+ var _this6 = this;
175
196
  return _asyncToGenerator(function* () {
176
- yield _this5.setTargets('set', options);
197
+ yield _this6.setTargets('set', options);
177
198
  })();
178
199
  }
179
200
  toggle(options) {
180
- var _this6 = this;
201
+ var _this7 = this;
181
202
  return _asyncToGenerator(function* () {
182
- const transaction = yield _this6.getTransaction(options);
183
- const sourceModel = yield _this6.getSourceModel(transaction);
184
- const has = yield sourceModel[_this6.accessors().hasSingle](options['tk'], {
203
+ const transaction = yield _this7.getTransaction(options);
204
+ const sourceModel = yield _this7.getSourceModel(transaction);
205
+ const has = yield sourceModel[_this7.accessors().hasSingle](options['tk'], {
185
206
  transaction
186
207
  });
187
208
  if (has) {
188
- yield _this6.remove(_objectSpread(_objectSpread({}, options), {}, {
209
+ yield _this7.remove(_objectSpread(_objectSpread({}, options), {}, {
189
210
  transaction
190
211
  }));
191
212
  } else {
192
- yield _this6.add(_objectSpread(_objectSpread({}, options), {}, {
213
+ yield _this7.add(_objectSpread(_objectSpread({}, options), {}, {
193
214
  transaction
194
215
  }));
195
216
  }
@@ -1,5 +1,5 @@
1
1
  import { Model } from '../model';
2
- import { CreateOptions, DestroyOptions, FindOneOptions, FindOptions, TargetKey, TK, UpdateOptions } from '../repository';
2
+ import { AggregateOptions, CreateOptions, DestroyOptions, FindOneOptions, FindOptions, TargetKey, TK, UpdateOptions } from '../repository';
3
3
  import { AssociatedOptions, FindAndCountOptions, MultipleRelationRepository } from './multiple-relation-repository';
4
4
  interface IHasManyRepository<M extends Model> {
5
5
  find(options?: FindOptions): Promise<M>;
@@ -14,6 +14,7 @@ interface IHasManyRepository<M extends Model> {
14
14
  }
15
15
  export declare class HasManyRepository extends MultipleRelationRepository implements IHasManyRepository<any> {
16
16
  find(options?: FindOptions): Promise<any>;
17
+ aggregate(options: AggregateOptions): Promise<any>;
17
18
  destroy(options?: TK | DestroyOptions): Promise<boolean>;
18
19
  handleKeyOfAdd(options: any): any;
19
20
  set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
@@ -53,18 +53,33 @@ class HasManyRepository extends _multipleRelationRepository.MultipleRelationRepo
53
53
  return yield targetRepository.find(findOptions);
54
54
  })();
55
55
  }
56
- destroy(options) {
56
+ aggregate(options) {
57
57
  var _this2 = this;
58
58
  return _asyncToGenerator(function* () {
59
- const transaction = yield _this2.getTransaction(options);
60
- const sourceModel = yield _this2.getSourceModel(transaction);
59
+ const targetRepository = _this2.targetCollection.repository;
60
+ const addFilter = {
61
+ [_this2.association.foreignKey]: _this2.sourceKeyValue
62
+ };
63
+ const aggOptions = _objectSpread(_objectSpread({}, options), {}, {
64
+ filter: {
65
+ $and: [options.filter || {}, addFilter]
66
+ }
67
+ });
68
+ return yield targetRepository.aggregate(aggOptions);
69
+ })();
70
+ }
71
+ destroy(options) {
72
+ var _this3 = this;
73
+ return _asyncToGenerator(function* () {
74
+ const transaction = yield _this3.getTransaction(options);
75
+ const sourceModel = yield _this3.getSourceModel(transaction);
61
76
  const where = [{
62
- [_this2.association.foreignKey]: sourceModel.get(_this2.association.sourceKey)
77
+ [_this3.association.foreignKey]: sourceModel.get(_this3.association.sourceKey)
63
78
  }];
64
79
  if (options && options['filter']) {
65
- const filterResult = _this2.parseFilter(options['filter'], options);
80
+ const filterResult = _this3.parseFilter(options['filter'], options);
66
81
  if (filterResult.include && filterResult.include.length > 0) {
67
- return yield _this2.destroyByFilter(options['filter'], transaction);
82
+ return yield _this3.destroyByFilter(options['filter'], transaction);
68
83
  }
69
84
  where.push(filterResult.where);
70
85
  }
@@ -73,10 +88,10 @@ class HasManyRepository extends _multipleRelationRepository.MultipleRelationRepo
73
88
  options = options['filterByTk'];
74
89
  }
75
90
  where.push({
76
- [_this2.targetKey()]: options
91
+ [_this3.targetKey()]: options
77
92
  });
78
93
  }
79
- yield _this2.targetModel.destroy({
94
+ yield _this3.targetModel.destroy({
80
95
  where: {
81
96
  [_sequelize().Op.and]: where
82
97
  },
@@ -96,21 +111,21 @@ class HasManyRepository extends _multipleRelationRepository.MultipleRelationRepo
96
111
  return handleKeys;
97
112
  }
98
113
  set(options) {
99
- var _this3 = this;
114
+ var _this4 = this;
100
115
  return _asyncToGenerator(function* () {
101
- const transaction = yield _this3.getTransaction(options);
102
- const sourceModel = yield _this3.getSourceModel(transaction);
103
- yield sourceModel[_this3.accessors().set](_this3.handleKeyOfAdd(options), {
116
+ const transaction = yield _this4.getTransaction(options);
117
+ const sourceModel = yield _this4.getSourceModel(transaction);
118
+ yield sourceModel[_this4.accessors().set](_this4.handleKeyOfAdd(options), {
104
119
  transaction
105
120
  });
106
121
  })();
107
122
  }
108
123
  add(options) {
109
- var _this4 = this;
124
+ var _this5 = this;
110
125
  return _asyncToGenerator(function* () {
111
- const transaction = yield _this4.getTransaction(options);
112
- const sourceModel = yield _this4.getSourceModel(transaction);
113
- yield sourceModel[_this4.accessors().add](_this4.handleKeyOfAdd(options), {
126
+ const transaction = yield _this5.getTransaction(options);
127
+ const sourceModel = yield _this5.getSourceModel(transaction);
128
+ yield sourceModel[_this5.accessors().add](_this5.handleKeyOfAdd(options), {
114
129
  transaction
115
130
  });
116
131
  })();
@@ -4,13 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.MultipleRelationRepository = void 0;
7
- function _lodash() {
8
- const data = require("lodash");
9
- _lodash = function _lodash() {
10
- return data;
11
- };
12
- return data;
13
- }
14
7
  function _sequelize() {
15
8
  const data = require("sequelize");
16
9
  _sequelize = function _sequelize() {
@@ -21,7 +14,7 @@ function _sequelize() {
21
14
  var _updateAssociations = require("../update-associations");
22
15
  var _updateGuard = require("../update-guard");
23
16
  var _relationRepository = require("./relation-repository");
24
- var _utils = require("../utils");
17
+ var _eagerLoadingTree = require("../eager-loading/eager-loading-tree");
25
18
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
26
19
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
27
20
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
@@ -68,25 +61,14 @@ class MultipleRelationRepository extends _relationRepository.RelationRepository
68
61
  if (ids.length == 0) {
69
62
  return [];
70
63
  }
71
- return yield (0, _utils.handleAppendsQuery)({
72
- templateModel: ids[0].row,
73
- queryPromises: findOptions.include.map(include => {
74
- return sourceModel[getAccessor](_objectSpread(_objectSpread({}, (0, _lodash().omit)(findOptions, ['limit', 'offset'])), {}, {
75
- include: [include],
76
- where: {
77
- [_this.targetKey()]: {
78
- [_sequelize().Op.in]: ids.map(id => id.pk)
79
- }
80
- },
81
- transaction
82
- })).then(rows => {
83
- return {
84
- rows,
85
- include
86
- };
87
- });
88
- })
64
+ const eagerLoadingTree = _eagerLoadingTree.EagerLoadingTree.buildFromSequelizeOptions({
65
+ model: _this.targetModel,
66
+ rootAttributes: findOptions.attributes,
67
+ includeOption: findOptions.include,
68
+ rootOrder: findOptions.order
89
69
  });
70
+ yield eagerLoadingTree.load(ids.map(i => i.pk), transaction);
71
+ return eagerLoadingTree.root.instances;
90
72
  }
91
73
  const data = yield sourceModel[getAccessor](_objectSpread(_objectSpread({}, findOptions), {}, {
92
74
  transaction
@@ -22,12 +22,6 @@ export declare abstract class RelationRepository {
22
22
  create(options?: CreateOptions): Promise<any>;
23
23
  getSourceModel(transaction?: Transaction): Promise<Model<any, any>>;
24
24
  protected buildQueryOptions(options: FindOptions): any;
25
- protected parseFilter(filter: Filter, options?: any): {
26
- where?: undefined;
27
- include?: undefined;
28
- } | {
29
- where: {};
30
- include: any[];
31
- };
25
+ protected parseFilter(filter: Filter, options?: any): any;
32
26
  protected getTransaction(options: any, autoGen?: boolean): Promise<Transaction | null>;
33
27
  }
@@ -14,7 +14,7 @@ interface SetOption extends Transactionable {
14
14
  export declare abstract class SingleRelationRepository extends RelationRepository {
15
15
  remove(options?: Transactionable): Promise<void>;
16
16
  set(options: TargetKey | SetOption): Promise<void>;
17
- find(options?: SingleRelationFindOption): Promise<Model<any> | null>;
17
+ find(options?: SingleRelationFindOption): Promise<any>;
18
18
  findOne(options?: SingleRelationFindOption): Promise<Model<any>>;
19
19
  destroy(options?: Transactionable): Promise<boolean>;
20
20
  update(options: UpdateOptions): Promise<any>;
@@ -12,8 +12,8 @@ function _lodash() {
12
12
  return data;
13
13
  }
14
14
  var _updateAssociations = require("../update-associations");
15
- var _utils = require("../utils");
16
15
  var _relationRepository = require("./relation-repository");
16
+ var _eagerLoadingTree = require("../eager-loading/eager-loading-tree");
17
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
18
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
19
19
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
@@ -64,23 +64,17 @@ class SingleRelationRepository extends _relationRepository.RelationRepository {
64
64
  const templateModel = yield sourceModel[getAccessor](_objectSpread(_objectSpread({}, findOptions), {}, {
65
65
  includeIgnoreAttributes: false,
66
66
  transaction,
67
- attributes: [_this3.targetKey()],
68
- group: `${_this3.targetModel.name}.${_this3.targetKey()}`
67
+ attributes: [_this3.targetModel.primaryKeyAttribute],
68
+ group: `${_this3.targetModel.name}.${_this3.targetModel.primaryKeyAttribute}`
69
69
  }));
70
- const results = yield (0, _utils.handleAppendsQuery)({
71
- templateModel,
72
- queryPromises: findOptions.include.map(include => {
73
- return sourceModel[getAccessor](_objectSpread(_objectSpread({}, findOptions), {}, {
74
- include: [include]
75
- })).then(row => {
76
- return {
77
- rows: [row],
78
- include
79
- };
80
- });
81
- })
70
+ if (!templateModel) return null;
71
+ const eagerLoadingTree = _eagerLoadingTree.EagerLoadingTree.buildFromSequelizeOptions({
72
+ model: _this3.targetModel,
73
+ rootAttributes: findOptions.attributes,
74
+ includeOption: findOptions.include
82
75
  });
83
- return results[0];
76
+ yield eagerLoadingTree.load([templateModel.get(_this3.targetModel.primaryKeyAttribute)], transaction);
77
+ return eagerLoadingTree.root.instances[0];
84
78
  }
85
79
  return yield sourceModel[getAccessor](_objectSpread(_objectSpread({}, findOptions), {}, {
86
80
  transaction
@@ -104,6 +104,7 @@ declare class RelationRepositoryBuilder<R extends RelationRepository> {
104
104
  ArrayField: typeof ArrayFieldRepository;
105
105
  };
106
106
  constructor(collection: Collection, associationName: string);
107
+ of(id: string | number): R;
107
108
  protected builder(): {
108
109
  HasOne: typeof HasOneRepository;
109
110
  BelongsTo: typeof BelongsToRepository;
@@ -111,7 +112,12 @@ declare class RelationRepositoryBuilder<R extends RelationRepository> {
111
112
  HasMany: typeof HasManyRepository;
112
113
  ArrayField: typeof ArrayFieldRepository;
113
114
  };
114
- of(id: string | number): R;
115
+ }
116
+ export interface AggregateOptions {
117
+ method: 'avg' | 'count' | 'min' | 'max' | 'sum';
118
+ field?: string;
119
+ filter?: Filter;
120
+ distinct?: boolean;
115
121
  }
116
122
  export declare class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> implements IRepository {
117
123
  database: Database;
@@ -122,6 +128,9 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
122
128
  * return count by filter
123
129
  */
124
130
  count(countOptions?: CountOptions): Promise<number>;
131
+ aggregate(options: AggregateOptions & {
132
+ optionsTransformer?: (options: any) => any;
133
+ }): Promise<any>;
125
134
  /**
126
135
  * find
127
136
  * @param options
@@ -173,12 +182,6 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
173
182
  */
174
183
  relation<R extends RelationRepository>(association: string): RelationRepositoryBuilder<R>;
175
184
  buildQueryOptions(options: any): any;
176
- protected parseFilter(filter: Filter, options?: any): {
177
- where?: undefined;
178
- include?: undefined;
179
- } | {
180
- where: {};
181
- include: any[];
182
- };
185
+ protected parseFilter(filter: Filter, options?: any): any;
183
186
  protected getTransaction(options: any, autoGen?: boolean): Promise<any>;
184
187
  }