@nocobase/database 0.8.0-alpha.8 → 0.8.1-alpha.3

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 (122) hide show
  1. package/lib/collection.d.ts +7 -3
  2. package/lib/collection.js +126 -22
  3. package/lib/database.d.ts +13 -7
  4. package/lib/database.js +39 -8
  5. package/lib/decorators/must-have-filter-decorator.js +4 -0
  6. package/lib/decorators/transaction-decorator.js +1 -0
  7. package/lib/features/ReferencesMap.js +14 -9
  8. package/lib/field-repository/array-field-repository.d.ts +28 -0
  9. package/lib/field-repository/array-field-repository.js +208 -0
  10. package/lib/fields/array-field.d.ts +1 -1
  11. package/lib/fields/array-field.js +15 -11
  12. package/lib/fields/belongs-to-field.d.ts +9 -1
  13. package/lib/fields/belongs-to-field.js +21 -7
  14. package/lib/fields/belongs-to-many-field.d.ts +4 -0
  15. package/lib/fields/belongs-to-many-field.js +24 -0
  16. package/lib/fields/field.d.ts +3 -2
  17. package/lib/fields/field.js +19 -13
  18. package/lib/fields/has-many-field.d.ts +2 -1
  19. package/lib/fields/has-many-field.js +18 -10
  20. package/lib/fields/has-one-field.d.ts +1 -0
  21. package/lib/fields/has-one-field.js +22 -10
  22. package/lib/fields/index.d.ts +3 -3
  23. package/lib/fields/index.js +14 -34
  24. package/lib/fields/relation-field.d.ts +2 -1
  25. package/lib/fields/relation-field.js +16 -0
  26. package/lib/fields/set-field.d.ts +10 -0
  27. package/lib/fields/set-field.js +35 -0
  28. package/lib/fields/sort-field.d.ts +1 -1
  29. package/lib/fields/sort-field.js +1 -1
  30. package/lib/filter-match.d.ts +1 -0
  31. package/lib/filter-match.js +84 -0
  32. package/lib/filter-parser.d.ts +2 -2
  33. package/lib/index.d.ts +5 -1
  34. package/lib/index.js +56 -0
  35. package/lib/inherited-collection.d.ts +13 -0
  36. package/lib/inherited-collection.js +210 -0
  37. package/lib/inherited-map.d.ts +21 -0
  38. package/lib/inherited-map.js +203 -0
  39. package/lib/model-hook.d.ts +1 -1
  40. package/lib/model.d.ts +1 -0
  41. package/lib/model.js +47 -0
  42. package/lib/operators/array.d.ts +1 -25
  43. package/lib/operators/association.d.ts +1 -9
  44. package/lib/operators/boolean.d.ts +1 -12
  45. package/lib/operators/date.d.ts +1 -33
  46. package/lib/operators/empty.d.ts +2 -25
  47. package/lib/operators/ne.d.ts +1 -13
  48. package/lib/operators/notIn.d.ts +1 -9
  49. package/lib/options-parser.d.ts +3 -2
  50. package/lib/options-parser.js +16 -1
  51. package/lib/relation-repository/relation-repository.d.ts +2 -2
  52. package/lib/relation-repository/single-relation-repository.js +2 -2
  53. package/lib/repository.d.ts +18 -10
  54. package/lib/repository.js +172 -38
  55. package/lib/sync-runner.d.ts +4 -0
  56. package/lib/sync-runner.js +181 -0
  57. package/lib/types.d.ts +2 -2
  58. package/lib/update-associations.d.ts +1 -0
  59. package/lib/update-associations.js +21 -2
  60. package/lib/update-guard.d.ts +3 -3
  61. package/lib/utils.js +5 -0
  62. package/package.json +4 -4
  63. package/src/__tests__/bigint.test.ts +48 -0
  64. package/src/__tests__/collection.test.ts +48 -13
  65. package/src/__tests__/database.test.ts +10 -0
  66. package/src/__tests__/field-repository/array-field-repository.test.ts +94 -0
  67. package/src/__tests__/fields/set.test.ts +37 -0
  68. package/src/__tests__/filter-match.test.ts +52 -0
  69. package/src/__tests__/inhertits/collection-inherits-sync.test.ts +38 -0
  70. package/src/__tests__/inhertits/collection-inherits.test.ts +994 -0
  71. package/src/__tests__/inhertits/helper.ts +3 -0
  72. package/src/__tests__/inhertits/inherited-map.test.ts +27 -0
  73. package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +2 -0
  74. package/src/__tests__/relation-repository/has-many-repository.test.ts +1 -1
  75. package/src/__tests__/repository/destroy.test.ts +122 -1
  76. package/src/__tests__/repository/update-many.test.ts +57 -0
  77. package/src/__tests__/update-association-values.test.ts +232 -0
  78. package/src/__tests__/update-associations.test.ts +6 -1
  79. package/src/collection.ts +90 -8
  80. package/src/database.ts +53 -14
  81. package/src/decorators/must-have-filter-decorator.ts +4 -0
  82. package/src/decorators/transaction-decorator.ts +1 -0
  83. package/src/features/ReferencesMap.ts +20 -9
  84. package/src/features/referential-integrity-check.ts +1 -0
  85. package/src/field-repository/array-field-repository.ts +155 -0
  86. package/src/fields/array-field.ts +4 -4
  87. package/src/fields/belongs-to-field.ts +26 -10
  88. package/src/fields/belongs-to-many-field.ts +34 -0
  89. package/src/fields/field.ts +26 -13
  90. package/src/fields/has-many-field.ts +17 -9
  91. package/src/fields/has-one-field.ts +23 -9
  92. package/src/fields/index.ts +5 -5
  93. package/src/fields/relation-field.ts +16 -0
  94. package/src/fields/set-field.ts +25 -0
  95. package/src/fields/sort-field.ts +5 -4
  96. package/src/filter-match.ts +49 -0
  97. package/src/filter-parser.ts +2 -2
  98. package/src/index.ts +5 -2
  99. package/src/inherited-collection.ts +112 -0
  100. package/src/inherited-map.ts +97 -0
  101. package/src/model-hook.ts +2 -3
  102. package/src/model.ts +43 -3
  103. package/src/operators/array.ts +1 -1
  104. package/src/operators/association.ts +1 -1
  105. package/src/operators/boolean.ts +1 -1
  106. package/src/operators/date.ts +1 -1
  107. package/src/operators/empty.ts +1 -1
  108. package/src/operators/ne.ts +1 -1
  109. package/src/operators/notIn.ts +2 -1
  110. package/src/options-parser.ts +20 -4
  111. package/src/relation-repository/relation-repository.ts +2 -2
  112. package/src/relation-repository/single-relation-repository.ts +2 -2
  113. package/src/repository.ts +144 -30
  114. package/src/sync-runner.ts +162 -0
  115. package/src/types.ts +2 -2
  116. package/src/update-associations.ts +23 -7
  117. package/src/update-guard.ts +3 -3
  118. package/src/utils.ts +5 -1
  119. package/lib/fields/sequence-field.d.ts +0 -31
  120. package/lib/fields/sequence-field.js +0 -299
  121. package/src/__tests__/fields/sequence-field.test.ts +0 -455
  122. package/src/fields/sequence-field.ts +0 -200
