@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.
- package/lib/collection.d.ts +7 -3
- package/lib/collection.js +126 -22
- package/lib/database.d.ts +13 -7
- package/lib/database.js +39 -8
- package/lib/decorators/must-have-filter-decorator.js +4 -0
- package/lib/decorators/transaction-decorator.js +1 -0
- package/lib/features/ReferencesMap.js +14 -9
- package/lib/field-repository/array-field-repository.d.ts +28 -0
- package/lib/field-repository/array-field-repository.js +208 -0
- package/lib/fields/array-field.d.ts +1 -1
- package/lib/fields/array-field.js +15 -11
- package/lib/fields/belongs-to-field.d.ts +9 -1
- package/lib/fields/belongs-to-field.js +21 -7
- package/lib/fields/belongs-to-many-field.d.ts +4 -0
- package/lib/fields/belongs-to-many-field.js +24 -0
- package/lib/fields/field.d.ts +3 -2
- package/lib/fields/field.js +19 -13
- package/lib/fields/has-many-field.d.ts +2 -1
- package/lib/fields/has-many-field.js +18 -10
- package/lib/fields/has-one-field.d.ts +1 -0
- package/lib/fields/has-one-field.js +22 -10
- package/lib/fields/index.d.ts +3 -3
- package/lib/fields/index.js +14 -34
- package/lib/fields/relation-field.d.ts +2 -1
- package/lib/fields/relation-field.js +16 -0
- package/lib/fields/set-field.d.ts +10 -0
- package/lib/fields/set-field.js +35 -0
- package/lib/fields/sort-field.d.ts +1 -1
- package/lib/fields/sort-field.js +1 -1
- package/lib/filter-match.d.ts +1 -0
- package/lib/filter-match.js +84 -0
- package/lib/filter-parser.d.ts +2 -2
- package/lib/index.d.ts +5 -1
- package/lib/index.js +56 -0
- package/lib/inherited-collection.d.ts +13 -0
- package/lib/inherited-collection.js +210 -0
- package/lib/inherited-map.d.ts +21 -0
- package/lib/inherited-map.js +203 -0
- package/lib/model-hook.d.ts +1 -1
- package/lib/model.d.ts +1 -0
- package/lib/model.js +47 -0
- package/lib/operators/array.d.ts +1 -25
- package/lib/operators/association.d.ts +1 -9
- package/lib/operators/boolean.d.ts +1 -12
- package/lib/operators/date.d.ts +1 -33
- package/lib/operators/empty.d.ts +2 -25
- package/lib/operators/ne.d.ts +1 -13
- package/lib/operators/notIn.d.ts +1 -9
- package/lib/options-parser.d.ts +3 -2
- package/lib/options-parser.js +16 -1
- package/lib/relation-repository/relation-repository.d.ts +2 -2
- package/lib/relation-repository/single-relation-repository.js +2 -2
- package/lib/repository.d.ts +18 -10
- package/lib/repository.js +172 -38
- package/lib/sync-runner.d.ts +4 -0
- package/lib/sync-runner.js +181 -0
- package/lib/types.d.ts +2 -2
- package/lib/update-associations.d.ts +1 -0
- package/lib/update-associations.js +21 -2
- package/lib/update-guard.d.ts +3 -3
- package/lib/utils.js +5 -0
- package/package.json +4 -4
- package/src/__tests__/bigint.test.ts +48 -0
- package/src/__tests__/collection.test.ts +48 -13
- package/src/__tests__/database.test.ts +10 -0
- package/src/__tests__/field-repository/array-field-repository.test.ts +94 -0
- package/src/__tests__/fields/set.test.ts +37 -0
- package/src/__tests__/filter-match.test.ts +52 -0
- package/src/__tests__/inhertits/collection-inherits-sync.test.ts +38 -0
- package/src/__tests__/inhertits/collection-inherits.test.ts +994 -0
- package/src/__tests__/inhertits/helper.ts +3 -0
- package/src/__tests__/inhertits/inherited-map.test.ts +27 -0
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +2 -0
- package/src/__tests__/relation-repository/has-many-repository.test.ts +1 -1
- package/src/__tests__/repository/destroy.test.ts +122 -1
- package/src/__tests__/repository/update-many.test.ts +57 -0
- package/src/__tests__/update-association-values.test.ts +232 -0
- package/src/__tests__/update-associations.test.ts +6 -1
- package/src/collection.ts +90 -8
- package/src/database.ts +53 -14
- package/src/decorators/must-have-filter-decorator.ts +4 -0
- package/src/decorators/transaction-decorator.ts +1 -0
- package/src/features/ReferencesMap.ts +20 -9
- package/src/features/referential-integrity-check.ts +1 -0
- package/src/field-repository/array-field-repository.ts +155 -0
- package/src/fields/array-field.ts +4 -4
- package/src/fields/belongs-to-field.ts +26 -10
- package/src/fields/belongs-to-many-field.ts +34 -0
- package/src/fields/field.ts +26 -13
- package/src/fields/has-many-field.ts +17 -9
- package/src/fields/has-one-field.ts +23 -9
- package/src/fields/index.ts +5 -5
- package/src/fields/relation-field.ts +16 -0
- package/src/fields/set-field.ts +25 -0
- package/src/fields/sort-field.ts +5 -4
- package/src/filter-match.ts +49 -0
- package/src/filter-parser.ts +2 -2
- package/src/index.ts +5 -2
- package/src/inherited-collection.ts +112 -0
- package/src/inherited-map.ts +97 -0
- package/src/model-hook.ts +2 -3
- package/src/model.ts +43 -3
- package/src/operators/array.ts +1 -1
- package/src/operators/association.ts +1 -1
- package/src/operators/boolean.ts +1 -1
- package/src/operators/date.ts +1 -1
- package/src/operators/empty.ts +1 -1
- package/src/operators/ne.ts +1 -1
- package/src/operators/notIn.ts +2 -1
- package/src/options-parser.ts +20 -4
- package/src/relation-repository/relation-repository.ts +2 -2
- package/src/relation-repository/single-relation-repository.ts +2 -2
- package/src/repository.ts +144 -30
- package/src/sync-runner.ts +162 -0
- package/src/types.ts +2 -2
- package/src/update-associations.ts +23 -7
- package/src/update-guard.ts +3 -3
- package/src/utils.ts +5 -1
- package/lib/fields/sequence-field.d.ts +0 -31
- package/lib/fields/sequence-field.js +0 -299
- package/src/__tests__/fields/sequence-field.test.ts +0 -455
- package/src/fields/sequence-field.ts +0 -200
package/lib/options-parser.js
CHANGED
|
@@ -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))
|
|
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,
|
|
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:
|
|
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; }
|
package/lib/repository.d.ts
CHANGED
|
@@ -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,
|
|
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
|
|
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:
|
|
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<
|
|
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<
|
|
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<
|
|
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
|
-
|
|
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
|
-
|
|
139
|
-
|
|
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
|
-
|
|
234
|
+
rows = yield (0, _utils.handleAppendsQuery)({
|
|
187
235
|
queryPromises: opts.include.map(include => {
|
|
188
|
-
|
|
236
|
+
const options = _objectSpread(_objectSpread({}, (0, _lodash().omit)(opts, ['limit', 'offset'])), {}, {
|
|
189
237
|
include: include,
|
|
190
238
|
where,
|
|
191
239
|
transaction
|
|
192
|
-
})
|
|
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:
|
|
249
|
+
templateModel: templateModel
|
|
200
250
|
});
|
|
251
|
+
} else {
|
|
252
|
+
rows = yield model.findAll(_objectSpread(_objectSpread({}, opts), {}, {
|
|
253
|
+
transaction
|
|
254
|
+
}));
|
|
201
255
|
}
|
|
202
256
|
|
|
203
|
-
|
|
204
|
-
|
|
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
|
|
322
|
-
|
|
394
|
+
var _iterator2 = _createForOfIteratorHelper(records),
|
|
395
|
+
_step2;
|
|
323
396
|
|
|
324
397
|
try {
|
|
325
|
-
for (
|
|
326
|
-
const values =
|
|
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
|
-
|
|
407
|
+
_iterator2.e(err);
|
|
335
408
|
} finally {
|
|
336
|
-
|
|
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
|
|
367
|
-
|
|
445
|
+
var _iterator3 = _createForOfIteratorHelper(instances),
|
|
446
|
+
_step3;
|
|
368
447
|
|
|
369
448
|
try {
|
|
370
|
-
for (
|
|
371
|
-
const instance =
|
|
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
|
-
|
|
457
|
+
_iterator3.e(err);
|
|
379
458
|
} finally {
|
|
380
|
-
|
|
459
|
+
_iterator3.f();
|
|
381
460
|
}
|
|
382
461
|
|
|
383
462
|
if (options.hooks !== false) {
|
|
384
|
-
var
|
|
385
|
-
|
|
463
|
+
var _iterator4 = _createForOfIteratorHelper(instances),
|
|
464
|
+
_step4;
|
|
386
465
|
|
|
387
466
|
try {
|
|
388
|
-
for (
|
|
389
|
-
const instance =
|
|
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
|
-
|
|
478
|
+
_iterator4.e(err);
|
|
400
479
|
} finally {
|
|
401
|
-
|
|
480
|
+
_iterator4.f();
|
|
402
481
|
}
|
|
403
482
|
}
|
|
404
483
|
|
|
@@ -406,12 +485,49 @@ class Repository {
|
|
|
406
485
|
})();
|
|
407
486
|
}
|
|
408
487
|
|
|
409
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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],
|