@nocobase/database 0.9.3-alpha.1 → 0.9.4-alpha.2
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 +42 -16
- 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__/filter.test.ts +54 -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 +33 -11
- 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/collection.d.ts
CHANGED
|
@@ -56,27 +56,19 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
56
56
|
fields: Map<string, any>;
|
|
57
57
|
model: ModelStatic<Model>;
|
|
58
58
|
repository: Repository<TModelAttributes, TCreationAttributes>;
|
|
59
|
+
constructor(options: CollectionOptions, context: CollectionContext);
|
|
59
60
|
get filterTargetKey(): string;
|
|
60
61
|
get name(): string;
|
|
61
62
|
get titleField(): string;
|
|
62
63
|
get db(): Database;
|
|
63
64
|
get treeParentField(): BelongsToField | null;
|
|
64
65
|
get treeChildrenField(): HasManyField | null;
|
|
65
|
-
constructor(options: CollectionOptions, context: CollectionContext);
|
|
66
|
-
private checkOptions;
|
|
67
|
-
private checkTableName;
|
|
68
66
|
tableName(): any;
|
|
69
|
-
protected sequelizeModelOptions(): {
|
|
70
|
-
modelName: string;
|
|
71
|
-
sequelize: import("sequelize").Sequelize;
|
|
72
|
-
tableName: any;
|
|
73
|
-
};
|
|
74
67
|
/**
|
|
75
68
|
* TODO
|
|
76
69
|
*/
|
|
77
70
|
modelInit(): void;
|
|
78
71
|
setRepository(repository?: RepositoryType | string): void;
|
|
79
|
-
private bindFieldEventListener;
|
|
80
72
|
forEachField(callback: (field: Field) => void): void;
|
|
81
73
|
findField(callback: (field: Field) => boolean): any;
|
|
82
74
|
hasField(name: string): boolean;
|
|
@@ -120,5 +112,13 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
120
112
|
quotedTableName(): any;
|
|
121
113
|
collectionSchema(): string;
|
|
122
114
|
isView(): boolean;
|
|
115
|
+
protected sequelizeModelOptions(): {
|
|
116
|
+
modelName: string;
|
|
117
|
+
sequelize: import("sequelize").Sequelize;
|
|
118
|
+
tableName: any;
|
|
119
|
+
};
|
|
120
|
+
private checkOptions;
|
|
121
|
+
private checkTableName;
|
|
122
|
+
private bindFieldEventListener;
|
|
123
123
|
}
|
|
124
124
|
export {};
|
package/lib/collection.js
CHANGED
|
@@ -56,6 +56,30 @@ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol
|
|
|
56
56
|
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); }
|
|
57
57
|
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; }
|
|
58
58
|
class Collection extends _events().EventEmitter {
|
|
59
|
+
constructor(options, context) {
|
|
60
|
+
super();
|
|
61
|
+
this.options = void 0;
|
|
62
|
+
this.context = void 0;
|
|
63
|
+
this.isThrough = void 0;
|
|
64
|
+
this.fields = new Map();
|
|
65
|
+
this.model = void 0;
|
|
66
|
+
this.repository = void 0;
|
|
67
|
+
this.context = context;
|
|
68
|
+
this.options = options;
|
|
69
|
+
this.checkOptions(options);
|
|
70
|
+
this.bindFieldEventListener();
|
|
71
|
+
this.modelInit();
|
|
72
|
+
this.db.modelCollection.set(this.model, this);
|
|
73
|
+
// set tableName to collection map
|
|
74
|
+
// the form of key is `${schema}.${tableName}` if schema exists
|
|
75
|
+
// otherwise is `${tableName}`
|
|
76
|
+
this.db.tableNameCollectionMap.set(this.getTableNameWithSchemaAsString(), this);
|
|
77
|
+
if (!options.inherits) {
|
|
78
|
+
this.setFields(options.fields);
|
|
79
|
+
}
|
|
80
|
+
this.setRepository(options.repository);
|
|
81
|
+
this.setSortable(options.sortable);
|
|
82
|
+
}
|
|
59
83
|
get filterTargetKey() {
|
|
60
84
|
const targetKey = _lodash().default.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
|
|
61
85
|
if (!targetKey && this.model.rawAttributes['id']) {
|
|
@@ -108,53 +132,6 @@ class Collection extends _events().EventEmitter {
|
|
|
108
132
|
_iterator2.f();
|
|
109
133
|
}
|
|
110
134
|
}
|
|
111
|
-
constructor(options, context) {
|
|
112
|
-
super();
|
|
113
|
-
this.options = void 0;
|
|
114
|
-
this.context = void 0;
|
|
115
|
-
this.isThrough = void 0;
|
|
116
|
-
this.fields = new Map();
|
|
117
|
-
this.model = void 0;
|
|
118
|
-
this.repository = void 0;
|
|
119
|
-
this.context = context;
|
|
120
|
-
this.options = options;
|
|
121
|
-
this.checkOptions(options);
|
|
122
|
-
this.bindFieldEventListener();
|
|
123
|
-
this.modelInit();
|
|
124
|
-
this.db.modelCollection.set(this.model, this);
|
|
125
|
-
// set tableName to collection map
|
|
126
|
-
// the form of key is `${schema}.${tableName}` if schema exists
|
|
127
|
-
// otherwise is `${tableName}`
|
|
128
|
-
this.db.tableNameCollectionMap.set(this.getTableNameWithSchemaAsString(), this);
|
|
129
|
-
if (!options.inherits) {
|
|
130
|
-
this.setFields(options.fields);
|
|
131
|
-
}
|
|
132
|
-
this.setRepository(options.repository);
|
|
133
|
-
this.setSortable(options.sortable);
|
|
134
|
-
}
|
|
135
|
-
checkOptions(options) {
|
|
136
|
-
(0, _utils.checkIdentifier)(options.name);
|
|
137
|
-
this.checkTableName();
|
|
138
|
-
}
|
|
139
|
-
checkTableName() {
|
|
140
|
-
const tableName = this.tableName();
|
|
141
|
-
var _iterator3 = _createForOfIteratorHelper(this.db.collections),
|
|
142
|
-
_step3;
|
|
143
|
-
try {
|
|
144
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
145
|
-
const _step3$value = _slicedToArray(_step3.value, 2),
|
|
146
|
-
k = _step3$value[0],
|
|
147
|
-
collection = _step3$value[1];
|
|
148
|
-
if (collection.name != this.options.name && tableName === collection.tableName() && collection.collectionSchema() === this.collectionSchema()) {
|
|
149
|
-
throw new Error(`collection ${collection.name} and ${this.name} have same tableName "${tableName}"`);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
} catch (err) {
|
|
153
|
-
_iterator3.e(err);
|
|
154
|
-
} finally {
|
|
155
|
-
_iterator3.f();
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
135
|
tableName() {
|
|
159
136
|
const _this$options = this.options,
|
|
160
137
|
name = _this$options.name,
|
|
@@ -162,14 +139,6 @@ class Collection extends _events().EventEmitter {
|
|
|
162
139
|
const tName = tableName || name;
|
|
163
140
|
return this.options.underscored ? (0, _utils.snakeCase)(tName) : tName;
|
|
164
141
|
}
|
|
165
|
-
sequelizeModelOptions() {
|
|
166
|
-
const name = this.options.name;
|
|
167
|
-
return _objectSpread(_objectSpread({}, _lodash().default.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), {}, {
|
|
168
|
-
modelName: name,
|
|
169
|
-
sequelize: this.context.database.sequelize,
|
|
170
|
-
tableName: this.tableName()
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
142
|
/**
|
|
174
143
|
* TODO
|
|
175
144
|
*/
|
|
@@ -221,15 +190,6 @@ class Collection extends _events().EventEmitter {
|
|
|
221
190
|
}
|
|
222
191
|
this.repository = new repo(this);
|
|
223
192
|
}
|
|
224
|
-
bindFieldEventListener() {
|
|
225
|
-
this.on('field.afterAdd', field => {
|
|
226
|
-
field.bind();
|
|
227
|
-
});
|
|
228
|
-
this.on('field.afterRemove', field => {
|
|
229
|
-
field.unbind();
|
|
230
|
-
this.db.emit('field.afterRemove', field);
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
193
|
forEachField(callback) {
|
|
234
194
|
return [...this.fields.values()].forEach(callback);
|
|
235
195
|
}
|
|
@@ -277,10 +237,14 @@ class Collection extends _events().EventEmitter {
|
|
|
277
237
|
sourceFieldName = _options$source$split2[1];
|
|
278
238
|
const sourceCollection = this.db.collections.get(sourceCollectionName);
|
|
279
239
|
if (!sourceCollection) {
|
|
280
|
-
|
|
240
|
+
this.db.logger.warn(`source collection "${sourceCollectionName}" not found for field "${name}" at collection "${this.name}"`);
|
|
281
241
|
}
|
|
282
242
|
const sourceField = sourceCollection.fields.get(sourceFieldName);
|
|
283
|
-
|
|
243
|
+
if (!sourceField) {
|
|
244
|
+
this.db.logger.warn(`source field "${sourceFieldName}" not found for field "${name}" at collection "${this.name}"`);
|
|
245
|
+
} else {
|
|
246
|
+
options = _objectSpread(_objectSpread({}, sourceField.options), options);
|
|
247
|
+
}
|
|
284
248
|
}
|
|
285
249
|
this.emit('field.beforeAdd', name, options, {
|
|
286
250
|
collection: this
|
|
@@ -302,13 +266,13 @@ class Collection extends _events().EventEmitter {
|
|
|
302
266
|
this.emit('field.afterAdd', field);
|
|
303
267
|
// refresh children models
|
|
304
268
|
if (this.isParent()) {
|
|
305
|
-
var
|
|
269
|
+
var _iterator3 = _createForOfIteratorHelper(this.context.database.inheritanceMap.getChildren(this.name, {
|
|
306
270
|
deep: false
|
|
307
271
|
})),
|
|
308
|
-
|
|
272
|
+
_step3;
|
|
309
273
|
try {
|
|
310
|
-
for (
|
|
311
|
-
const child =
|
|
274
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
275
|
+
const child = _step3.value;
|
|
312
276
|
const childCollection = this.db.getCollection(child);
|
|
313
277
|
const existField = childCollection.getField(name);
|
|
314
278
|
if (!existField || existField.options.inherit) {
|
|
@@ -318,9 +282,9 @@ class Collection extends _events().EventEmitter {
|
|
|
318
282
|
}
|
|
319
283
|
}
|
|
320
284
|
} catch (err) {
|
|
321
|
-
|
|
285
|
+
_iterator3.e(err);
|
|
322
286
|
} finally {
|
|
323
|
-
|
|
287
|
+
_iterator3.f();
|
|
324
288
|
}
|
|
325
289
|
}
|
|
326
290
|
return field;
|
|
@@ -332,34 +296,34 @@ class Collection extends _events().EventEmitter {
|
|
|
332
296
|
if (resetFields) {
|
|
333
297
|
this.resetFields();
|
|
334
298
|
}
|
|
335
|
-
var
|
|
336
|
-
|
|
299
|
+
var _iterator4 = _createForOfIteratorHelper(fields),
|
|
300
|
+
_step4;
|
|
337
301
|
try {
|
|
338
|
-
for (
|
|
339
|
-
const _ref =
|
|
302
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
303
|
+
const _ref = _step4.value;
|
|
340
304
|
const name = _ref.name,
|
|
341
305
|
options = _objectWithoutProperties(_ref, _excluded);
|
|
342
306
|
this.addField(name, options);
|
|
343
307
|
}
|
|
344
308
|
} catch (err) {
|
|
345
|
-
|
|
309
|
+
_iterator4.e(err);
|
|
346
310
|
} finally {
|
|
347
|
-
|
|
311
|
+
_iterator4.f();
|
|
348
312
|
}
|
|
349
313
|
}
|
|
350
314
|
resetFields() {
|
|
351
315
|
const fieldNames = this.fields.keys();
|
|
352
|
-
var
|
|
353
|
-
|
|
316
|
+
var _iterator5 = _createForOfIteratorHelper(fieldNames),
|
|
317
|
+
_step5;
|
|
354
318
|
try {
|
|
355
|
-
for (
|
|
356
|
-
const fieldName =
|
|
319
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
320
|
+
const fieldName = _step5.value;
|
|
357
321
|
this.removeField(fieldName);
|
|
358
322
|
}
|
|
359
323
|
} catch (err) {
|
|
360
|
-
|
|
324
|
+
_iterator5.e(err);
|
|
361
325
|
} finally {
|
|
362
|
-
|
|
326
|
+
_iterator5.f();
|
|
363
327
|
}
|
|
364
328
|
}
|
|
365
329
|
remove() {
|
|
@@ -391,13 +355,13 @@ class Collection extends _events().EventEmitter {
|
|
|
391
355
|
const bool = this.fields.delete(name);
|
|
392
356
|
if (bool) {
|
|
393
357
|
if (this.isParent()) {
|
|
394
|
-
var
|
|
358
|
+
var _iterator6 = _createForOfIteratorHelper(this.db.inheritanceMap.getChildren(this.name, {
|
|
395
359
|
deep: false
|
|
396
360
|
})),
|
|
397
|
-
|
|
361
|
+
_step6;
|
|
398
362
|
try {
|
|
399
|
-
for (
|
|
400
|
-
const child =
|
|
363
|
+
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
364
|
+
const child = _step6.value;
|
|
401
365
|
const childCollection = this.db.getCollection(child);
|
|
402
366
|
const existField = childCollection.getField(name);
|
|
403
367
|
if (existField && existField.options.inherit) {
|
|
@@ -405,9 +369,9 @@ class Collection extends _events().EventEmitter {
|
|
|
405
369
|
}
|
|
406
370
|
}
|
|
407
371
|
} catch (err) {
|
|
408
|
-
|
|
372
|
+
_iterator6.e(err);
|
|
409
373
|
} finally {
|
|
410
|
-
|
|
374
|
+
_iterator6.f();
|
|
411
375
|
}
|
|
412
376
|
}
|
|
413
377
|
this.emit('field.afterRemove', field);
|
|
@@ -497,11 +461,11 @@ class Collection extends _events().EventEmitter {
|
|
|
497
461
|
if (name.startsWith(`${indexName.join(',')},`)) {
|
|
498
462
|
return;
|
|
499
463
|
}
|
|
500
|
-
var
|
|
501
|
-
|
|
464
|
+
var _iterator7 = _createForOfIteratorHelper(indexes),
|
|
465
|
+
_step7;
|
|
502
466
|
try {
|
|
503
|
-
for (
|
|
504
|
-
const item =
|
|
467
|
+
for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
|
|
468
|
+
const item = _step7.value;
|
|
505
469
|
if (_lodash().default.isEqual(item.fields, indexName)) {
|
|
506
470
|
return;
|
|
507
471
|
}
|
|
@@ -511,9 +475,9 @@ class Collection extends _events().EventEmitter {
|
|
|
511
475
|
}
|
|
512
476
|
}
|
|
513
477
|
} catch (err) {
|
|
514
|
-
|
|
478
|
+
_iterator7.e(err);
|
|
515
479
|
} finally {
|
|
516
|
-
|
|
480
|
+
_iterator7.f();
|
|
517
481
|
}
|
|
518
482
|
if (!indexItem) {
|
|
519
483
|
return;
|
|
@@ -631,5 +595,45 @@ class Collection extends _events().EventEmitter {
|
|
|
631
595
|
isView() {
|
|
632
596
|
return false;
|
|
633
597
|
}
|
|
598
|
+
sequelizeModelOptions() {
|
|
599
|
+
const name = this.options.name;
|
|
600
|
+
return _objectSpread(_objectSpread({}, _lodash().default.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), {}, {
|
|
601
|
+
modelName: name,
|
|
602
|
+
sequelize: this.context.database.sequelize,
|
|
603
|
+
tableName: this.tableName()
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
checkOptions(options) {
|
|
607
|
+
(0, _utils.checkIdentifier)(options.name);
|
|
608
|
+
this.checkTableName();
|
|
609
|
+
}
|
|
610
|
+
checkTableName() {
|
|
611
|
+
const tableName = this.tableName();
|
|
612
|
+
var _iterator8 = _createForOfIteratorHelper(this.db.collections),
|
|
613
|
+
_step8;
|
|
614
|
+
try {
|
|
615
|
+
for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
|
|
616
|
+
const _step8$value = _slicedToArray(_step8.value, 2),
|
|
617
|
+
k = _step8$value[0],
|
|
618
|
+
collection = _step8$value[1];
|
|
619
|
+
if (collection.name != this.options.name && tableName === collection.tableName() && collection.collectionSchema() === this.collectionSchema()) {
|
|
620
|
+
throw new Error(`collection ${collection.name} and ${this.name} have same tableName "${tableName}"`);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
} catch (err) {
|
|
624
|
+
_iterator8.e(err);
|
|
625
|
+
} finally {
|
|
626
|
+
_iterator8.f();
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
bindFieldEventListener() {
|
|
630
|
+
this.on('field.afterAdd', field => {
|
|
631
|
+
field.bind();
|
|
632
|
+
});
|
|
633
|
+
this.on('field.afterRemove', field => {
|
|
634
|
+
field.unbind();
|
|
635
|
+
this.db.emit('field.afterRemove', field);
|
|
636
|
+
});
|
|
637
|
+
}
|
|
634
638
|
}
|
|
635
639
|
exports.Collection = Collection;
|
package/lib/database.d.ts
CHANGED
|
@@ -17,11 +17,11 @@ import { ModelHook } from './model-hook';
|
|
|
17
17
|
import { RelationRepository } from './relation-repository/relation-repository';
|
|
18
18
|
import { Repository } from './repository';
|
|
19
19
|
import { AfterDefineCollectionListener, BeforeDefineCollectionListener, CreateListener, CreateWithAssociationsListener, DatabaseAfterDefineCollectionEventType, DatabaseAfterRemoveCollectionEventType, DatabaseBeforeDefineCollectionEventType, DatabaseBeforeRemoveCollectionEventType, DestroyListener, EventType, ModelCreateEventTypes, ModelCreateWithAssociationsEventTypes, ModelDestroyEventTypes, ModelSaveEventTypes, ModelSaveWithAssociationsEventTypes, ModelUpdateEventTypes, ModelUpdateWithAssociationsEventTypes, ModelValidateEventTypes, RemoveCollectionListener, SaveListener, SaveWithAssociationsListener, SyncListener, UpdateListener, UpdateWithAssociationsListener, ValidateListener } from './types';
|
|
20
|
-
import DatabaseUtils from './database-utils';
|
|
21
|
-
import { BaseValueParser } from './value-parsers';
|
|
22
|
-
import QueryInterface from './query-interface/query-interface';
|
|
23
20
|
import { Logger } from '@nocobase/logger';
|
|
24
21
|
import { CollectionGroupManager } from './collection-group-manager';
|
|
22
|
+
import DatabaseUtils from './database-utils';
|
|
23
|
+
import QueryInterface from './query-interface/query-interface';
|
|
24
|
+
import { BaseValueParser } from './value-parsers';
|
|
25
25
|
export declare type MergeOptions = merge.Options;
|
|
26
26
|
export interface PendingOptions {
|
|
27
27
|
field: RelationField;
|
|
@@ -99,6 +99,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
99
99
|
}[]>;
|
|
100
100
|
logger: Logger;
|
|
101
101
|
collectionGroupManager: CollectionGroupManager;
|
|
102
|
+
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
102
103
|
constructor(options: DatabaseOptions);
|
|
103
104
|
setLogger(logger: Logger): void;
|
|
104
105
|
sequelizeOptions(options: any): any;
|
|
@@ -165,7 +166,6 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
165
166
|
from?: string;
|
|
166
167
|
extensions?: ImportFileExtension[];
|
|
167
168
|
}): Promise<Map<string, Collection>>;
|
|
168
|
-
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
169
169
|
}
|
|
170
170
|
export declare function extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions): {
|
|
171
171
|
collectionOptions: CollectionOptions;
|
package/lib/database.js
CHANGED
|
@@ -72,11 +72,11 @@ var _migration = require("./migration");
|
|
|
72
72
|
var _modelHook = require("./model-hook");
|
|
73
73
|
var _operators = _interopRequireDefault(require("./operators"));
|
|
74
74
|
var _utils2 = require("./utils");
|
|
75
|
+
var _collectionGroupManager = require("./collection-group-manager");
|
|
75
76
|
var _databaseUtils = _interopRequireDefault(require("./database-utils"));
|
|
76
77
|
var _listeners = require("./listeners");
|
|
77
|
-
var _valueParsers = require("./value-parsers");
|
|
78
78
|
var _queryInterfaceBuilder = _interopRequireDefault(require("./query-interface/query-interface-builder"));
|
|
79
|
-
var
|
|
79
|
+
var _valueParsers = require("./value-parsers");
|
|
80
80
|
var _viewCollection = require("./view-collection");
|
|
81
81
|
const _excluded = ["drop"],
|
|
82
82
|
_excluded2 = ["retry"];
|
|
@@ -246,6 +246,11 @@ class Database extends _events().EventEmitter {
|
|
|
246
246
|
sequelize: this.sequelize
|
|
247
247
|
}))
|
|
248
248
|
});
|
|
249
|
+
this.sequelize.beforeDefine((model, opts) => {
|
|
250
|
+
if (this.options.tablePrefix) {
|
|
251
|
+
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
252
|
+
}
|
|
253
|
+
});
|
|
249
254
|
this.collection({
|
|
250
255
|
name: 'migrations',
|
|
251
256
|
autoGenId: false,
|
|
@@ -257,11 +262,6 @@ class Database extends _events().EventEmitter {
|
|
|
257
262
|
name: 'name'
|
|
258
263
|
}]
|
|
259
264
|
});
|
|
260
|
-
this.sequelize.beforeDefine((model, opts) => {
|
|
261
|
-
if (this.options.tablePrefix) {
|
|
262
|
-
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
265
|
this.initListener();
|
|
266
266
|
(0, _utils2.patchSequelizeQueryInterface)(this);
|
|
267
267
|
}
|
|
@@ -372,34 +372,6 @@ class Database extends _events().EventEmitter {
|
|
|
372
372
|
options.schema = this.options.schema;
|
|
373
373
|
}
|
|
374
374
|
});
|
|
375
|
-
this.on('afterRepositoryFind', ({
|
|
376
|
-
findOptions,
|
|
377
|
-
dataCollection,
|
|
378
|
-
data
|
|
379
|
-
}) => {
|
|
380
|
-
if (dataCollection.isParent()) {
|
|
381
|
-
var _iterator = _createForOfIteratorHelper(data),
|
|
382
|
-
_step;
|
|
383
|
-
try {
|
|
384
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
385
|
-
const row = _step.value;
|
|
386
|
-
const rowCollection = this.tableNameCollectionMap.get(findOptions.raw ? `${row['__schemaName']}.${row['__tableName']}` : `${row.get('__schemaName')}.${row.get('__tableName')}`);
|
|
387
|
-
if (!rowCollection) {
|
|
388
|
-
this.logger.warn(`Can not find collection by table name ${JSON.stringify(row)}, current collections: ${Array.from(this.tableNameCollectionMap.keys()).join(', ')}`);
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
const rowCollectionName = rowCollection.name;
|
|
392
|
-
findOptions.raw ? row['__collection'] = rowCollectionName : row.set('__collection', rowCollectionName, {
|
|
393
|
-
raw: true
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
} catch (err) {
|
|
397
|
-
_iterator.e(err);
|
|
398
|
-
} finally {
|
|
399
|
-
_iterator.f();
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
375
|
(0, _listeners.registerBuiltInListeners)(this);
|
|
404
376
|
}
|
|
405
377
|
addMigration(item) {
|
|
@@ -415,11 +387,11 @@ class Database extends _events().EventEmitter {
|
|
|
415
387
|
const files = _glob().default.sync(patten, {
|
|
416
388
|
ignore: ['**/*.d.ts']
|
|
417
389
|
});
|
|
418
|
-
var
|
|
419
|
-
|
|
390
|
+
var _iterator = _createForOfIteratorHelper(files),
|
|
391
|
+
_step;
|
|
420
392
|
try {
|
|
421
|
-
for (
|
|
422
|
-
const file =
|
|
393
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
394
|
+
const file = _step.value;
|
|
423
395
|
let filename = (0, _path().basename)(file);
|
|
424
396
|
filename = filename.substring(0, filename.lastIndexOf('.')) || filename;
|
|
425
397
|
this.migrations.add({
|
|
@@ -429,9 +401,9 @@ class Database extends _events().EventEmitter {
|
|
|
429
401
|
});
|
|
430
402
|
}
|
|
431
403
|
} catch (err) {
|
|
432
|
-
|
|
404
|
+
_iterator.e(err);
|
|
433
405
|
} finally {
|
|
434
|
-
|
|
406
|
+
_iterator.f();
|
|
435
407
|
}
|
|
436
408
|
}
|
|
437
409
|
inDialect(...dialect) {
|
|
@@ -658,20 +630,20 @@ class Database extends _events().EventEmitter {
|
|
|
658
630
|
});
|
|
659
631
|
const skip = options.skip || [];
|
|
660
632
|
// @ts-ignore
|
|
661
|
-
var
|
|
662
|
-
|
|
633
|
+
var _iterator2 = _createForOfIteratorHelper(tableNames),
|
|
634
|
+
_step2;
|
|
663
635
|
try {
|
|
664
|
-
for (
|
|
665
|
-
const tableName =
|
|
636
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
637
|
+
const tableName = _step2.value;
|
|
666
638
|
if (skip.includes(tableName)) {
|
|
667
639
|
continue;
|
|
668
640
|
}
|
|
669
641
|
yield _this4.sequelize.query(`DROP TABLE IF EXISTS ${tableName} CASCADE`);
|
|
670
642
|
}
|
|
671
643
|
} catch (err) {
|
|
672
|
-
|
|
644
|
+
_iterator2.e(err);
|
|
673
645
|
} finally {
|
|
674
|
-
|
|
646
|
+
_iterator2.f();
|
|
675
647
|
}
|
|
676
648
|
return;
|
|
677
649
|
}
|
|
@@ -788,11 +760,11 @@ class Database extends _events().EventEmitter {
|
|
|
788
760
|
const reader = new _collectionImporter.ImporterReader(options.directory, options.extensions);
|
|
789
761
|
const modules = yield reader.read();
|
|
790
762
|
const result = new Map();
|
|
791
|
-
var
|
|
792
|
-
|
|
763
|
+
var _iterator3 = _createForOfIteratorHelper(modules),
|
|
764
|
+
_step3;
|
|
793
765
|
try {
|
|
794
|
-
for (
|
|
795
|
-
const module =
|
|
766
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
767
|
+
const module = _step3.value;
|
|
796
768
|
if (module.extend) {
|
|
797
769
|
_this10.extendCollection(module.collectionOptions, module.mergeOptions);
|
|
798
770
|
} else {
|
|
@@ -804,9 +776,9 @@ class Database extends _events().EventEmitter {
|
|
|
804
776
|
}
|
|
805
777
|
}
|
|
806
778
|
} catch (err) {
|
|
807
|
-
|
|
779
|
+
_iterator3.e(err);
|
|
808
780
|
} finally {
|
|
809
|
-
|
|
781
|
+
_iterator3.f();
|
|
810
782
|
}
|
|
811
783
|
return result;
|
|
812
784
|
})();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Association, Includeable, Model, ModelStatic, Transaction } from 'sequelize';
|
|
2
|
+
interface EagerLoadingNode {
|
|
3
|
+
model: ModelStatic<any>;
|
|
4
|
+
association: Association;
|
|
5
|
+
attributes: Array<string>;
|
|
6
|
+
rawAttributes: Array<string>;
|
|
7
|
+
children: Array<EagerLoadingNode>;
|
|
8
|
+
parent?: EagerLoadingNode;
|
|
9
|
+
instances?: Array<Model>;
|
|
10
|
+
order?: any;
|
|
11
|
+
}
|
|
12
|
+
export declare class EagerLoadingTree {
|
|
13
|
+
root: EagerLoadingNode;
|
|
14
|
+
constructor(root: EagerLoadingNode);
|
|
15
|
+
static buildFromSequelizeOptions(options: {
|
|
16
|
+
model: ModelStatic<any>;
|
|
17
|
+
rootAttributes: Array<string>;
|
|
18
|
+
rootOrder?: any;
|
|
19
|
+
includeOption: Includeable | Includeable[];
|
|
20
|
+
}): EagerLoadingTree;
|
|
21
|
+
load(pks: Array<string | number>, transaction?: Transaction): Promise<{}>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|