@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.
- package/lib/collection.d.ts +9 -9
- package/lib/collection.js +100 -96
- package/lib/database.d.ts +4 -4
- package/lib/database.js +25 -53
- package/lib/eager-loading/eager-loading-tree.d.ts +23 -0
- package/lib/eager-loading/eager-loading-tree.js +338 -0
- package/lib/filter-parser.d.ts +1 -7
- package/lib/filter-parser.js +27 -7
- package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +5 -0
- package/lib/listeners/append-child-collection-name-after-repository-find.js +40 -0
- package/lib/listeners/index.js +2 -0
- package/lib/mock-database.js +3 -1
- package/lib/operators/string.js +1 -1
- package/lib/options-parser.js +4 -0
- package/lib/query-interface/postgres-query-interface.js +2 -2
- package/lib/relation-repository/belongs-to-many-repository.d.ts +2 -1
- package/lib/relation-repository/belongs-to-many-repository.js +58 -37
- package/lib/relation-repository/hasmany-repository.d.ts +2 -1
- package/lib/relation-repository/hasmany-repository.js +31 -16
- package/lib/relation-repository/multiple-relation-repository.js +8 -26
- package/lib/relation-repository/relation-repository.d.ts +1 -7
- package/lib/relation-repository/single-relation-repository.d.ts +1 -1
- package/lib/relation-repository/single-relation-repository.js +10 -16
- package/lib/repository.d.ts +11 -8
- package/lib/repository.js +104 -89
- package/lib/sql-parser/postgres.js +41 -0
- package/lib/update-guard.d.ts +1 -1
- package/lib/update-guard.js +16 -13
- package/lib/utils.d.ts +0 -7
- package/lib/utils.js +0 -76
- package/package.json +4 -4
- package/src/__tests__/eager-loading/eager-loading-tree.test.ts +393 -0
- package/src/__tests__/migrator.test.ts +4 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +1 -0
- package/src/__tests__/repository/aggregation.test.ts +297 -0
- package/src/__tests__/repository/count.test.ts +1 -1
- package/src/__tests__/repository/find.test.ts +10 -1
- package/src/__tests__/repository.test.ts +30 -0
- package/src/__tests__/update-guard.test.ts +13 -0
- package/src/collection.ts +74 -66
- package/src/database.ts +26 -42
- package/src/eager-loading/eager-loading-tree.ts +304 -0
- package/src/filter-parser.ts +16 -2
- package/src/listeners/adjacency-list.ts +1 -3
- package/src/listeners/append-child-collection-name-after-repository-find.ts +31 -0
- package/src/listeners/index.ts +2 -0
- package/src/mock-database.ts +3 -1
- package/src/operators/notIn.ts +1 -0
- package/src/operators/string.ts +1 -1
- package/src/options-parser.ts +5 -0
- package/src/query-interface/postgres-query-interface.ts +1 -1
- package/src/relation-repository/belongs-to-many-repository.ts +33 -1
- package/src/relation-repository/hasmany-repository.ts +17 -0
- package/src/relation-repository/multiple-relation-repository.ts +14 -19
- package/src/relation-repository/single-relation-repository.ts +13 -15
- package/src/repository.ts +79 -36
- package/src/sql-parser/postgres.js +25505 -0
- package/src/update-guard.ts +21 -16
- package/src/utils.ts +0 -61
package/lib/repository.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.Repository = void 0;
|
|
7
7
|
function _lodash() {
|
|
8
|
-
const data =
|
|
8
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
9
9
|
_lodash = function _lodash() {
|
|
10
10
|
return data;
|
|
11
11
|
};
|
|
@@ -30,10 +30,8 @@ var _hasmanyRepository = require("./relation-repository/hasmany-repository");
|
|
|
30
30
|
var _hasoneRepository = require("./relation-repository/hasone-repository");
|
|
31
31
|
var _updateAssociations = require("./update-associations");
|
|
32
32
|
var _updateGuard = require("./update-guard");
|
|
33
|
-
var
|
|
33
|
+
var _eagerLoadingTree = require("./eager-loading/eager-loading-tree");
|
|
34
34
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
35
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
36
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
37
35
|
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; } } }; }
|
|
38
36
|
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); }
|
|
39
37
|
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; }
|
|
@@ -79,9 +77,6 @@ class RelationRepositoryBuilder {
|
|
|
79
77
|
}
|
|
80
78
|
}
|
|
81
79
|
}
|
|
82
|
-
builder() {
|
|
83
|
-
return this.builderMap;
|
|
84
|
-
}
|
|
85
80
|
of(id) {
|
|
86
81
|
if (!this.association) {
|
|
87
82
|
return;
|
|
@@ -89,6 +84,9 @@ class RelationRepositoryBuilder {
|
|
|
89
84
|
const klass = this.builder()[this.association.associationType];
|
|
90
85
|
return new klass(this.collection, this.associationName, id);
|
|
91
86
|
}
|
|
87
|
+
builder() {
|
|
88
|
+
return this.builderMap;
|
|
89
|
+
}
|
|
92
90
|
}
|
|
93
91
|
class Repository {
|
|
94
92
|
constructor(collection) {
|
|
@@ -131,18 +129,56 @@ class Repository {
|
|
|
131
129
|
return count;
|
|
132
130
|
})();
|
|
133
131
|
}
|
|
132
|
+
aggregate(options) {
|
|
133
|
+
var _this2 = this;
|
|
134
|
+
return _asyncToGenerator(function* () {
|
|
135
|
+
var _options$optionsTrans;
|
|
136
|
+
const method = options.method,
|
|
137
|
+
field = options.field;
|
|
138
|
+
const queryOptions = _this2.buildQueryOptions(_objectSpread(_objectSpread({}, options), {}, {
|
|
139
|
+
fields: []
|
|
140
|
+
}));
|
|
141
|
+
(_options$optionsTrans = options.optionsTransformer) === null || _options$optionsTrans === void 0 ? void 0 : _options$optionsTrans.call(options, queryOptions);
|
|
142
|
+
const hasAssociationFilter = () => {
|
|
143
|
+
if (queryOptions.include && queryOptions.include.length > 0) {
|
|
144
|
+
const filterInclude = queryOptions.include.filter(include => {
|
|
145
|
+
var _JSON$stringify;
|
|
146
|
+
return Object.keys(include.where || {}).length > 0 || ((_JSON$stringify = JSON.stringify(queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.filter)) === null || _JSON$stringify === void 0 ? void 0 : _JSON$stringify.includes(include.association));
|
|
147
|
+
});
|
|
148
|
+
return filterInclude.length > 0;
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
};
|
|
152
|
+
if (hasAssociationFilter()) {
|
|
153
|
+
const primaryKeyField = _this2.model.primaryKeyAttribute;
|
|
154
|
+
const queryInterface = _this2.database.sequelize.getQueryInterface();
|
|
155
|
+
const findOptions = _objectSpread(_objectSpread({}, queryOptions), {}, {
|
|
156
|
+
raw: true,
|
|
157
|
+
includeIgnoreAttributes: false,
|
|
158
|
+
attributes: [[_sequelize().Sequelize.literal(`DISTINCT ${queryInterface.quoteIdentifiers(`${_this2.collection.name}.${primaryKeyField}`)}`), primaryKeyField]]
|
|
159
|
+
});
|
|
160
|
+
const ids = yield _this2.model.findAll(findOptions);
|
|
161
|
+
return yield _this2.model.aggregate(field, method, _objectSpread(_objectSpread({}, _lodash().default.omit(queryOptions, ['where', 'include'])), {}, {
|
|
162
|
+
where: {
|
|
163
|
+
[primaryKeyField]: ids.map(node => node[primaryKeyField])
|
|
164
|
+
}
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
return yield _this2.model.aggregate(field, method, queryOptions);
|
|
168
|
+
})();
|
|
169
|
+
}
|
|
134
170
|
/**
|
|
135
171
|
* find
|
|
136
172
|
* @param options
|
|
137
173
|
*/
|
|
138
174
|
find(options = {}) {
|
|
139
|
-
var
|
|
175
|
+
var _this3 = this;
|
|
140
176
|
return _asyncToGenerator(function* () {
|
|
141
|
-
const model =
|
|
142
|
-
const transaction = yield
|
|
177
|
+
const model = _this3.collection.model;
|
|
178
|
+
const transaction = yield _this3.getTransaction(options);
|
|
143
179
|
const opts = _objectSpread({
|
|
144
180
|
subQuery: false
|
|
145
|
-
},
|
|
181
|
+
}, _this3.buildQueryOptions(options));
|
|
146
182
|
let rows;
|
|
147
183
|
if (opts.include && opts.include.length > 0) {
|
|
148
184
|
// @ts-ignore
|
|
@@ -154,8 +190,8 @@ class Repository {
|
|
|
154
190
|
group: `${model.name}.${primaryKeyField}`,
|
|
155
191
|
transaction,
|
|
156
192
|
include: opts.include.filter(include => {
|
|
157
|
-
var _JSON$
|
|
158
|
-
return Object.keys(include.where || {}).length > 0 || ((_JSON$
|
|
193
|
+
var _JSON$stringify2;
|
|
194
|
+
return Object.keys(include.where || {}).length > 0 || ((_JSON$stringify2 = JSON.stringify(opts === null || opts === void 0 ? void 0 : opts.filter)) === null || _JSON$stringify2 === void 0 ? void 0 : _JSON$stringify2.includes(include.association));
|
|
159
195
|
})
|
|
160
196
|
}))).map(row => {
|
|
161
197
|
return {
|
|
@@ -166,44 +202,23 @@ class Repository {
|
|
|
166
202
|
if (ids.length == 0) {
|
|
167
203
|
return [];
|
|
168
204
|
}
|
|
169
|
-
// find
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
limit: 1,
|
|
176
|
-
offset: 0
|
|
177
|
-
}));
|
|
178
|
-
const where = {
|
|
179
|
-
[primaryKeyField]: {
|
|
180
|
-
[_sequelize().Op.in]: ids.map(id => id['pk'])
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
rows = yield (0, _utils.handleAppendsQuery)({
|
|
184
|
-
queryPromises: opts.include.map(include => {
|
|
185
|
-
const options = _objectSpread(_objectSpread({}, (0, _lodash().omit)(opts, ['limit', 'offset', 'filter'])), {}, {
|
|
186
|
-
include: include,
|
|
187
|
-
where,
|
|
188
|
-
transaction
|
|
189
|
-
});
|
|
190
|
-
return model.findAll(options).then(rows => {
|
|
191
|
-
return {
|
|
192
|
-
rows,
|
|
193
|
-
include
|
|
194
|
-
};
|
|
195
|
-
});
|
|
196
|
-
}),
|
|
197
|
-
templateModel: templateModel
|
|
205
|
+
// find all rows
|
|
206
|
+
const eagerLoadingTree = _eagerLoadingTree.EagerLoadingTree.buildFromSequelizeOptions({
|
|
207
|
+
model,
|
|
208
|
+
rootAttributes: opts.attributes,
|
|
209
|
+
includeOption: opts.include,
|
|
210
|
+
rootOrder: opts.order
|
|
198
211
|
});
|
|
212
|
+
yield eagerLoadingTree.load(ids.map(i => i.pk), transaction);
|
|
213
|
+
rows = eagerLoadingTree.root.instances;
|
|
199
214
|
} else {
|
|
200
215
|
rows = yield model.findAll(_objectSpread(_objectSpread({}, opts), {}, {
|
|
201
216
|
transaction
|
|
202
217
|
}));
|
|
203
218
|
}
|
|
204
|
-
yield
|
|
219
|
+
yield _this3.collection.db.emitAsync('afterRepositoryFind', {
|
|
205
220
|
findOptions: options,
|
|
206
|
-
dataCollection:
|
|
221
|
+
dataCollection: _this3.collection,
|
|
207
222
|
data: rows
|
|
208
223
|
});
|
|
209
224
|
return rows;
|
|
@@ -214,13 +229,13 @@ class Repository {
|
|
|
214
229
|
* @param options
|
|
215
230
|
*/
|
|
216
231
|
findAndCount(options) {
|
|
217
|
-
var
|
|
232
|
+
var _this4 = this;
|
|
218
233
|
return _asyncToGenerator(function* () {
|
|
219
234
|
options = _objectSpread(_objectSpread({}, options), {}, {
|
|
220
|
-
transaction: yield
|
|
235
|
+
transaction: yield _this4.getTransaction(options)
|
|
221
236
|
});
|
|
222
|
-
const count = yield
|
|
223
|
-
const results = count ? yield
|
|
237
|
+
const count = yield _this4.count(options);
|
|
238
|
+
const results = count ? yield _this4.find(options) : [];
|
|
224
239
|
return [results, count];
|
|
225
240
|
})();
|
|
226
241
|
}
|
|
@@ -237,10 +252,10 @@ class Repository {
|
|
|
237
252
|
* @param options
|
|
238
253
|
*/
|
|
239
254
|
findOne(options) {
|
|
240
|
-
var
|
|
255
|
+
var _this5 = this;
|
|
241
256
|
return _asyncToGenerator(function* () {
|
|
242
|
-
const transaction = yield
|
|
243
|
-
const rows = yield
|
|
257
|
+
const transaction = yield _this5.getTransaction(options);
|
|
258
|
+
const rows = yield _this5.find(_objectSpread(_objectSpread({}, options), {}, {
|
|
244
259
|
limit: 1,
|
|
245
260
|
transaction
|
|
246
261
|
}));
|
|
@@ -254,20 +269,20 @@ class Repository {
|
|
|
254
269
|
* @param options
|
|
255
270
|
*/
|
|
256
271
|
create(options) {
|
|
257
|
-
var
|
|
272
|
+
var _this6 = this;
|
|
258
273
|
return _asyncToGenerator(function* () {
|
|
259
274
|
if (Array.isArray(options.values)) {
|
|
260
|
-
return
|
|
275
|
+
return _this6.createMany(_objectSpread(_objectSpread({}, options), {}, {
|
|
261
276
|
records: options.values
|
|
262
277
|
}));
|
|
263
278
|
}
|
|
264
|
-
const transaction = yield
|
|
265
|
-
const guard = _updateGuard.UpdateGuard.fromOptions(
|
|
279
|
+
const transaction = yield _this6.getTransaction(options);
|
|
280
|
+
const guard = _updateGuard.UpdateGuard.fromOptions(_this6.model, _objectSpread(_objectSpread({}, options), {}, {
|
|
266
281
|
action: 'create',
|
|
267
|
-
underscored:
|
|
282
|
+
underscored: _this6.collection.options.underscored
|
|
268
283
|
}));
|
|
269
284
|
const values = guard.sanitize(options.values || {});
|
|
270
|
-
const instance = yield
|
|
285
|
+
const instance = yield _this6.model.create(values, _objectSpread(_objectSpread({}, options), {}, {
|
|
271
286
|
transaction
|
|
272
287
|
}));
|
|
273
288
|
if (!instance) {
|
|
@@ -277,10 +292,10 @@ class Repository {
|
|
|
277
292
|
transaction
|
|
278
293
|
}));
|
|
279
294
|
if (options.hooks !== false) {
|
|
280
|
-
yield
|
|
295
|
+
yield _this6.database.emitAsync(`${_this6.collection.name}.afterCreateWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
|
|
281
296
|
transaction
|
|
282
297
|
}));
|
|
283
|
-
yield
|
|
298
|
+
yield _this6.database.emitAsync(`${_this6.collection.name}.afterSaveWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
|
|
284
299
|
transaction
|
|
285
300
|
}));
|
|
286
301
|
instance.clearChangedWithAssociations();
|
|
@@ -295,9 +310,9 @@ class Repository {
|
|
|
295
310
|
* @param options
|
|
296
311
|
*/
|
|
297
312
|
createMany(options) {
|
|
298
|
-
var
|
|
313
|
+
var _this7 = this;
|
|
299
314
|
return _asyncToGenerator(function* () {
|
|
300
|
-
const transaction = yield
|
|
315
|
+
const transaction = yield _this7.getTransaction(options);
|
|
301
316
|
const records = options.records;
|
|
302
317
|
const instances = [];
|
|
303
318
|
var _iterator = _createForOfIteratorHelper(records),
|
|
@@ -305,7 +320,7 @@ class Repository {
|
|
|
305
320
|
try {
|
|
306
321
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
307
322
|
const values = _step.value;
|
|
308
|
-
const instance = yield
|
|
323
|
+
const instance = yield _this7.create({
|
|
309
324
|
values,
|
|
310
325
|
transaction
|
|
311
326
|
});
|
|
@@ -326,20 +341,20 @@ class Repository {
|
|
|
326
341
|
* @param options
|
|
327
342
|
*/
|
|
328
343
|
update(options) {
|
|
329
|
-
var
|
|
344
|
+
var _this8 = this;
|
|
330
345
|
return _asyncToGenerator(function* () {
|
|
331
346
|
if (Array.isArray(options.values)) {
|
|
332
|
-
return
|
|
347
|
+
return _this8.updateMany(_objectSpread(_objectSpread({}, options), {}, {
|
|
333
348
|
records: options.values
|
|
334
349
|
}));
|
|
335
350
|
}
|
|
336
|
-
const transaction = yield
|
|
337
|
-
const guard = _updateGuard.UpdateGuard.fromOptions(
|
|
338
|
-
underscored:
|
|
351
|
+
const transaction = yield _this8.getTransaction(options);
|
|
352
|
+
const guard = _updateGuard.UpdateGuard.fromOptions(_this8.model, _objectSpread(_objectSpread({}, options), {}, {
|
|
353
|
+
underscored: _this8.collection.options.underscored
|
|
339
354
|
}));
|
|
340
355
|
const values = guard.sanitize(options.values);
|
|
341
|
-
const queryOptions =
|
|
342
|
-
const instances = yield
|
|
356
|
+
const queryOptions = _this8.buildQueryOptions(options);
|
|
357
|
+
const instances = yield _this8.find(_objectSpread(_objectSpread({}, queryOptions), {}, {
|
|
343
358
|
transaction
|
|
344
359
|
}));
|
|
345
360
|
var _iterator2 = _createForOfIteratorHelper(instances),
|
|
@@ -363,10 +378,10 @@ class Repository {
|
|
|
363
378
|
try {
|
|
364
379
|
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
365
380
|
const instance = _step3.value;
|
|
366
|
-
yield
|
|
381
|
+
yield _this8.database.emitAsync(`${_this8.collection.name}.afterUpdateWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
|
|
367
382
|
transaction
|
|
368
383
|
}));
|
|
369
|
-
yield
|
|
384
|
+
yield _this8.database.emitAsync(`${_this8.collection.name}.afterSaveWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
|
|
370
385
|
transaction
|
|
371
386
|
}));
|
|
372
387
|
instance.clearChangedWithAssociations();
|
|
@@ -381,9 +396,9 @@ class Repository {
|
|
|
381
396
|
})();
|
|
382
397
|
}
|
|
383
398
|
updateMany(options) {
|
|
384
|
-
var
|
|
399
|
+
var _this9 = this;
|
|
385
400
|
return _asyncToGenerator(function* () {
|
|
386
|
-
const transaction = yield
|
|
401
|
+
const transaction = yield _this9.getTransaction(options);
|
|
387
402
|
const records = options.records;
|
|
388
403
|
const instances = [];
|
|
389
404
|
var _iterator4 = _createForOfIteratorHelper(records),
|
|
@@ -391,11 +406,11 @@ class Repository {
|
|
|
391
406
|
try {
|
|
392
407
|
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
393
408
|
const values = _step4.value;
|
|
394
|
-
const filterByTk = values[
|
|
409
|
+
const filterByTk = values[_this9.model.primaryKeyAttribute];
|
|
395
410
|
if (!filterByTk) {
|
|
396
411
|
throw new Error('filterByTk invalid');
|
|
397
412
|
}
|
|
398
|
-
const instance = yield
|
|
413
|
+
const instance = yield _this9.update({
|
|
399
414
|
values,
|
|
400
415
|
filterByTk,
|
|
401
416
|
transaction
|
|
@@ -411,24 +426,24 @@ class Repository {
|
|
|
411
426
|
})();
|
|
412
427
|
}
|
|
413
428
|
destroy(options) {
|
|
414
|
-
var
|
|
429
|
+
var _this10 = this;
|
|
415
430
|
return _asyncToGenerator(function* () {
|
|
416
|
-
const transaction = yield
|
|
417
|
-
const modelFilterKey =
|
|
431
|
+
const transaction = yield _this10.getTransaction(options);
|
|
432
|
+
const modelFilterKey = _this10.collection.filterTargetKey;
|
|
418
433
|
options = options;
|
|
419
434
|
if (options['individualHooks'] === undefined) {
|
|
420
435
|
options['individualHooks'] = true;
|
|
421
436
|
}
|
|
422
437
|
const filterByTk = options.filterByTk && !_lodash().default.isArray(options.filterByTk) ? [options.filterByTk] : options.filterByTk;
|
|
423
|
-
if (
|
|
424
|
-
if (
|
|
438
|
+
if (_this10.collection.model.primaryKeyAttributes.length !== 1 && filterByTk && !_lodash().default.get(_this10.collection.options, 'filterTargetKey')) {
|
|
439
|
+
if (_this10.collection.model.primaryKeyAttributes.length > 1) {
|
|
425
440
|
throw new Error(`filterByTk is not supported for composite primary key`);
|
|
426
441
|
} else {
|
|
427
442
|
throw new Error(`filterByTk is not supported for collection that has no primary key`);
|
|
428
443
|
}
|
|
429
444
|
}
|
|
430
445
|
if (filterByTk && !options.filter) {
|
|
431
|
-
return yield
|
|
446
|
+
return yield _this10.model.destroy(_objectSpread(_objectSpread({}, options), {}, {
|
|
432
447
|
where: {
|
|
433
448
|
[modelFilterKey]: {
|
|
434
449
|
[_sequelize().Op.in]: filterByTk
|
|
@@ -438,26 +453,26 @@ class Repository {
|
|
|
438
453
|
}));
|
|
439
454
|
}
|
|
440
455
|
if (options.filter) {
|
|
441
|
-
if (
|
|
442
|
-
const queryOptions = _objectSpread({},
|
|
443
|
-
return yield
|
|
456
|
+
if (_this10.collection.model.primaryKeyAttributes.length !== 1 && !_lodash().default.get(_this10.collection.options, 'filterTargetKey')) {
|
|
457
|
+
const queryOptions = _objectSpread({}, _this10.buildQueryOptions(options));
|
|
458
|
+
return yield _this10.model.destroy(_objectSpread(_objectSpread({}, queryOptions), {}, {
|
|
444
459
|
transaction
|
|
445
460
|
}));
|
|
446
461
|
}
|
|
447
|
-
let pks = (yield
|
|
462
|
+
let pks = (yield _this10.find({
|
|
448
463
|
filter: options.filter,
|
|
449
464
|
transaction
|
|
450
465
|
})).map(instance => instance.get(modelFilterKey));
|
|
451
466
|
if (filterByTk) {
|
|
452
467
|
pks = _lodash().default.intersection(pks.map(i => `${i}`), filterByTk.map(i => `${i}`));
|
|
453
468
|
}
|
|
454
|
-
return yield
|
|
469
|
+
return yield _this10.destroy(_objectSpread(_objectSpread({}, _lodash().default.omit(options, 'filter')), {}, {
|
|
455
470
|
filterByTk: pks,
|
|
456
471
|
transaction
|
|
457
472
|
}));
|
|
458
473
|
}
|
|
459
474
|
if (options.truncate) {
|
|
460
|
-
return yield
|
|
475
|
+
return yield _this10.model.destroy(_objectSpread(_objectSpread({}, options), {}, {
|
|
461
476
|
truncate: true,
|
|
462
477
|
transaction
|
|
463
478
|
}));
|
|
@@ -490,13 +505,13 @@ class Repository {
|
|
|
490
505
|
return parser.toSequelizeParams();
|
|
491
506
|
}
|
|
492
507
|
getTransaction(options, autoGen = false) {
|
|
493
|
-
var
|
|
508
|
+
var _this11 = this;
|
|
494
509
|
return _asyncToGenerator(function* () {
|
|
495
510
|
if (_lodash().default.isPlainObject(options) && options.transaction) {
|
|
496
511
|
return options.transaction;
|
|
497
512
|
}
|
|
498
513
|
if (autoGen) {
|
|
499
|
-
return yield
|
|
514
|
+
return yield _this11.model.sequelize.transaction();
|
|
500
515
|
}
|
|
501
516
|
return null;
|
|
502
517
|
})();
|