@nocobase/database 0.9.2-alpha.4 → 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 +104 -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/fields/field.js +1 -0
- package/lib/filter-parser.d.ts +1 -7
- package/lib/filter-parser.js +27 -7
- package/lib/listeners/adjacency-list.d.ts +1 -2
- package/lib/listeners/adjacency-list.js +2 -71
- 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 -1
- package/lib/mock-database.js +3 -1
- package/lib/operators/array.js +7 -4
- package/lib/operators/string.js +1 -1
- package/lib/options-parser.js +14 -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 +106 -90
- package/lib/sql-parser/postgres.js +41 -0
- package/lib/tree-repository/adjacency-list-repository.d.ts +9 -0
- package/lib/tree-repository/adjacency-list-repository.js +165 -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__/collection.test.ts +19 -0
- 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 +267 -1
- package/src/__tests__/repository.test.ts +30 -0
- package/src/__tests__/tree.test.ts +266 -3
- package/src/__tests__/update-guard.test.ts +13 -0
- package/src/collection.ts +79 -65
- package/src/database.ts +26 -42
- package/src/eager-loading/eager-loading-tree.ts +304 -0
- package/src/fields/field.ts +4 -0
- package/src/filter-parser.ts +16 -2
- package/src/listeners/adjacency-list.ts +1 -44
- package/src/listeners/append-child-collection-name-after-repository-find.ts +31 -0
- package/src/listeners/index.ts +3 -2
- package/src/mock-database.ts +3 -1
- package/src/operators/array.ts +8 -4
- package/src/operators/notIn.ts +1 -0
- package/src/operators/string.ts +1 -1
- package/src/options-parser.ts +17 -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 +83 -38
- package/src/sql-parser/postgres.js +25505 -0
- package/src/tree-repository/adjacency-list-repository.ts +159 -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
|
@@ -35,6 +35,7 @@ function _sequelize() {
|
|
|
35
35
|
var _model = require("./model");
|
|
36
36
|
var _repository = require("./repository");
|
|
37
37
|
var _utils = require("./utils");
|
|
38
|
+
var _adjacencyListRepository = require("./tree-repository/adjacency-list-repository");
|
|
38
39
|
const _excluded = ["name"],
|
|
39
40
|
_excluded2 = ["name"];
|
|
40
41
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -55,6 +56,30 @@ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol
|
|
|
55
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); }
|
|
56
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; }
|
|
57
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
|
+
}
|
|
58
83
|
get filterTargetKey() {
|
|
59
84
|
const targetKey = _lodash().default.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
|
|
60
85
|
if (!targetKey && this.model.rawAttributes['id']) {
|
|
@@ -107,53 +132,6 @@ class Collection extends _events().EventEmitter {
|
|
|
107
132
|
_iterator2.f();
|
|
108
133
|
}
|
|
109
134
|
}
|
|
110
|
-
constructor(options, context) {
|
|
111
|
-
super();
|
|
112
|
-
this.options = void 0;
|
|
113
|
-
this.context = void 0;
|
|
114
|
-
this.isThrough = void 0;
|
|
115
|
-
this.fields = new Map();
|
|
116
|
-
this.model = void 0;
|
|
117
|
-
this.repository = void 0;
|
|
118
|
-
this.context = context;
|
|
119
|
-
this.options = options;
|
|
120
|
-
this.checkOptions(options);
|
|
121
|
-
this.bindFieldEventListener();
|
|
122
|
-
this.modelInit();
|
|
123
|
-
this.db.modelCollection.set(this.model, this);
|
|
124
|
-
// set tableName to collection map
|
|
125
|
-
// the form of key is `${schema}.${tableName}` if schema exists
|
|
126
|
-
// otherwise is `${tableName}`
|
|
127
|
-
this.db.tableNameCollectionMap.set(this.getTableNameWithSchemaAsString(), this);
|
|
128
|
-
if (!options.inherits) {
|
|
129
|
-
this.setFields(options.fields);
|
|
130
|
-
}
|
|
131
|
-
this.setRepository(options.repository);
|
|
132
|
-
this.setSortable(options.sortable);
|
|
133
|
-
}
|
|
134
|
-
checkOptions(options) {
|
|
135
|
-
(0, _utils.checkIdentifier)(options.name);
|
|
136
|
-
this.checkTableName();
|
|
137
|
-
}
|
|
138
|
-
checkTableName() {
|
|
139
|
-
const tableName = this.tableName();
|
|
140
|
-
var _iterator3 = _createForOfIteratorHelper(this.db.collections),
|
|
141
|
-
_step3;
|
|
142
|
-
try {
|
|
143
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
144
|
-
const _step3$value = _slicedToArray(_step3.value, 2),
|
|
145
|
-
k = _step3$value[0],
|
|
146
|
-
collection = _step3$value[1];
|
|
147
|
-
if (collection.name != this.options.name && tableName === collection.tableName() && collection.collectionSchema() === this.collectionSchema()) {
|
|
148
|
-
throw new Error(`collection ${collection.name} and ${this.name} have same tableName "${tableName}"`);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
} catch (err) {
|
|
152
|
-
_iterator3.e(err);
|
|
153
|
-
} finally {
|
|
154
|
-
_iterator3.f();
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
135
|
tableName() {
|
|
158
136
|
const _this$options = this.options,
|
|
159
137
|
name = _this$options.name,
|
|
@@ -161,14 +139,6 @@ class Collection extends _events().EventEmitter {
|
|
|
161
139
|
const tName = tableName || name;
|
|
162
140
|
return this.options.underscored ? (0, _utils.snakeCase)(tName) : tName;
|
|
163
141
|
}
|
|
164
|
-
sequelizeModelOptions() {
|
|
165
|
-
const name = this.options.name;
|
|
166
|
-
return _objectSpread(_objectSpread({}, _lodash().default.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), {}, {
|
|
167
|
-
modelName: name,
|
|
168
|
-
sequelize: this.context.database.sequelize,
|
|
169
|
-
tableName: this.tableName()
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
142
|
/**
|
|
173
143
|
* TODO
|
|
174
144
|
*/
|
|
@@ -215,17 +185,11 @@ class Collection extends _events().EventEmitter {
|
|
|
215
185
|
if (typeof repository === 'string') {
|
|
216
186
|
repo = this.context.database.repositories.get(repository) || _repository.Repository;
|
|
217
187
|
}
|
|
188
|
+
if (this.options.tree == 'adjacency-list' || this.options.tree == 'adjacencyList') {
|
|
189
|
+
repo = _adjacencyListRepository.AdjacencyListRepository;
|
|
190
|
+
}
|
|
218
191
|
this.repository = new repo(this);
|
|
219
192
|
}
|
|
220
|
-
bindFieldEventListener() {
|
|
221
|
-
this.on('field.afterAdd', field => {
|
|
222
|
-
field.bind();
|
|
223
|
-
});
|
|
224
|
-
this.on('field.afterRemove', field => {
|
|
225
|
-
field.unbind();
|
|
226
|
-
this.db.emit('field.afterRemove', field);
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
193
|
forEachField(callback) {
|
|
230
194
|
return [...this.fields.values()].forEach(callback);
|
|
231
195
|
}
|
|
@@ -273,10 +237,14 @@ class Collection extends _events().EventEmitter {
|
|
|
273
237
|
sourceFieldName = _options$source$split2[1];
|
|
274
238
|
const sourceCollection = this.db.collections.get(sourceCollectionName);
|
|
275
239
|
if (!sourceCollection) {
|
|
276
|
-
|
|
240
|
+
this.db.logger.warn(`source collection "${sourceCollectionName}" not found for field "${name}" at collection "${this.name}"`);
|
|
277
241
|
}
|
|
278
242
|
const sourceField = sourceCollection.fields.get(sourceFieldName);
|
|
279
|
-
|
|
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
|
+
}
|
|
280
248
|
}
|
|
281
249
|
this.emit('field.beforeAdd', name, options, {
|
|
282
250
|
collection: this
|
|
@@ -298,13 +266,13 @@ class Collection extends _events().EventEmitter {
|
|
|
298
266
|
this.emit('field.afterAdd', field);
|
|
299
267
|
// refresh children models
|
|
300
268
|
if (this.isParent()) {
|
|
301
|
-
var
|
|
269
|
+
var _iterator3 = _createForOfIteratorHelper(this.context.database.inheritanceMap.getChildren(this.name, {
|
|
302
270
|
deep: false
|
|
303
271
|
})),
|
|
304
|
-
|
|
272
|
+
_step3;
|
|
305
273
|
try {
|
|
306
|
-
for (
|
|
307
|
-
const child =
|
|
274
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
275
|
+
const child = _step3.value;
|
|
308
276
|
const childCollection = this.db.getCollection(child);
|
|
309
277
|
const existField = childCollection.getField(name);
|
|
310
278
|
if (!existField || existField.options.inherit) {
|
|
@@ -314,9 +282,9 @@ class Collection extends _events().EventEmitter {
|
|
|
314
282
|
}
|
|
315
283
|
}
|
|
316
284
|
} catch (err) {
|
|
317
|
-
|
|
285
|
+
_iterator3.e(err);
|
|
318
286
|
} finally {
|
|
319
|
-
|
|
287
|
+
_iterator3.f();
|
|
320
288
|
}
|
|
321
289
|
}
|
|
322
290
|
return field;
|
|
@@ -328,34 +296,34 @@ class Collection extends _events().EventEmitter {
|
|
|
328
296
|
if (resetFields) {
|
|
329
297
|
this.resetFields();
|
|
330
298
|
}
|
|
331
|
-
var
|
|
332
|
-
|
|
299
|
+
var _iterator4 = _createForOfIteratorHelper(fields),
|
|
300
|
+
_step4;
|
|
333
301
|
try {
|
|
334
|
-
for (
|
|
335
|
-
const _ref =
|
|
302
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
303
|
+
const _ref = _step4.value;
|
|
336
304
|
const name = _ref.name,
|
|
337
305
|
options = _objectWithoutProperties(_ref, _excluded);
|
|
338
306
|
this.addField(name, options);
|
|
339
307
|
}
|
|
340
308
|
} catch (err) {
|
|
341
|
-
|
|
309
|
+
_iterator4.e(err);
|
|
342
310
|
} finally {
|
|
343
|
-
|
|
311
|
+
_iterator4.f();
|
|
344
312
|
}
|
|
345
313
|
}
|
|
346
314
|
resetFields() {
|
|
347
315
|
const fieldNames = this.fields.keys();
|
|
348
|
-
var
|
|
349
|
-
|
|
316
|
+
var _iterator5 = _createForOfIteratorHelper(fieldNames),
|
|
317
|
+
_step5;
|
|
350
318
|
try {
|
|
351
|
-
for (
|
|
352
|
-
const fieldName =
|
|
319
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
320
|
+
const fieldName = _step5.value;
|
|
353
321
|
this.removeField(fieldName);
|
|
354
322
|
}
|
|
355
323
|
} catch (err) {
|
|
356
|
-
|
|
324
|
+
_iterator5.e(err);
|
|
357
325
|
} finally {
|
|
358
|
-
|
|
326
|
+
_iterator5.f();
|
|
359
327
|
}
|
|
360
328
|
}
|
|
361
329
|
remove() {
|
|
@@ -387,13 +355,13 @@ class Collection extends _events().EventEmitter {
|
|
|
387
355
|
const bool = this.fields.delete(name);
|
|
388
356
|
if (bool) {
|
|
389
357
|
if (this.isParent()) {
|
|
390
|
-
var
|
|
358
|
+
var _iterator6 = _createForOfIteratorHelper(this.db.inheritanceMap.getChildren(this.name, {
|
|
391
359
|
deep: false
|
|
392
360
|
})),
|
|
393
|
-
|
|
361
|
+
_step6;
|
|
394
362
|
try {
|
|
395
|
-
for (
|
|
396
|
-
const child =
|
|
363
|
+
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
364
|
+
const child = _step6.value;
|
|
397
365
|
const childCollection = this.db.getCollection(child);
|
|
398
366
|
const existField = childCollection.getField(name);
|
|
399
367
|
if (existField && existField.options.inherit) {
|
|
@@ -401,9 +369,9 @@ class Collection extends _events().EventEmitter {
|
|
|
401
369
|
}
|
|
402
370
|
}
|
|
403
371
|
} catch (err) {
|
|
404
|
-
|
|
372
|
+
_iterator6.e(err);
|
|
405
373
|
} finally {
|
|
406
|
-
|
|
374
|
+
_iterator6.f();
|
|
407
375
|
}
|
|
408
376
|
}
|
|
409
377
|
this.emit('field.afterRemove', field);
|
|
@@ -493,11 +461,11 @@ class Collection extends _events().EventEmitter {
|
|
|
493
461
|
if (name.startsWith(`${indexName.join(',')},`)) {
|
|
494
462
|
return;
|
|
495
463
|
}
|
|
496
|
-
var
|
|
497
|
-
|
|
464
|
+
var _iterator7 = _createForOfIteratorHelper(indexes),
|
|
465
|
+
_step7;
|
|
498
466
|
try {
|
|
499
|
-
for (
|
|
500
|
-
const item =
|
|
467
|
+
for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
|
|
468
|
+
const item = _step7.value;
|
|
501
469
|
if (_lodash().default.isEqual(item.fields, indexName)) {
|
|
502
470
|
return;
|
|
503
471
|
}
|
|
@@ -507,9 +475,9 @@ class Collection extends _events().EventEmitter {
|
|
|
507
475
|
}
|
|
508
476
|
}
|
|
509
477
|
} catch (err) {
|
|
510
|
-
|
|
478
|
+
_iterator7.e(err);
|
|
511
479
|
} finally {
|
|
512
|
-
|
|
480
|
+
_iterator7.f();
|
|
513
481
|
}
|
|
514
482
|
if (!indexItem) {
|
|
515
483
|
return;
|
|
@@ -627,5 +595,45 @@ class Collection extends _events().EventEmitter {
|
|
|
627
595
|
isView() {
|
|
628
596
|
return false;
|
|
629
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
|
+
}
|
|
630
638
|
}
|
|
631
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 {};
|