@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/collection.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
-
import {
|
|
3
|
+
import { ModelOptions, QueryInterfaceDropTableOptions, SyncOptions, Transactionable, ModelStatic } from 'sequelize';
|
|
4
4
|
import { Database } from './database';
|
|
5
5
|
import { Field, FieldOptions } from './fields';
|
|
6
6
|
import { Model } from './model';
|
|
@@ -13,9 +13,10 @@ export declare type CollectionSortable = string | boolean | {
|
|
|
13
13
|
export interface CollectionOptions extends Omit<ModelOptions, 'name' | 'hooks'> {
|
|
14
14
|
name: string;
|
|
15
15
|
tableName?: string;
|
|
16
|
+
inherits?: string[] | string;
|
|
16
17
|
filterTargetKey?: string;
|
|
17
18
|
fields?: FieldOptions[];
|
|
18
|
-
model?: string |
|
|
19
|
+
model?: string | ModelStatic<Model>;
|
|
19
20
|
repository?: string | RepositoryType;
|
|
20
21
|
sortable?: CollectionSortable;
|
|
21
22
|
/**
|
|
@@ -36,7 +37,7 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
36
37
|
context: CollectionContext;
|
|
37
38
|
isThrough?: boolean;
|
|
38
39
|
fields: Map<string, any>;
|
|
39
|
-
model:
|
|
40
|
+
model: ModelStatic<Model>;
|
|
40
41
|
repository: Repository<TModelAttributes, TCreationAttributes>;
|
|
41
42
|
get filterTargetKey(): string;
|
|
42
43
|
get name(): string;
|
|
@@ -80,5 +81,8 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
80
81
|
[key: string]: any;
|
|
81
82
|
}): void;
|
|
82
83
|
removeIndex(fields: any): void;
|
|
84
|
+
refreshIndexes(): void;
|
|
83
85
|
sync(syncOptions?: SyncOptions): Promise<void>;
|
|
86
|
+
isInherited(): boolean;
|
|
87
|
+
isParent(): boolean;
|
|
84
88
|
}
|
package/lib/collection.js
CHANGED
|
@@ -103,7 +103,12 @@ class Collection extends _events().EventEmitter {
|
|
|
103
103
|
this.bindFieldEventListener();
|
|
104
104
|
this.modelInit();
|
|
105
105
|
this.db.modelCollection.set(this.model, this);
|
|
106
|
-
this.
|
|
106
|
+
this.db.tableNameCollectionMap.set(this.model.tableName, this);
|
|
107
|
+
|
|
108
|
+
if (!options.inherits) {
|
|
109
|
+
this.setFields(options.fields);
|
|
110
|
+
}
|
|
111
|
+
|
|
107
112
|
this.setRepository(options.repository);
|
|
108
113
|
this.setSortable(options.sortable);
|
|
109
114
|
}
|
|
@@ -184,8 +189,12 @@ class Collection extends _events().EventEmitter {
|
|
|
184
189
|
}
|
|
185
190
|
|
|
186
191
|
bindFieldEventListener() {
|
|
187
|
-
this.on('field.afterAdd', field =>
|
|
188
|
-
|
|
192
|
+
this.on('field.afterAdd', field => {
|
|
193
|
+
field.bind();
|
|
194
|
+
});
|
|
195
|
+
this.on('field.afterRemove', field => {
|
|
196
|
+
field.unbind();
|
|
197
|
+
});
|
|
189
198
|
}
|
|
190
199
|
|
|
191
200
|
forEachField(callback) {
|
|
@@ -216,9 +225,45 @@ class Collection extends _events().EventEmitter {
|
|
|
216
225
|
}, options), _objectSpread(_objectSpread({}, this.context), {}, {
|
|
217
226
|
collection: this
|
|
218
227
|
}));
|
|
228
|
+
const oldField = this.fields.get(name);
|
|
229
|
+
|
|
230
|
+
if (oldField && oldField.options.inherit && field.typeToString() != oldField.typeToString()) {
|
|
231
|
+
throw new Error(`Field type conflict: cannot set "${name}" on "${this.name}" to ${options.type}, parent "${name}" type is ${oldField.options.type}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (this.options.autoGenId !== false && options.primaryKey) {
|
|
235
|
+
this.model.removeAttribute('id');
|
|
236
|
+
}
|
|
237
|
+
|
|
219
238
|
this.removeField(name);
|
|
220
239
|
this.fields.set(name, field);
|
|
221
|
-
this.emit('field.afterAdd', field);
|
|
240
|
+
this.emit('field.afterAdd', field); // refresh children models
|
|
241
|
+
|
|
242
|
+
if (this.isParent()) {
|
|
243
|
+
var _iterator = _createForOfIteratorHelper(this.context.database.inheritanceMap.getChildren(this.name, {
|
|
244
|
+
deep: false
|
|
245
|
+
})),
|
|
246
|
+
_step;
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
250
|
+
const child = _step.value;
|
|
251
|
+
const childCollection = this.db.getCollection(child);
|
|
252
|
+
const existField = childCollection.getField(name);
|
|
253
|
+
|
|
254
|
+
if (!existField || existField.options.inherit) {
|
|
255
|
+
childCollection.setField(name, _objectSpread(_objectSpread({}, options), {}, {
|
|
256
|
+
inherit: true
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
} catch (err) {
|
|
261
|
+
_iterator.e(err);
|
|
262
|
+
} finally {
|
|
263
|
+
_iterator.f();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
222
267
|
return field;
|
|
223
268
|
}
|
|
224
269
|
|
|
@@ -231,12 +276,12 @@ class Collection extends _events().EventEmitter {
|
|
|
231
276
|
this.resetFields();
|
|
232
277
|
}
|
|
233
278
|
|
|
234
|
-
var
|
|
235
|
-
|
|
279
|
+
var _iterator2 = _createForOfIteratorHelper(fields),
|
|
280
|
+
_step2;
|
|
236
281
|
|
|
237
282
|
try {
|
|
238
|
-
for (
|
|
239
|
-
const _ref =
|
|
283
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
284
|
+
const _ref = _step2.value;
|
|
240
285
|
|
|
241
286
|
const name = _ref.name,
|
|
242
287
|
options = _objectWithoutProperties(_ref, _excluded);
|
|
@@ -244,27 +289,27 @@ class Collection extends _events().EventEmitter {
|
|
|
244
289
|
this.addField(name, options);
|
|
245
290
|
}
|
|
246
291
|
} catch (err) {
|
|
247
|
-
|
|
292
|
+
_iterator2.e(err);
|
|
248
293
|
} finally {
|
|
249
|
-
|
|
294
|
+
_iterator2.f();
|
|
250
295
|
}
|
|
251
296
|
}
|
|
252
297
|
|
|
253
298
|
resetFields() {
|
|
254
299
|
const fieldNames = this.fields.keys();
|
|
255
300
|
|
|
256
|
-
var
|
|
257
|
-
|
|
301
|
+
var _iterator3 = _createForOfIteratorHelper(fieldNames),
|
|
302
|
+
_step3;
|
|
258
303
|
|
|
259
304
|
try {
|
|
260
|
-
for (
|
|
261
|
-
const fieldName =
|
|
305
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
306
|
+
const fieldName = _step3.value;
|
|
262
307
|
this.removeField(fieldName);
|
|
263
308
|
}
|
|
264
309
|
} catch (err) {
|
|
265
|
-
|
|
310
|
+
_iterator3.e(err);
|
|
266
311
|
} finally {
|
|
267
|
-
|
|
312
|
+
_iterator3.f();
|
|
268
313
|
}
|
|
269
314
|
}
|
|
270
315
|
|
|
@@ -305,6 +350,29 @@ class Collection extends _events().EventEmitter {
|
|
|
305
350
|
const bool = this.fields.delete(name);
|
|
306
351
|
|
|
307
352
|
if (bool) {
|
|
353
|
+
if (this.isParent()) {
|
|
354
|
+
var _iterator4 = _createForOfIteratorHelper(this.db.inheritanceMap.getChildren(this.name, {
|
|
355
|
+
deep: false
|
|
356
|
+
})),
|
|
357
|
+
_step4;
|
|
358
|
+
|
|
359
|
+
try {
|
|
360
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
361
|
+
const child = _step4.value;
|
|
362
|
+
const childCollection = this.db.getCollection(child);
|
|
363
|
+
const existField = childCollection.getField(name);
|
|
364
|
+
|
|
365
|
+
if (existField && existField.options.inherit) {
|
|
366
|
+
childCollection.removeField(name);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
} catch (err) {
|
|
370
|
+
_iterator4.e(err);
|
|
371
|
+
} finally {
|
|
372
|
+
_iterator4.f();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
308
376
|
this.emit('field.afterRemove', field);
|
|
309
377
|
}
|
|
310
378
|
|
|
@@ -407,12 +475,12 @@ class Collection extends _events().EventEmitter {
|
|
|
407
475
|
return;
|
|
408
476
|
}
|
|
409
477
|
|
|
410
|
-
var
|
|
411
|
-
|
|
478
|
+
var _iterator5 = _createForOfIteratorHelper(indexes),
|
|
479
|
+
_step5;
|
|
412
480
|
|
|
413
481
|
try {
|
|
414
|
-
for (
|
|
415
|
-
const item =
|
|
482
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
483
|
+
const item = _step5.value;
|
|
416
484
|
|
|
417
485
|
if (_lodash().default.isEqual(item.fields, indexName)) {
|
|
418
486
|
return;
|
|
@@ -425,9 +493,9 @@ class Collection extends _events().EventEmitter {
|
|
|
425
493
|
}
|
|
426
494
|
}
|
|
427
495
|
} catch (err) {
|
|
428
|
-
|
|
496
|
+
_iterator5.e(err);
|
|
429
497
|
} finally {
|
|
430
|
-
|
|
498
|
+
_iterator5.f();
|
|
431
499
|
}
|
|
432
500
|
|
|
433
501
|
if (!indexItem) {
|
|
@@ -446,6 +514,7 @@ class Collection extends _events().EventEmitter {
|
|
|
446
514
|
|
|
447
515
|
return item;
|
|
448
516
|
});
|
|
517
|
+
this.refreshIndexes();
|
|
449
518
|
}
|
|
450
519
|
|
|
451
520
|
removeIndex(fields) {
|
|
@@ -459,6 +528,33 @@ class Collection extends _events().EventEmitter {
|
|
|
459
528
|
this.model._indexes = indexes.filter(item => {
|
|
460
529
|
return !_lodash().default.isEqual(item.fields, fields);
|
|
461
530
|
});
|
|
531
|
+
this.refreshIndexes();
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
refreshIndexes() {
|
|
535
|
+
// @ts-ignore
|
|
536
|
+
const indexes = this.model._indexes; // @ts-ignore
|
|
537
|
+
|
|
538
|
+
this.model._indexes = indexes.filter(item => {
|
|
539
|
+
var _iterator6 = _createForOfIteratorHelper(item.fields),
|
|
540
|
+
_step6;
|
|
541
|
+
|
|
542
|
+
try {
|
|
543
|
+
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
544
|
+
const field = _step6.value;
|
|
545
|
+
|
|
546
|
+
if (!this.model.rawAttributes[field]) {
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
} catch (err) {
|
|
551
|
+
_iterator6.e(err);
|
|
552
|
+
} finally {
|
|
553
|
+
_iterator6.f();
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
return true;
|
|
557
|
+
});
|
|
462
558
|
}
|
|
463
559
|
|
|
464
560
|
sync(syncOptions) {
|
|
@@ -492,6 +588,14 @@ class Collection extends _events().EventEmitter {
|
|
|
492
588
|
})();
|
|
493
589
|
}
|
|
494
590
|
|
|
591
|
+
isInherited() {
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
isParent() {
|
|
596
|
+
return this.context.database.inheritanceMap.isParentNode(this.name);
|
|
597
|
+
}
|
|
598
|
+
|
|
495
599
|
}
|
|
496
600
|
|
|
497
601
|
exports.Collection = Collection;
|
package/lib/database.d.ts
CHANGED
|
@@ -2,24 +2,26 @@
|
|
|
2
2
|
import { AsyncEmitter } from '@nocobase/utils';
|
|
3
3
|
import merge from 'deepmerge';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
|
-
import {
|
|
5
|
+
import { ModelStatic, Options, QueryInterfaceDropAllTablesOptions, QueryOptions, Sequelize, SyncOptions, Transactionable } from 'sequelize';
|
|
6
6
|
import { Umzug } from 'umzug';
|
|
7
7
|
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
|
8
8
|
import { ImportFileExtension } from './collection-importer';
|
|
9
|
+
import ReferencesMap from './features/ReferencesMap';
|
|
10
|
+
import { ArrayFieldRepository } from './field-repository/array-field-repository';
|
|
9
11
|
import * as FieldTypes from './fields';
|
|
10
12
|
import { Field, FieldContext, RelationField } from './fields';
|
|
13
|
+
import InheritanceMap from './inherited-map';
|
|
11
14
|
import { MigrationItem, Migrations } from './migration';
|
|
12
15
|
import { Model } from './model';
|
|
13
16
|
import { ModelHook } from './model-hook';
|
|
14
17
|
import { RelationRepository } from './relation-repository/relation-repository';
|
|
15
18
|
import { Repository } from './repository';
|
|
16
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';
|
|
17
|
-
import ReferencesMap from './features/ReferencesMap';
|
|
18
20
|
export interface MergeOptions extends merge.Options {
|
|
19
21
|
}
|
|
20
22
|
export interface PendingOptions {
|
|
21
23
|
field: RelationField;
|
|
22
|
-
model:
|
|
24
|
+
model: ModelStatic<Model>;
|
|
23
25
|
}
|
|
24
26
|
interface MapOf<T> {
|
|
25
27
|
[key: string]: T;
|
|
@@ -27,6 +29,7 @@ interface MapOf<T> {
|
|
|
27
29
|
export interface IDatabaseOptions extends Options {
|
|
28
30
|
tablePrefix?: string;
|
|
29
31
|
migrator?: any;
|
|
32
|
+
usingBigIntForId?: boolean;
|
|
30
33
|
}
|
|
31
34
|
export declare type DatabaseOptions = IDatabaseOptions;
|
|
32
35
|
interface RegisterOperatorsContext {
|
|
@@ -56,13 +59,15 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
56
59
|
migrations: Migrations;
|
|
57
60
|
fieldTypes: Map<any, any>;
|
|
58
61
|
options: IDatabaseOptions;
|
|
59
|
-
models: Map<string,
|
|
62
|
+
models: Map<string, ModelStatic<Model<any, any>>>;
|
|
60
63
|
repositories: Map<string, typeof Repository>;
|
|
61
64
|
operators: Map<any, any>;
|
|
62
65
|
collections: Map<string, Collection<any, any>>;
|
|
63
66
|
pendingFields: Map<string, FieldTypes.RelationField[]>;
|
|
64
|
-
modelCollection: Map<
|
|
67
|
+
modelCollection: Map<ModelStatic<any>, Collection<any, any>>;
|
|
68
|
+
tableNameCollectionMap: Map<string, Collection<any, any>>;
|
|
65
69
|
referenceMap: ReferencesMap;
|
|
70
|
+
inheritanceMap: InheritanceMap;
|
|
66
71
|
modelHook: ModelHook;
|
|
67
72
|
version: DatabaseVersion;
|
|
68
73
|
delayCollectionExtend: Map<string, {
|
|
@@ -87,13 +92,14 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
87
92
|
getCollection(name: string): Collection;
|
|
88
93
|
hasCollection(name: string): boolean;
|
|
89
94
|
removeCollection(name: string): Collection<any, any>;
|
|
90
|
-
getModel<M extends Model>(name: string):
|
|
95
|
+
getModel<M extends Model>(name: string): ModelStatic<M>;
|
|
91
96
|
getRepository<R extends Repository>(name: string): R;
|
|
92
97
|
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
|
|
98
|
+
getRepository<R extends ArrayFieldRepository>(name: string, relationId: string | number): R;
|
|
93
99
|
addPendingField(field: RelationField): void;
|
|
94
100
|
removePendingField(field: RelationField): void;
|
|
95
101
|
registerFieldTypes(fieldTypes: MapOf<typeof Field>): void;
|
|
96
|
-
registerModels(models: MapOf<
|
|
102
|
+
registerModels(models: MapOf<ModelStatic<any>>): void;
|
|
97
103
|
registerRepositories(repositories: MapOf<RepositoryType>): void;
|
|
98
104
|
initOperators(): void;
|
|
99
105
|
registerOperators(operators: MapOf<OperatorFunc>): void;
|
package/lib/database.js
CHANGED
|
@@ -90,18 +90,22 @@ var _collection = require("./collection");
|
|
|
90
90
|
|
|
91
91
|
var _collectionImporter = require("./collection-importer");
|
|
92
92
|
|
|
93
|
+
var _ReferencesMap = _interopRequireDefault(require("./features/ReferencesMap"));
|
|
94
|
+
|
|
95
|
+
var _referentialIntegrityCheck = require("./features/referential-integrity-check");
|
|
96
|
+
|
|
93
97
|
var FieldTypes = _interopRequireWildcard(require("./fields"));
|
|
94
98
|
|
|
99
|
+
var _inheritedCollection = require("./inherited-collection");
|
|
100
|
+
|
|
101
|
+
var _inheritedMap = _interopRequireDefault(require("./inherited-map"));
|
|
102
|
+
|
|
95
103
|
var _migration = require("./migration");
|
|
96
104
|
|
|
97
105
|
var _modelHook = require("./model-hook");
|
|
98
106
|
|
|
99
107
|
var _operators = _interopRequireDefault(require("./operators"));
|
|
100
108
|
|
|
101
|
-
var _referentialIntegrityCheck = require("./features/referential-integrity-check");
|
|
102
|
-
|
|
103
|
-
var _ReferencesMap = _interopRequireDefault(require("./features/ReferencesMap"));
|
|
104
|
-
|
|
105
109
|
const _excluded = ["drop"],
|
|
106
110
|
_excluded2 = ["retry"];
|
|
107
111
|
|
|
@@ -196,8 +200,7 @@ class DatabaseVersion {
|
|
|
196
200
|
|
|
197
201
|
class Database extends _events().EventEmitter {
|
|
198
202
|
constructor(options) {
|
|
199
|
-
super();
|
|
200
|
-
|
|
203
|
+
super();
|
|
201
204
|
this.sequelize = void 0;
|
|
202
205
|
this.migrator = void 0;
|
|
203
206
|
this.migrations = void 0;
|
|
@@ -209,7 +212,9 @@ class Database extends _events().EventEmitter {
|
|
|
209
212
|
this.collections = new Map();
|
|
210
213
|
this.pendingFields = new Map();
|
|
211
214
|
this.modelCollection = new Map();
|
|
215
|
+
this.tableNameCollectionMap = new Map();
|
|
212
216
|
this.referenceMap = new _ReferencesMap.default();
|
|
217
|
+
this.inheritanceMap = new _inheritedMap.default();
|
|
213
218
|
this.modelHook = void 0;
|
|
214
219
|
this.version = void 0;
|
|
215
220
|
this.delayCollectionExtend = new Map();
|
|
@@ -236,6 +241,11 @@ class Database extends _events().EventEmitter {
|
|
|
236
241
|
opts.timezone = '+00:00';
|
|
237
242
|
}
|
|
238
243
|
|
|
244
|
+
if (options.dialect === 'postgres') {
|
|
245
|
+
// https://github.com/sequelize/sequelize/issues/1774
|
|
246
|
+
require('pg').defaults.parseInt8 = true;
|
|
247
|
+
}
|
|
248
|
+
|
|
239
249
|
this.sequelize = new (_sequelize().Sequelize)(opts);
|
|
240
250
|
this.options = opts;
|
|
241
251
|
this.collections = new Map();
|
|
@@ -332,6 +342,19 @@ class Database extends _events().EventEmitter {
|
|
|
332
342
|
return _ref3.apply(this, arguments);
|
|
333
343
|
};
|
|
334
344
|
}());
|
|
345
|
+
this.on('afterRemoveCollection', collection => {
|
|
346
|
+
this.inheritanceMap.removeNode(collection.name);
|
|
347
|
+
});
|
|
348
|
+
this.on('afterDefine', model => {
|
|
349
|
+
if (_lodash().default.get(this.options, 'usingBigIntForId', true)) {
|
|
350
|
+
const idAttribute = model.rawAttributes['id'];
|
|
351
|
+
|
|
352
|
+
if (idAttribute && idAttribute.primaryKey) {
|
|
353
|
+
model.rawAttributes['id'].type = _sequelize().DataTypes.BIGINT;
|
|
354
|
+
model.refreshAttributes();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
});
|
|
335
358
|
}
|
|
336
359
|
|
|
337
360
|
addMigration(item) {
|
|
@@ -382,7 +405,14 @@ class Database extends _events().EventEmitter {
|
|
|
382
405
|
|
|
383
406
|
collection(options) {
|
|
384
407
|
this.emit('beforeDefineCollection', options);
|
|
385
|
-
|
|
408
|
+
|
|
409
|
+
const hasValidInheritsOptions = (() => {
|
|
410
|
+
return options.inherits && _lodash().default.castArray(options.inherits).length > 0;
|
|
411
|
+
})();
|
|
412
|
+
|
|
413
|
+
const collection = hasValidInheritsOptions ? new _inheritedCollection.InheritedCollection(options, {
|
|
414
|
+
database: this
|
|
415
|
+
}) : new _collection.Collection(options, {
|
|
386
416
|
database: this
|
|
387
417
|
});
|
|
388
418
|
this.collections.set(collection.name, collection);
|
|
@@ -410,6 +440,7 @@ class Database extends _events().EventEmitter {
|
|
|
410
440
|
removeCollection(name) {
|
|
411
441
|
const collection = this.collections.get(name);
|
|
412
442
|
this.emit('beforeRemoveCollection', collection);
|
|
443
|
+
collection.resetFields();
|
|
413
444
|
const result = this.collections.delete(name);
|
|
414
445
|
this.sequelize.modelManager.removeModel(collection.model);
|
|
415
446
|
|
|
@@ -593,7 +624,7 @@ class Database extends _events().EventEmitter {
|
|
|
593
624
|
return true;
|
|
594
625
|
} catch (error) {
|
|
595
626
|
if (count >= retry) {
|
|
596
|
-
throw
|
|
627
|
+
throw new Error('Connection failed, please check your database connection credentials and try again.');
|
|
597
628
|
}
|
|
598
629
|
|
|
599
630
|
console.log('reconnecting...', count);
|
|
@@ -11,6 +11,10 @@ const mustHaveFilter = () => (target, propertyKey, descriptor) => {
|
|
|
11
11
|
descriptor.value = function () {
|
|
12
12
|
const options = arguments[0];
|
|
13
13
|
|
|
14
|
+
if (Array.isArray(options.values)) {
|
|
15
|
+
return oldValue.apply(this, arguments);
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
if (!(options === null || options === void 0 ? void 0 : options.filter) && !(options === null || options === void 0 ? void 0 : options.filterByTk) && !(options === null || options === void 0 ? void 0 : options.forceUpdate)) {
|
|
15
19
|
throw new Error(`must provide filter or filterByTk for ${propertyKey} call, or set forceUpdate to true`);
|
|
16
20
|
}
|
|
@@ -11,21 +11,26 @@ class ReferencesMap {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
addReference(reference) {
|
|
14
|
+
if (!reference.onDelete) {
|
|
15
|
+
reference.onDelete = 'SET NULL';
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
const existReference = this.existReference(reference);
|
|
15
19
|
|
|
16
|
-
if (existReference) {
|
|
17
|
-
if (reference.onDelete
|
|
20
|
+
if (existReference && existReference.onDelete !== reference.onDelete) {
|
|
21
|
+
if (reference.onDelete === 'SET NULL') {
|
|
22
|
+
// using existing reference
|
|
23
|
+
return;
|
|
24
|
+
} else if (existReference.onDelete === 'SET NULL') {
|
|
25
|
+
existReference.onDelete = reference.onDelete;
|
|
26
|
+
} else {
|
|
18
27
|
throw new Error(`On Delete Conflict, exist reference ${JSON.stringify(existReference)}, new reference ${JSON.stringify(reference)}`);
|
|
19
28
|
}
|
|
20
|
-
|
|
21
|
-
return;
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
if (!
|
|
25
|
-
reference.
|
|
31
|
+
if (!existReference) {
|
|
32
|
+
this.map.set(reference.targetCollectionName, [...(this.map.get(reference.targetCollectionName) || []), reference]);
|
|
26
33
|
}
|
|
27
|
-
|
|
28
|
-
this.map.set(reference.targetCollectionName, [...(this.map.get(reference.targetCollectionName) || []), reference]);
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
getReferences(collectionName) {
|
|
@@ -50,7 +55,7 @@ class ReferencesMap {
|
|
|
50
55
|
return;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
|
-
const keys =
|
|
58
|
+
const keys = ['sourceCollectionName', 'sourceField', 'targetField', 'targetCollectionName'];
|
|
54
59
|
this.map.set(reference.targetCollectionName, references.filter(ref => !keys.every(key => ref[key] === reference[key])));
|
|
55
60
|
}
|
|
56
61
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Transactionable } from 'sequelize/types';
|
|
2
|
+
import { Collection } from '../collection';
|
|
3
|
+
export declare class ArrayFieldRepository {
|
|
4
|
+
protected collection: Collection;
|
|
5
|
+
protected fieldName: string;
|
|
6
|
+
protected targetValue: string | number;
|
|
7
|
+
constructor(collection: Collection, fieldName: string, targetValue: string | number);
|
|
8
|
+
get(options?: Transactionable): Promise<any>;
|
|
9
|
+
find(options?: Transactionable): Promise<any>;
|
|
10
|
+
set(options: Transactionable & {
|
|
11
|
+
values: Array<string | number> | string | number;
|
|
12
|
+
hooks?: boolean;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
protected emitAfterSave(instance: any, options: any): Promise<void>;
|
|
15
|
+
toggle(options: Transactionable & {
|
|
16
|
+
value: string | number;
|
|
17
|
+
hooks?: boolean;
|
|
18
|
+
}): Promise<void>;
|
|
19
|
+
add(options: Transactionable & {
|
|
20
|
+
values: Array<string | number> | string | number;
|
|
21
|
+
hooks?: boolean;
|
|
22
|
+
}): Promise<void>;
|
|
23
|
+
remove(options: Transactionable & {
|
|
24
|
+
values: Array<string | number> | string | number;
|
|
25
|
+
hooks?: boolean;
|
|
26
|
+
}): Promise<void>;
|
|
27
|
+
protected getInstance(options: Transactionable): Promise<any>;
|
|
28
|
+
}
|