@@ -143,6 +143,10 @@ class OptionsParser {
143
143
  return filterParams;
144
144
  }
145
145
 
146
+ inheritFromSubQuery() {
147
+ return [_sequelize().Sequelize.literal(`(select relname from pg_class where pg_class.oid = "${this.collection.name}".tableoid)`), '__tableName'];
148
+ }
149
+
146
150
  parseFields(filterParams) {
147
151
  var _this$options3, _this$options4, _this$options5;
148
152
 
@@ -153,6 +157,10 @@ class OptionsParser {
153
157
  exclude: []
154
158
  }; // out put all fields by default
155
159
 
160
+ if (this.collection.isParent()) {
161
+ attributes.include.push(this.inheritFromSubQuery());
162
+ }
163
+
156
164
  if ((_this$options4 = this.options) === null || _this$options4 === void 0 ? void 0 : _this$options4.fields) {
157
165
  // 将fields拆分为 attributes 和 appends
158
166
  var _iterator2 = _createForOfIteratorHelper(this.options.fields),
@@ -167,7 +175,14 @@ class OptionsParser {
167
175
  appends.push(field);
168
176
  } else {
169
177
  // field is model attribute, change attributes to array type
170
- if (!Array.isArray(attributes)) attributes = [];
178
+ if (!Array.isArray(attributes)) {
179
+ attributes = [];
180
+
181
+ if (this.collection.isParent()) {
182
+ attributes.push(this.inheritFromSubQuery());
183
+ }
184
+ }
185
+
171
186
  attributes.push(field);
172
187
  }
173
188
  }
@@ -1,4 +1,4 @@
1
- import { Association, ModelCtor, Transaction } from 'sequelize';
1
+ import { Association, ModelStatic, Transaction } from 'sequelize';
2
2
  import { Collection } from '../collection';
3
3
  import Database from '../database';
4
4
  import { RelationField } from '../fields/relation-field';
@@ -8,7 +8,7 @@ export declare const transaction: (transactionInjector?: any) => (target: any, n
8
8
  export declare abstract class RelationRepository {
9
9
  sourceCollection: Collection;
10
10
  association: Association;
11
- targetModel: ModelCtor<any>;
11
+ targetModel: ModelStatic<any>;
12
12
  targetCollection: Collection;
13
13
  associationName: string;
14
14
  associationField: RelationField;
@@ -17,10 +17,10 @@ function _lodash() {
17
17
 
18
18
  var _updateAssociations = require("../update-associations");
19
19
 
20
- var _relationRepository = require("./relation-repository");
21
-
22
20
  var _utils = require("../utils");
23
21
 
22
+ var _relationRepository = require("./relation-repository");
23
+
24
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
25
 
26
26
  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; }
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" />
2
- import { Association, BulkCreateOptions, CountOptions as SequelizeCountOptions, CreateOptions as SequelizeCreateOptions, DestroyOptions as SequelizeDestroyOptions, FindAndCountOptions as SequelizeAndCountOptions, FindOptions as SequelizeFindOptions, ModelCtor, Transactionable, UpdateOptions as SequelizeUpdateOptions } from 'sequelize';
3
- import { WhereOperators } from 'sequelize/types/lib/model';
2
+ import { Association, BulkCreateOptions, CountOptions as SequelizeCountOptions, CreateOptions as SequelizeCreateOptions, DestroyOptions as SequelizeDestroyOptions, FindAndCountOptions as SequelizeAndCountOptions, FindOptions as SequelizeFindOptions, ModelStatic, Transactionable, UpdateOptions as SequelizeUpdateOptions, WhereOperators } from 'sequelize';
4
3
  import { Collection } from './collection';
5
4
  import { Database } from './database';
5
+ import { ArrayFieldRepository } from './field-repository/array-field-repository';
6
6
  import { Model } from './model';
7
7
  import operators from './operators';
8
8
  import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
@@ -46,10 +46,10 @@ export declare type WhiteList = string[];
46
46
  export declare type BlackList = string[];
47
47
  export declare type AssociationKeysToBeUpdate = string[];
48
48
  export declare type Values = any;
49
- export interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
49
+ export declare type CountOptions = Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'> & Transactionable & {
50
50
  filter?: Filter;
51
51
  context?: any;
52
- }
52
+ } & FilterByTk;
53
53
  export interface FilterByTk {
54
54
  filterByTk?: TargetKey;
55
55
  }
@@ -86,15 +86,21 @@ export interface UpdateOptions extends Omit<SequelizeUpdateOptions, 'where'> {
86
86
  updateAssociationValues?: AssociationKeysToBeUpdate;
87
87
  context?: any;
88
88
  }
89
+ interface UpdateManyOptions extends UpdateOptions {
90
+ records: Values[];
91
+ }
89
92
  declare class RelationRepositoryBuilder<R extends RelationRepository> {
90
93
  collection: Collection;
91
94
  associationName: string;
92
- association: Association;
95
+ association: Association | {
96
+ associationType: string;
97
+ };
93
98
  builderMap: {
94
99
  HasOne: typeof HasOneRepository;
95
100
  BelongsTo: typeof BelongsToRepository;
96
101
  BelongsToMany: typeof BelongsToManyRepository;
97
102
  HasMany: typeof HasManyRepository;
103
+ ArrayField: typeof ArrayFieldRepository;
98
104
  };
99
105
  constructor(collection: Collection, associationName: string);
100
106
  protected builder(): {
@@ -102,13 +108,14 @@ declare class RelationRepositoryBuilder<R extends RelationRepository> {
102
108
  BelongsTo: typeof BelongsToRepository;
103
109
  BelongsToMany: typeof BelongsToManyRepository;
104
110
  HasMany: typeof HasManyRepository;
111
+ ArrayField: typeof ArrayFieldRepository;
105
112
  };
106
113
  of(id: string | number): R;
107
114
  }
108
115
  export declare class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> implements IRepository {
109
116
  database: Database;
110
117
  collection: Collection;
111
- model: ModelCtor<Model>;
118
+ model: ModelStatic<Model>;
112
119
  constructor(collection: Collection);
113
120
  /**
114
121
  * return count by filter
@@ -118,7 +125,7 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
118
125
  * find
119
126
  * @param options
120
127
  */
121
- find(options?: FindOptions): Promise<Model<any, any>[]>;
128
+ find(options?: FindOptions): Promise<any>;
122
129
  /**
123
130
  * find and count
124
131
  * @param options
@@ -134,7 +141,7 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
134
141
  *
135
142
  * @param options
136
143
  */
137
- findOne(options?: FindOneOptions): Promise<Model<any, any>>;
144
+ findOne(options?: FindOneOptions): Promise<any>;
138
145
  /**
139
146
  * Save instance to database
140
147
  *
@@ -157,13 +164,14 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
157
164
  */
158
165
  update(options: UpdateOptions & {
159
166
  forceUpdate?: boolean;
160
- }): Promise<Model<any, any>[]>;
167
+ }): Promise<any>;
168
+ updateMany(options: UpdateManyOptions): Promise<any[]>;
161
169
  destroy(options?: TargetKey | TargetKey[] | DestroyOptions): any;
162
170
  /**
163
171
  * @param association target association
164
172
  */
165
173
  relation<R extends RelationRepository>(association: string): RelationRepositoryBuilder<R>;
166
- protected buildQueryOptions(options: any): any;
174
+ buildQueryOptions(options: any): any;
167
175
  protected parseFilter(filter: Filter, options?: any): {
168
176
  where?: undefined;
169
177
  include?: undefined;
package/lib/repository.js CHANGED
@@ -29,6 +29,10 @@ var _mustHaveFilterDecorator = _interopRequireDefault(require("./decorators/must
29
29
 
30
30
  var _transactionDecorator = require("./decorators/transaction-decorator");
31
31
 
32
+ var _arrayFieldRepository = require("./field-repository/array-field-repository");
33
+
34
+ var _fields = require("./fields");
35
+
32
36
  var _filterParser = _interopRequireDefault(require("./filter-parser"));
33
37
 
34
38
  var _optionsParser = require("./options-parser");
@@ -92,11 +96,22 @@ class RelationRepositoryBuilder {
92
96
  HasOne: _hasoneRepository.HasOneRepository,
93
97
  BelongsTo: _belongsToRepository.BelongsToRepository,
94
98
  BelongsToMany: _belongsToManyRepository.BelongsToManyRepository,
95
- HasMany: _hasmanyRepository.HasManyRepository
99
+ HasMany: _hasmanyRepository.HasManyRepository,
100
+ ArrayField: _arrayFieldRepository.ArrayFieldRepository
96
101
  };
97
102
  this.collection = collection;
98
103
  this.associationName = associationName;
99
104
  this.association = this.collection.model.associations[this.associationName];
105
+
106
+ if (!this.association) {
107
+ const field = collection.getField(associationName);
108
+
109
+ if (field && field instanceof _fields.ArrayField) {
110
+ this.association = {
111
+ associationType: 'ArrayField'
112
+ };
113
+ }
114
+ }
100
115
  }
101
116
 
102
117
  builder() {
@@ -128,6 +143,8 @@ class Repository {
128
143
  var _this = this;
129
144
 
130
145
  return _asyncToGenerator(function* () {
146
+ var _queryOptions$include;
147
+
131
148
  let options = countOptions ? _lodash().default.clone(countOptions) : {};
132
149
  const transaction = yield _this.getTransaction(options);
133
150
 
@@ -135,10 +152,26 @@ class Repository {
135
152
  options = _objectSpread(_objectSpread({}, options), _this.parseFilter(countOptions.filter, countOptions));
136
153
  }
137
154
 
138
- const count = yield _this.collection.model.count(_objectSpread(_objectSpread({}, options), {}, {
139
- distinct: true,
155
+ if (countOptions === null || countOptions === void 0 ? void 0 : countOptions.filterByTk) {
156
+ options['where'] = {
157
+ [_sequelize().Op.and]: [options['where'] || {}, {
158
+ [_this.collection.filterTargetKey]: options.filterByTk
159
+ }]
160
+ };
161
+ }
162
+
163
+ const queryOptions = _objectSpread(_objectSpread({}, options), {}, {
164
+ distinct: Boolean(_this.collection.model.primaryKeyAttribute)
165
+ });
166
+
167
+ if (((_queryOptions$include = queryOptions.include) === null || _queryOptions$include === void 0 ? void 0 : _queryOptions$include.length) === 0) {
168
+ delete queryOptions.include;
169
+ }
170
+
171
+ const count = yield _this.collection.model.count(_objectSpread(_objectSpread({}, queryOptions), {}, {
140
172
  transaction
141
- }));
173
+ })); // @ts-ignore
174
+
142
175
  return count;
143
176
  })();
144
177
  }
@@ -159,6 +192,8 @@ class Repository {
159
192
  subQuery: false
160
193
  }, _this2.buildQueryOptions(options));
161
194
 
195
+ let rows;
196
+
162
197
  if (opts.include && opts.include.length > 0) {
163
198
  // @ts-ignore
164
199
  const primaryKeyField = model.primaryKeyField || model.primaryKeyAttribute;
@@ -166,7 +201,12 @@ class Repository {
166
201
  includeIgnoreAttributes: false,
167
202
  attributes: [primaryKeyField],
168
203
  group: `${model.name}.${primaryKeyField}`,
169
- transaction
204
+ transaction,
205
+ include: opts.include.filter(include => {
206
+ var _JSON$stringify;
207
+
208
+ return Object.keys(include.where || {}).length > 0 || ((_JSON$stringify = JSON.stringify(opts === null || opts === void 0 ? void 0 : opts.filter)) === null || _JSON$stringify === void 0 ? void 0 : _JSON$stringify.includes(include.association));
209
+ })
170
210
  }))).map(row => {
171
211
  return {
172
212
  row,
@@ -178,31 +218,64 @@ class Repository {
178
218
  return [];
179
219
  }
180
220
 
221
+ const templateModel = yield model.findOne(_objectSpread(_objectSpread({}, opts), {}, {
222
+ includeIgnoreAttributes: false,
223
+ attributes: [primaryKeyField],
224
+ group: `${model.name}.${primaryKeyField}`,
225
+ transaction,
226
+ limit: 1,
227
+ offset: 0
228
+ }));
181
229
  const where = {
182
230
  [primaryKeyField]: {
183
231
  [_sequelize().Op.in]: ids.map(id => id['pk'])
184
232
  }
185
233
  };
186
- return yield (0, _utils.handleAppendsQuery)({
234
+ rows = yield (0, _utils.handleAppendsQuery)({
187
235
  queryPromises: opts.include.map(include => {
188
- return model.findAll(_objectSpread(_objectSpread({}, (0, _lodash().omit)(opts, ['limit', 'offset'])), {}, {
236
+ const options = _objectSpread(_objectSpread({}, (0, _lodash().omit)(opts, ['limit', 'offset'])), {}, {
189
237
  include: include,
190
238
  where,
191
239
  transaction
192
- })).then(rows => {
240
+ });
241
+
242
+ return model.findAll(options).then(rows => {
193
243
  return {
194
244
  rows,
195
245
  include
196
246
  };
197
247
  });
198
248
  }),
199
- templateModel: ids[0].row
249
+ templateModel: templateModel
200
250
  });
251
+ } else {
252
+ rows = yield model.findAll(_objectSpread(_objectSpread({}, opts), {}, {
253
+ transaction
254
+ }));
201
255
  }
202
256
 
203
- return yield model.findAll(_objectSpread(_objectSpread({}, opts), {}, {
204
- transaction
205
- }));
257
+ if (_this2.collection.isParent()) {
258
+ var _iterator = _createForOfIteratorHelper(rows),
259
+ _step;
260
+
261
+ try {
262
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
263
+ const row = _step.value;
264
+
265
+ const rowCollectionName = _this2.database.tableNameCollectionMap.get(row.get('__tableName')).name;
266
+
267
+ row.set('__collection', rowCollectionName, {
268
+ raw: true
269
+ });
270
+ }
271
+ } catch (err) {
272
+ _iterator.e(err);
273
+ } finally {
274
+ _iterator.f();
275
+ }
276
+ }
277
+
278
+ return rows;
206
279
  })();
207
280
  }
208
281
  /**
@@ -318,12 +391,12 @@ class Repository {
318
391
  const records = options.records;
319
392
  const instances = [];
320
393
 
321
- var _iterator = _createForOfIteratorHelper(records),
322
- _step;
394
+ var _iterator2 = _createForOfIteratorHelper(records),
395
+ _step2;
323
396
 
324
397
  try {
325
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
326
- const values = _step.value;
398
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
399
+ const values = _step2.value;
327
400
  const instance = yield _this6.create({
328
401
  values,
329
402
  transaction
@@ -331,9 +404,9 @@ class Repository {
331
404
  instances.push(instance);
332
405
  }
333
406
  } catch (err) {
334
- _iterator.e(err);
407
+ _iterator2.e(err);
335
408
  } finally {
336
- _iterator.f();
409
+ _iterator2.f();
337
410
  }
338
411
 
339
412
  return instances;
@@ -351,6 +424,12 @@ class Repository {
351
424
  var _this7 = this;
352
425
 
353
426
  return _asyncToGenerator(function* () {
427
+ if (Array.isArray(options.values)) {
428
+ return _this7.updateMany(_objectSpread(_objectSpread({}, options), {}, {
429
+ records: options.values
430
+ }));
431
+ }
432
+
354
433
  const transaction = yield _this7.getTransaction(options);
355
434
 
356
435
  const guard = _updateGuard.UpdateGuard.fromOptions(_this7.model, options);
@@ -363,30 +442,30 @@ class Repository {
363
442
  transaction
364
443
  }));
365
444
 
366
- var _iterator2 = _createForOfIteratorHelper(instances),
367
- _step2;
445
+ var _iterator3 = _createForOfIteratorHelper(instances),
446
+ _step3;
368
447
 
369
448
  try {
370
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
371
- const instance = _step2.value;
449
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
450
+ const instance = _step3.value;
372
451
  yield (0, _updateAssociations.updateModelByValues)(instance, values, _objectSpread(_objectSpread({}, options), {}, {
373
452
  sanitized: true,
374
453
  transaction
375
454
  }));
376
455
  }
377
456
  } catch (err) {
378
- _iterator2.e(err);
457
+ _iterator3.e(err);
379
458
  } finally {
380
- _iterator2.f();
459
+ _iterator3.f();
381
460
  }
382
461
 
383
462
  if (options.hooks !== false) {
384
- var _iterator3 = _createForOfIteratorHelper(instances),
385
- _step3;
463
+ var _iterator4 = _createForOfIteratorHelper(instances),
464
+ _step4;
386
465
 
387
466
  try {
388
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
389
- const instance = _step3.value;
467
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
468
+ const instance = _step4.value;
390
469
  yield _this7.database.emitAsync(`${_this7.collection.name}.afterUpdateWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
391
470
  transaction
392
471
  }));
@@ -396,9 +475,9 @@ class Repository {
396
475
  instance.clearChangedWithAssociations();
397
476
  }
398
477
  } catch (err) {
399
- _iterator3.e(err);
478
+ _iterator4.e(err);
400
479
  } finally {
401
- _iterator3.f();
480
+ _iterator4.f();
402
481
  }
403
482
  }
404
483
 
@@ -406,12 +485,49 @@ class Repository {
406
485
  })();
407
486
  }
408
487
 
409
- destroy(options) {
488
+ updateMany(options) {
410
489
  var _this8 = this;
411
490
 
412
491
  return _asyncToGenerator(function* () {
413
492
  const transaction = yield _this8.getTransaction(options);
414
- const modelFilterKey = _this8.collection.filterTargetKey;
493
+ const records = options.records;
494
+ const instances = [];
495
+
496
+ var _iterator5 = _createForOfIteratorHelper(records),
497
+ _step5;
498
+
499
+ try {
500
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
501
+ const values = _step5.value;
502
+ const filterByTk = values[_this8.model.primaryKeyAttribute];
503
+
504
+ if (!filterByTk) {
505
+ throw new Error('filterByTk invalid');
506
+ }
507
+
508
+ const instance = yield _this8.update({
509
+ values,
510
+ filterByTk,
511
+ transaction
512
+ });
513
+ instances.push(instance);
514
+ }
515
+ } catch (err) {
516
+ _iterator5.e(err);
517
+ } finally {
518
+ _iterator5.f();
519
+ }
520
+
521
+ return instances;
522
+ })();
523
+ }
524
+
525
+ destroy(options) {
526
+ var _this9 = this;
527
+
528
+ return _asyncToGenerator(function* () {
529
+ const transaction = yield _this9.getTransaction(options);
530
+ const modelFilterKey = _this9.collection.filterTargetKey;
415
531
  options = options;
416
532
 
417
533
  if (options['individualHooks'] === undefined) {
@@ -420,8 +536,16 @@ class Repository {
420
536
 
421
537
  const filterByTk = options.filterByTk && !_lodash().default.isArray(options.filterByTk) ? [options.filterByTk] : options.filterByTk;
422
538
 
539
+ if (_this9.collection.model.primaryKeyAttributes.length !== 1 && filterByTk && !_lodash().default.get(_this9.collection.options, 'filterTargetKey')) {
540
+ if (_this9.collection.model.primaryKeyAttributes.length > 1) {
541
+ throw new Error(`filterByTk is not supported for composite primary key`);
542
+ } else {
543
+ throw new Error(`filterByTk is not supported for collection that has no primary key`);
544
+ }
545
+ }
546
+
423
547
  if (filterByTk && !options.filter) {
424
- return yield _this8.model.destroy(_objectSpread(_objectSpread({}, options), {}, {
548
+ return yield _this9.model.destroy(_objectSpread(_objectSpread({}, options), {}, {
425
549
  where: {
426
550
  [modelFilterKey]: {
427
551
  [_sequelize().Op.in]: filterByTk
@@ -432,7 +556,15 @@ class Repository {
432
556
  }
433
557
 
434
558
  if (options.filter) {
435
- let pks = (yield _this8.find({
559
+ if (_this9.collection.model.primaryKeyAttributes.length !== 1 && !_lodash().default.get(_this9.collection.options, 'filterTargetKey')) {
560
+ const queryOptions = _objectSpread({}, _this9.buildQueryOptions(options));
561
+
562
+ return yield _this9.model.destroy(_objectSpread(_objectSpread({}, queryOptions), {}, {
563
+ transaction
564
+ }));
565
+ }
566
+
567
+ let pks = (yield _this9.find({
436
568
  filter: options.filter,
437
569
  transaction
438
570
  })).map(instance => instance.get(modelFilterKey));
@@ -441,14 +573,14 @@ class Repository {
441
573
  pks = _lodash().default.intersection(pks.map(i => `${i}`), filterByTk.map(i => `${i}`));
442
574
  }
443
575
 
444
- return yield _this8.destroy(_objectSpread(_objectSpread({}, _lodash().default.omit(options, 'filter')), {}, {
576
+ return yield _this9.destroy(_objectSpread(_objectSpread({}, _lodash().default.omit(options, 'filter')), {}, {
445
577
  filterByTk: pks,
446
578
  transaction
447
579
  }));
448
580
  }
449
581
 
450
582
  if (options.truncate) {
451
- return yield _this8.model.destroy(_objectSpread(_objectSpread({}, options), {}, {
583
+ return yield _this9.model.destroy(_objectSpread(_objectSpread({}, options), {}, {
452
584
  truncate: true,
453
585
  transaction
454
586
  }));
@@ -486,7 +618,7 @@ class Repository {
486
618
  }
487
619
 
488
620
  getTransaction(options, autoGen = false) {
489
- var _this9 = this;
621
+ var _this10 = this;
490
622
 
491
623
  return _asyncToGenerator(function* () {
492
624
  if (_lodash().default.isPlainObject(options) && options.transaction) {
@@ -494,7 +626,7 @@ class Repository {
494
626
  }
495
627
 
496
628
  if (autoGen) {
497
- return yield _this9.model.sequelize.transaction();
629
+ return yield _this10.model.sequelize.transaction();
498
630
  }
499
631
 
500
632
  return null;
@@ -511,6 +643,8 @@ __decorate([transaction()], Repository.prototype, "createMany", null);
511
643
 
512
644
  __decorate([transaction(), (0, _mustHaveFilterDecorator.default)()], Repository.prototype, "update", null);
513
645
 
646
+ __decorate([transaction()], Repository.prototype, "updateMany", null);
647
+
514
648
  __decorate([transaction((args, transaction) => {
515
649
  return {
516
650
  filterByTk: args[0],
@@ -0,0 +1,4 @@
1
+ export declare class SyncRunner {
2
+ static syncInheritModel(model: any, options: any): Promise<void>;
3
+ static createTable(tableName: any, attributes: any, options: any, model: any, parentTables: any): Promise<any>;
4
+ }