@nocobase/database 0.7.0-alpha.34 → 0.7.0-alpha.57
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-importer.js +101 -67
- package/lib/collection.js +339 -210
- package/lib/database.js +488 -304
- package/lib/fields/array-field.js +45 -25
- package/lib/fields/belongs-to-field.js +101 -54
- package/lib/fields/belongs-to-many-field.js +98 -53
- package/lib/fields/boolean-field.js +24 -9
- package/lib/fields/context-field.js +77 -45
- package/lib/fields/date-field.js +24 -9
- package/lib/fields/field.js +114 -75
- package/lib/fields/has-inverse-field.js +4 -2
- package/lib/fields/has-many-field.js +105 -56
- package/lib/fields/has-one-field.js +105 -54
- package/lib/fields/index.js +277 -33
- package/lib/fields/json-field.js +36 -16
- package/lib/fields/number-field.js +53 -26
- package/lib/fields/password-field.js +118 -73
- package/lib/fields/radio-field.js +75 -47
- package/lib/fields/relation-field.js +41 -28
- package/lib/fields/sort-field.js +165 -89
- package/lib/fields/string-field.js +24 -9
- package/lib/fields/text-field.js +24 -9
- package/lib/fields/time-field.js +24 -9
- package/lib/fields/uid-field.js +57 -28
- package/lib/fields/uuid-field.js +36 -12
- package/lib/fields/virtual-field.js +24 -9
- package/lib/filter-parser.js +288 -179
- package/lib/index.js +210 -29
- package/lib/magic-attribute-model.js +123 -71
- package/lib/mock-database.js +68 -34
- package/lib/model-hook.js +101 -59
- package/lib/model.js +116 -81
- package/lib/operators/array.js +136 -96
- package/lib/operators/association.js +30 -14
- package/lib/operators/date.js +78 -34
- package/lib/operators/empty.js +113 -75
- package/lib/operators/index.js +15 -3
- package/lib/operators/ne.js +27 -12
- package/lib/operators/notIn.js +27 -12
- package/lib/operators/string.js +56 -35
- package/lib/operators/utils.js +18 -10
- package/lib/options-parser.js +323 -215
- package/lib/playground.js +66 -53
- package/lib/relation-repository/belongs-to-many-repository.js +281 -198
- package/lib/relation-repository/belongs-to-repository.js +10 -6
- package/lib/relation-repository/hasmany-repository.js +168 -121
- package/lib/relation-repository/hasone-repository.js +10 -6
- package/lib/relation-repository/multiple-relation-repository.js +263 -148
- package/lib/relation-repository/relation-repository.js +155 -97
- package/lib/relation-repository/single-relation-repository.js +138 -99
- package/lib/relation-repository/types.js +4 -2
- package/lib/repository.js +465 -291
- package/lib/transaction-decorator.js +80 -67
- package/lib/update-associations.js +525 -321
- package/lib/update-guard.js +160 -117
- package/package.json +3 -8
- package/src/database.ts +9 -3
- package/src/mock-database.ts +1 -1
- package/esm/collection-importer.d.ts +0 -7
- package/esm/collection-importer.js +0 -49
- package/esm/collection-importer.js.map +0 -1
- package/esm/collection.d.ts +0 -73
- package/esm/collection.js +0 -224
- package/esm/collection.js.map +0 -1
- package/esm/database.d.ts +0 -104
- package/esm/database.js +0 -305
- package/esm/database.js.map +0 -1
- package/esm/fields/array-field.d.ts +0 -11
- package/esm/fields/array-field.js +0 -26
- package/esm/fields/array-field.js.map +0 -1
- package/esm/fields/belongs-to-field.d.ts +0 -12
- package/esm/fields/belongs-to-field.js +0 -57
- package/esm/fields/belongs-to-field.js.map +0 -1
- package/esm/fields/belongs-to-many-field.d.ts +0 -11
- package/esm/fields/belongs-to-many-field.js +0 -55
- package/esm/fields/belongs-to-many-field.js.map +0 -1
- package/esm/fields/boolean-field.d.ts +0 -8
- package/esm/fields/boolean-field.js +0 -8
- package/esm/fields/boolean-field.js.map +0 -1
- package/esm/fields/context-field.d.ts +0 -13
- package/esm/fields/context-field.js +0 -43
- package/esm/fields/context-field.js.map +0 -1
- package/esm/fields/date-field.d.ts +0 -8
- package/esm/fields/date-field.js +0 -8
- package/esm/fields/date-field.js.map +0 -1
- package/esm/fields/field.d.ts +0 -37
- package/esm/fields/field.js +0 -74
- package/esm/fields/field.js.map +0 -1
- package/esm/fields/has-inverse-field.d.ts +0 -4
- package/esm/fields/has-inverse-field.js +0 -2
- package/esm/fields/has-inverse-field.js.map +0 -1
- package/esm/fields/has-many-field.d.ts +0 -64
- package/esm/fields/has-many-field.js +0 -58
- package/esm/fields/has-many-field.js.map +0 -1
- package/esm/fields/has-one-field.d.ts +0 -64
- package/esm/fields/has-one-field.js +0 -57
- package/esm/fields/has-one-field.js.map +0 -1
- package/esm/fields/index.d.ts +0 -42
- package/esm/fields/index.js +0 -22
- package/esm/fields/index.js.map +0 -1
- package/esm/fields/json-field.d.ts +0 -14
- package/esm/fields/json-field.js +0 -17
- package/esm/fields/json-field.js.map +0 -1
- package/esm/fields/number-field.d.ts +0 -32
- package/esm/fields/number-field.js +0 -28
- package/esm/fields/number-field.js.map +0 -1
- package/esm/fields/password-field.d.ts +0 -21
- package/esm/fields/password-field.js +0 -71
- package/esm/fields/password-field.js.map +0 -1
- package/esm/fields/radio-field.d.ts +0 -14
- package/esm/fields/radio-field.js +0 -49
- package/esm/fields/radio-field.js.map +0 -1
- package/esm/fields/relation-field.d.ts +0 -20
- package/esm/fields/relation-field.js +0 -27
- package/esm/fields/relation-field.js.map +0 -1
- package/esm/fields/sort-field.d.ts +0 -16
- package/esm/fields/sort-field.js +0 -90
- package/esm/fields/sort-field.js.map +0 -1
- package/esm/fields/string-field.d.ts +0 -8
- package/esm/fields/string-field.js +0 -8
- package/esm/fields/string-field.js.map +0 -1
- package/esm/fields/text-field.d.ts +0 -8
- package/esm/fields/text-field.js +0 -8
- package/esm/fields/text-field.js.map +0 -1
- package/esm/fields/time-field.d.ts +0 -8
- package/esm/fields/time-field.js +0 -8
- package/esm/fields/time-field.js.map +0 -1
- package/esm/fields/uid-field.d.ts +0 -10
- package/esm/fields/uid-field.js +0 -27
- package/esm/fields/uid-field.js.map +0 -1
- package/esm/fields/uuid-field.d.ts +0 -9
- package/esm/fields/uuid-field.js +0 -11
- package/esm/fields/uuid-field.js.map +0 -1
- package/esm/fields/virtual-field.d.ts +0 -8
- package/esm/fields/virtual-field.js +0 -8
- package/esm/fields/virtual-field.js.map +0 -1
- package/esm/filter-parser.d.ts +0 -27
- package/esm/filter-parser.js +0 -185
- package/esm/filter-parser.js.map +0 -1
- package/esm/index.d.ts +0 -15
- package/esm/index.js +0 -16
- package/esm/index.js.map +0 -1
- package/esm/magic-attribute-model.d.ts +0 -7
- package/esm/magic-attribute-model.js +0 -70
- package/esm/magic-attribute-model.js.map +0 -1
- package/esm/mock-database.d.ts +0 -22
- package/esm/mock-database.js +0 -34
- package/esm/mock-database.js.map +0 -1
- package/esm/model-hook.d.ts +0 -12
- package/esm/model-hook.js +0 -60
- package/esm/model-hook.js.map +0 -1
- package/esm/model.d.ts +0 -15
- package/esm/model.js +0 -80
- package/esm/model.js.map +0 -1
- package/esm/operators/array.d.ts +0 -26
- package/esm/operators/array.js +0 -105
- package/esm/operators/array.js.map +0 -1
- package/esm/operators/association.d.ts +0 -10
- package/esm/operators/association.js +0 -14
- package/esm/operators/association.js.map +0 -1
- package/esm/operators/date.d.ts +0 -34
- package/esm/operators/date.js +0 -35
- package/esm/operators/date.js.map +0 -1
- package/esm/operators/empty.d.ts +0 -28
- package/esm/operators/empty.js +0 -58
- package/esm/operators/empty.js.map +0 -1
- package/esm/operators/index.d.ts +0 -2
- package/esm/operators/index.js +0 -2
- package/esm/operators/index.js.map +0 -1
- package/esm/operators/ne.d.ts +0 -10
- package/esm/operators/ne.js +0 -12
- package/esm/operators/ne.js.map +0 -1
- package/esm/operators/notIn.d.ts +0 -10
- package/esm/operators/notIn.js +0 -12
- package/esm/operators/notIn.js.map +0 -1
- package/esm/operators/string.d.ts +0 -21
- package/esm/operators/string.js +0 -35
- package/esm/operators/string.js.map +0 -1
- package/esm/operators/utils.d.ts +0 -4
- package/esm/operators/utils.js +0 -11
- package/esm/operators/utils.js.map +0 -1
- package/esm/options-parser.d.ts +0 -31
- package/esm/options-parser.js +0 -225
- package/esm/options-parser.js.map +0 -1
- package/esm/playground.d.ts +0 -1
- package/esm/playground.js +0 -53
- package/esm/playground.js.map +0 -1
- package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
- package/esm/relation-repository/belongs-to-many-repository.js +0 -199
- package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
- package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
- package/esm/relation-repository/belongs-to-repository.js +0 -4
- package/esm/relation-repository/belongs-to-repository.js.map +0 -1
- package/esm/relation-repository/hasmany-repository.d.ts +0 -23
- package/esm/relation-repository/hasmany-repository.js +0 -125
- package/esm/relation-repository/hasmany-repository.js.map +0 -1
- package/esm/relation-repository/hasone-repository.d.ts +0 -17
- package/esm/relation-repository/hasone-repository.js +0 -4
- package/esm/relation-repository/hasone-repository.js.map +0 -1
- package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
- package/esm/relation-repository/multiple-relation-repository.js +0 -149
- package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
- package/esm/relation-repository/relation-repository.d.ts +0 -32
- package/esm/relation-repository/relation-repository.js +0 -95
- package/esm/relation-repository/relation-repository.js.map +0 -1
- package/esm/relation-repository/single-relation-repository.d.ts +0 -23
- package/esm/relation-repository/single-relation-repository.js +0 -96
- package/esm/relation-repository/single-relation-repository.js.map +0 -1
- package/esm/relation-repository/types.d.ts +0 -7
- package/esm/relation-repository/types.js +0 -2
- package/esm/relation-repository/types.js.map +0 -1
- package/esm/repository.d.ts +0 -165
- package/esm/repository.js +0 -276
- package/esm/repository.js.map +0 -1
- package/esm/transaction-decorator.d.ts +0 -1
- package/esm/transaction-decorator.js +0 -63
- package/esm/transaction-decorator.js.map +0 -1
- package/esm/update-associations.d.ts +0 -60
- package/esm/update-associations.js +0 -362
- package/esm/update-associations.js.map +0 -1
- package/esm/update-guard.d.ts +0 -26
- package/esm/update-guard.js +0 -122
- package/esm/update-guard.js.map +0 -1
- package/lib/collection-importer.js.map +0 -1
- package/lib/collection.js.map +0 -1
- package/lib/database.js.map +0 -1
- package/lib/fields/array-field.js.map +0 -1
- package/lib/fields/belongs-to-field.js.map +0 -1
- package/lib/fields/belongs-to-many-field.js.map +0 -1
- package/lib/fields/boolean-field.js.map +0 -1
- package/lib/fields/context-field.js.map +0 -1
- package/lib/fields/date-field.js.map +0 -1
- package/lib/fields/field.js.map +0 -1
- package/lib/fields/has-inverse-field.js.map +0 -1
- package/lib/fields/has-many-field.js.map +0 -1
- package/lib/fields/has-one-field.js.map +0 -1
- package/lib/fields/index.js.map +0 -1
- package/lib/fields/json-field.js.map +0 -1
- package/lib/fields/number-field.js.map +0 -1
- package/lib/fields/password-field.js.map +0 -1
- package/lib/fields/radio-field.js.map +0 -1
- package/lib/fields/relation-field.js.map +0 -1
- package/lib/fields/sort-field.js.map +0 -1
- package/lib/fields/string-field.js.map +0 -1
- package/lib/fields/text-field.js.map +0 -1
- package/lib/fields/time-field.js.map +0 -1
- package/lib/fields/uid-field.js.map +0 -1
- package/lib/fields/uuid-field.js.map +0 -1
- package/lib/fields/virtual-field.js.map +0 -1
- package/lib/filter-parser.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/magic-attribute-model.js.map +0 -1
- package/lib/mock-database.js.map +0 -1
- package/lib/model-hook.js.map +0 -1
- package/lib/model.js.map +0 -1
- package/lib/operators/array.js.map +0 -1
- package/lib/operators/association.js.map +0 -1
- package/lib/operators/date.js.map +0 -1
- package/lib/operators/empty.js.map +0 -1
- package/lib/operators/index.js.map +0 -1
- package/lib/operators/ne.js.map +0 -1
- package/lib/operators/notIn.js.map +0 -1
- package/lib/operators/string.js.map +0 -1
- package/lib/operators/utils.js.map +0 -1
- package/lib/options-parser.js.map +0 -1
- package/lib/playground.js.map +0 -1
- package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
- package/lib/relation-repository/belongs-to-repository.js.map +0 -1
- package/lib/relation-repository/hasmany-repository.js.map +0 -1
- package/lib/relation-repository/hasone-repository.js.map +0 -1
- package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
- package/lib/relation-repository/relation-repository.js.map +0 -1
- package/lib/relation-repository/single-relation-repository.js.map +0 -1
- package/lib/relation-repository/types.js.map +0 -1
- package/lib/repository.js.map +0 -1
- package/lib/transaction-decorator.js.map +0 -1
- package/lib/update-associations.js.map +0 -1
- package/lib/update-guard.js.map +0 -1
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
package/esm/collection.js
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
|
-
import merge from 'deepmerge';
|
|
22
|
-
import { EventEmitter } from 'events';
|
|
23
|
-
import { default as lodash, default as _ } from 'lodash';
|
|
24
|
-
import { Model } from './model';
|
|
25
|
-
import { Repository } from './repository';
|
|
26
|
-
export class Collection extends EventEmitter {
|
|
27
|
-
constructor(options, context) {
|
|
28
|
-
super();
|
|
29
|
-
this.fields = new Map();
|
|
30
|
-
this.context = context;
|
|
31
|
-
this.options = options;
|
|
32
|
-
this.bindFieldEventListener();
|
|
33
|
-
this.modelInit();
|
|
34
|
-
this.setFields(options.fields);
|
|
35
|
-
this.setRepository(options.repository);
|
|
36
|
-
this.setSortable(options.sortable);
|
|
37
|
-
}
|
|
38
|
-
get filterTargetKey() {
|
|
39
|
-
return lodash.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
|
|
40
|
-
}
|
|
41
|
-
get name() {
|
|
42
|
-
return this.options.name;
|
|
43
|
-
}
|
|
44
|
-
sequelizeModelOptions() {
|
|
45
|
-
const { name, tableName } = this.options;
|
|
46
|
-
return Object.assign(Object.assign({}, _.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), { modelName: name, sequelize: this.context.database.sequelize, tableName: tableName || name });
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* TODO
|
|
50
|
-
*/
|
|
51
|
-
modelInit() {
|
|
52
|
-
if (this.model) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const { name, model, autoGenId = true } = this.options;
|
|
56
|
-
let M = Model;
|
|
57
|
-
if (this.context.database.sequelize.isDefined(name)) {
|
|
58
|
-
const m = this.context.database.sequelize.model(name);
|
|
59
|
-
if (m.isThrough) {
|
|
60
|
-
// @ts-ignore
|
|
61
|
-
this.model = m;
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
this.model.database = this.context.database;
|
|
64
|
-
// @ts-ignore
|
|
65
|
-
this.model.collection = this;
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (typeof model === 'string') {
|
|
70
|
-
M = this.context.database.models.get(model) || Model;
|
|
71
|
-
}
|
|
72
|
-
else if (model) {
|
|
73
|
-
M = model;
|
|
74
|
-
}
|
|
75
|
-
// @ts-ignore
|
|
76
|
-
this.model = class extends M {
|
|
77
|
-
};
|
|
78
|
-
this.model.init(null, this.sequelizeModelOptions());
|
|
79
|
-
if (!autoGenId) {
|
|
80
|
-
this.model.removeAttribute('id');
|
|
81
|
-
}
|
|
82
|
-
// @ts-ignore
|
|
83
|
-
this.model.database = this.context.database;
|
|
84
|
-
// @ts-ignore
|
|
85
|
-
this.model.collection = this;
|
|
86
|
-
}
|
|
87
|
-
setRepository(repository) {
|
|
88
|
-
let repo = Repository;
|
|
89
|
-
if (typeof repository === 'string') {
|
|
90
|
-
repo = this.context.database.repositories.get(repository) || Repository;
|
|
91
|
-
}
|
|
92
|
-
this.repository = new repo(this);
|
|
93
|
-
}
|
|
94
|
-
bindFieldEventListener() {
|
|
95
|
-
this.on('field.afterAdd', (field) => {
|
|
96
|
-
field.bind();
|
|
97
|
-
});
|
|
98
|
-
this.on('field.afterRemove', (field) => field.unbind());
|
|
99
|
-
}
|
|
100
|
-
forEachField(callback) {
|
|
101
|
-
return [...this.fields.values()].forEach(callback);
|
|
102
|
-
}
|
|
103
|
-
findField(callback) {
|
|
104
|
-
return [...this.fields.values()].find(callback);
|
|
105
|
-
}
|
|
106
|
-
hasField(name) {
|
|
107
|
-
return this.fields.has(name);
|
|
108
|
-
}
|
|
109
|
-
getField(name) {
|
|
110
|
-
return this.fields.get(name);
|
|
111
|
-
}
|
|
112
|
-
addField(name, options) {
|
|
113
|
-
return this.setField(name, options);
|
|
114
|
-
}
|
|
115
|
-
setField(name, options) {
|
|
116
|
-
const { database } = this.context;
|
|
117
|
-
const field = database.buildField(Object.assign({ name }, options), Object.assign(Object.assign({}, this.context), { collection: this }));
|
|
118
|
-
this.removeField(name);
|
|
119
|
-
this.fields.set(name, field);
|
|
120
|
-
this.emit('field.afterAdd', field);
|
|
121
|
-
return field;
|
|
122
|
-
}
|
|
123
|
-
setFields(fields, resetFields = true) {
|
|
124
|
-
if (!Array.isArray(fields)) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
if (resetFields) {
|
|
128
|
-
this.resetFields();
|
|
129
|
-
}
|
|
130
|
-
for (let _a of fields) {
|
|
131
|
-
const { name } = _a, options = __rest(_a, ["name"]);
|
|
132
|
-
this.addField(name, options);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
resetFields() {
|
|
136
|
-
const fieldNames = this.fields.keys();
|
|
137
|
-
for (const fieldName of fieldNames) {
|
|
138
|
-
this.removeField(fieldName);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
removeField(name) {
|
|
142
|
-
if (!this.fields.has(name)) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
const field = this.fields.get(name);
|
|
146
|
-
const bool = this.fields.delete(name);
|
|
147
|
-
if (bool) {
|
|
148
|
-
this.emit('field.afterRemove', field);
|
|
149
|
-
}
|
|
150
|
-
return bool;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* TODO
|
|
154
|
-
*/
|
|
155
|
-
updateOptions(options, mergeOptions) {
|
|
156
|
-
let newOptions = lodash.cloneDeep(options);
|
|
157
|
-
newOptions = merge(this.options, newOptions, mergeOptions);
|
|
158
|
-
this.context.database.emit('beforeUpdateCollection', this, newOptions);
|
|
159
|
-
this.setFields(options.fields, false);
|
|
160
|
-
this.setRepository(options.repository);
|
|
161
|
-
this.context.database.emit('afterUpdateCollection', this);
|
|
162
|
-
return this;
|
|
163
|
-
}
|
|
164
|
-
setSortable(sortable) {
|
|
165
|
-
if (!sortable) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (sortable === true) {
|
|
169
|
-
this.setField('sort', {
|
|
170
|
-
type: 'sort',
|
|
171
|
-
hidden: true,
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
if (typeof sortable === 'string') {
|
|
175
|
-
this.setField(sortable, {
|
|
176
|
-
type: 'sort',
|
|
177
|
-
hidden: true,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
else if (typeof sortable === 'object') {
|
|
181
|
-
const { name } = sortable, opts = __rest(sortable, ["name"]);
|
|
182
|
-
this.setField(name || 'sort', Object.assign({ type: 'sort', hidden: true }, opts));
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* TODO
|
|
187
|
-
*
|
|
188
|
-
* @param name
|
|
189
|
-
* @param options
|
|
190
|
-
*/
|
|
191
|
-
updateField(name, options) {
|
|
192
|
-
if (!this.hasField(name)) {
|
|
193
|
-
throw new Error(`field ${name} not exists`);
|
|
194
|
-
}
|
|
195
|
-
if (options.name && options.name !== name) {
|
|
196
|
-
this.removeField(name);
|
|
197
|
-
}
|
|
198
|
-
this.setField(options.name || name, options);
|
|
199
|
-
}
|
|
200
|
-
sync(syncOptions) {
|
|
201
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
202
|
-
const modelNames = [this.model.name];
|
|
203
|
-
const associations = this.model.associations;
|
|
204
|
-
for (const associationKey in associations) {
|
|
205
|
-
const association = associations[associationKey];
|
|
206
|
-
modelNames.push(association.target.name);
|
|
207
|
-
if (association.through) {
|
|
208
|
-
modelNames.push(association.through.model.name);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
const models = [];
|
|
212
|
-
// @ts-ignore
|
|
213
|
-
this.context.database.sequelize.modelManager.forEachModel((model) => {
|
|
214
|
-
if (modelNames.includes(model.name)) {
|
|
215
|
-
models.push(model);
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
for (const model of models) {
|
|
219
|
-
yield model.sync(syncOptions);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
//# sourceMappingURL=collection.js.map
|
package/esm/collection.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"collection.js","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,WAAW,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,OAAO,IAAI,CAAC,EAAE,MAAM,QAAQ,CAAC;AAIzD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;A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ACH,CAAC,CAAC,CAAC;YAEH,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;gBAC1B,MAAM,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;aAC/B;QACH,CAAC;KAAA;CACF","sourcesContent":["import merge from 'deepmerge';\nimport { EventEmitter } from 'events';\nimport { default as lodash, default as _ } from 'lodash';\nimport { col, ModelCtor, ModelOptions, SyncOptions } from 'sequelize';\nimport { Database } from './database';\nimport { Field, FieldOptions } from './fields';\nimport { Model } from './model';\nimport { Repository } from './repository';\n\nexport type RepositoryType = typeof Repository;\n\nexport type CollectionSortable = string | boolean | { name?: string; scopeKey?: string };\n\nexport interface CollectionOptions extends Omit<ModelOptions, 'name' | 'hooks'> {\n name: string;\n tableName?: string;\n filterTargetKey?: string;\n fields?: FieldOptions[];\n model?: string | ModelCtor<Model>;\n repository?: string | RepositoryType;\n sortable?: CollectionSortable;\n /**\n * @default true\n */\n autoGenId?: boolean;\n /**\n * @default 'options'\n */\n magicAttribute?: string;\n [key: string]: any;\n}\n\nexport interface CollectionContext {\n database: Database;\n}\n\nexport class Collection<\n TModelAttributes extends {} = any,\n TCreationAttributes extends {} = TModelAttributes,\n> extends EventEmitter {\n options: CollectionOptions;\n context: CollectionContext;\n isThrough?: boolean;\n fields: Map<string, any> = new Map<string, any>();\n model: ModelCtor<Model>;\n repository: Repository<TModelAttributes, TCreationAttributes>;\n\n get filterTargetKey() {\n return lodash.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);\n }\n\n get name() {\n return this.options.name;\n }\n\n constructor(options: CollectionOptions, context?: CollectionContext) {\n super();\n this.context = context;\n this.options = options;\n this.bindFieldEventListener();\n this.modelInit();\n this.setFields(options.fields);\n this.setRepository(options.repository);\n this.setSortable(options.sortable);\n }\n\n private sequelizeModelOptions() {\n const { name, tableName } = this.options;\n return {\n ..._.omit(this.options, ['name', 'fields', 'model', 'targetKey']),\n modelName: name,\n sequelize: this.context.database.sequelize,\n tableName: tableName || name,\n };\n }\n\n /**\n * TODO\n */\n modelInit() {\n if (this.model) {\n return;\n }\n const { name, model, autoGenId = true } = this.options;\n let M: ModelCtor<Model> = Model;\n if (this.context.database.sequelize.isDefined(name)) {\n const m = this.context.database.sequelize.model(name);\n if ((m as any).isThrough) {\n // @ts-ignore\n this.model = m;\n // @ts-ignore\n this.model.database = this.context.database;\n // @ts-ignore\n this.model.collection = this;\n return;\n }\n }\n if (typeof model === 'string') {\n M = this.context.database.models.get(model) || Model;\n } else if (model) {\n M = model;\n }\n // @ts-ignore\n this.model = class extends M {};\n this.model.init(null, this.sequelizeModelOptions());\n\n if (!autoGenId) {\n this.model.removeAttribute('id');\n }\n\n // @ts-ignore\n this.model.database = this.context.database;\n // @ts-ignore\n this.model.collection = this;\n }\n\n setRepository(repository?: RepositoryType | string) {\n let repo = Repository;\n if (typeof repository === 'string') {\n repo = this.context.database.repositories.get(repository) || Repository;\n }\n this.repository = new repo(this);\n }\n\n private bindFieldEventListener() {\n this.on('field.afterAdd', (field: Field) => {\n field.bind();\n });\n this.on('field.afterRemove', (field) => field.unbind());\n }\n\n forEachField(callback: (field: Field) => void) {\n return [...this.fields.values()].forEach(callback);\n }\n\n findField(callback: (field: Field) => boolean) {\n return [...this.fields.values()].find(callback);\n }\n\n hasField(name: string) {\n return this.fields.has(name);\n }\n\n getField<F extends Field>(name: string): F {\n return this.fields.get(name);\n }\n\n addField(name: string, options: FieldOptions): Field {\n return this.setField(name, options);\n }\n\n setField(name: string, options: FieldOptions): Field {\n const { database } = this.context;\n\n const field = database.buildField(\n { name, ...options },\n {\n ...this.context,\n collection: this,\n },\n );\n this.removeField(name);\n this.fields.set(name, field);\n this.emit('field.afterAdd', field);\n return field;\n }\n\n setFields(fields: FieldOptions[], resetFields = true) {\n if (!Array.isArray(fields)) {\n return;\n }\n\n if (resetFields) {\n this.resetFields();\n }\n\n for (const { name, ...options } of fields) {\n this.addField(name, options);\n }\n }\n\n resetFields() {\n const fieldNames = this.fields.keys();\n for (const fieldName of fieldNames) {\n this.removeField(fieldName);\n }\n }\n\n removeField(name) {\n if (!this.fields.has(name)) {\n return;\n }\n const field = this.fields.get(name);\n const bool = this.fields.delete(name);\n if (bool) {\n this.emit('field.afterRemove', field);\n }\n return bool;\n }\n\n /**\n * TODO\n */\n updateOptions(options: CollectionOptions, mergeOptions?: any) {\n let newOptions = lodash.cloneDeep(options);\n newOptions = merge(this.options, newOptions, mergeOptions);\n\n this.context.database.emit('beforeUpdateCollection', this, newOptions);\n\n this.setFields(options.fields, false);\n this.setRepository(options.repository);\n\n this.context.database.emit('afterUpdateCollection', this);\n\n return this;\n }\n\n setSortable(sortable) {\n if (!sortable) {\n return;\n }\n if (sortable === true) {\n this.setField('sort', {\n type: 'sort',\n hidden: true,\n });\n }\n if (typeof sortable === 'string') {\n this.setField(sortable, {\n type: 'sort',\n hidden: true,\n });\n } else if (typeof sortable === 'object') {\n const { name, ...opts } = sortable;\n this.setField(name || 'sort', { type: 'sort', hidden: true, ...opts });\n }\n }\n\n /**\n * TODO\n *\n * @param name\n * @param options\n */\n updateField(name: string, options: FieldOptions) {\n if (!this.hasField(name)) {\n throw new Error(`field ${name} not exists`);\n }\n\n if (options.name && options.name !== name) {\n this.removeField(name);\n }\n\n this.setField(options.name || name, options);\n }\n\n async sync(syncOptions?: SyncOptions) {\n const modelNames = [this.model.name];\n\n const associations = this.model.associations;\n\n for (const associationKey in associations) {\n const association = associations[associationKey];\n modelNames.push(association.target.name);\n if ((<any>association).through) {\n modelNames.push((<any>association).through.model.name);\n }\n }\n\n const models: ModelCtor<Model>[] = [];\n // @ts-ignore\n this.context.database.sequelize.modelManager.forEachModel((model) => {\n if (modelNames.includes(model.name)) {\n models.push(model);\n }\n });\n\n for (const model of models) {\n await model.sync(syncOptions);\n }\n }\n}\n"]}
|
package/esm/database.d.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { AsyncEmitter } from '@nocobase/utils';
|
|
3
|
-
import merge from 'deepmerge';
|
|
4
|
-
import { EventEmitter } from 'events';
|
|
5
|
-
import { ModelCtor, Options, QueryInterfaceDropAllTablesOptions, QueryOptions, Sequelize, SyncOptions } from 'sequelize';
|
|
6
|
-
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
|
7
|
-
import { ImportFileExtension } from './collection-importer';
|
|
8
|
-
import * as FieldTypes from './fields';
|
|
9
|
-
import { Field, FieldContext, RelationField } from './fields';
|
|
10
|
-
import { Model } from './model';
|
|
11
|
-
import { ModelHook } from './model-hook';
|
|
12
|
-
import { RelationRepository } from './relation-repository/relation-repository';
|
|
13
|
-
import { Repository } from './repository';
|
|
14
|
-
export interface MergeOptions extends merge.Options {
|
|
15
|
-
}
|
|
16
|
-
export interface PendingOptions {
|
|
17
|
-
field: RelationField;
|
|
18
|
-
model: ModelCtor<Model>;
|
|
19
|
-
}
|
|
20
|
-
interface MapOf<T> {
|
|
21
|
-
[key: string]: T;
|
|
22
|
-
}
|
|
23
|
-
export interface IDatabaseOptions extends Options {
|
|
24
|
-
tablePrefix?: string;
|
|
25
|
-
}
|
|
26
|
-
export declare type DatabaseOptions = IDatabaseOptions | Sequelize;
|
|
27
|
-
interface RegisterOperatorsContext {
|
|
28
|
-
db?: Database;
|
|
29
|
-
path?: string;
|
|
30
|
-
field?: Field;
|
|
31
|
-
app?: any;
|
|
32
|
-
}
|
|
33
|
-
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
|
|
34
|
-
drop?: boolean;
|
|
35
|
-
}
|
|
36
|
-
declare type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
|
|
37
|
-
export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
38
|
-
sequelize: Sequelize;
|
|
39
|
-
fieldTypes: Map<any, any>;
|
|
40
|
-
options: IDatabaseOptions;
|
|
41
|
-
models: Map<string, ModelCtor<Model<any, any>>>;
|
|
42
|
-
repositories: Map<string, typeof Repository>;
|
|
43
|
-
operators: Map<any, any>;
|
|
44
|
-
collections: Map<string, Collection<any, any>>;
|
|
45
|
-
pendingFields: Map<string, FieldTypes.RelationField[]>;
|
|
46
|
-
modelCollection: Map<ModelCtor<any>, Collection<any, any>>;
|
|
47
|
-
modelHook: ModelHook;
|
|
48
|
-
delayCollectionExtend: Map<string, {
|
|
49
|
-
collectionOptions: CollectionOptions;
|
|
50
|
-
mergeOptions?: any;
|
|
51
|
-
}[]>;
|
|
52
|
-
constructor(options: DatabaseOptions);
|
|
53
|
-
/**
|
|
54
|
-
* Add collection to database
|
|
55
|
-
* @param options
|
|
56
|
-
*/
|
|
57
|
-
collection<Attributes = any, CreateAttributes = Attributes>(options: CollectionOptions): Collection<Attributes, CreateAttributes>;
|
|
58
|
-
getTablePrefix(): string;
|
|
59
|
-
/**
|
|
60
|
-
* get exists collection by its name
|
|
61
|
-
* @param name
|
|
62
|
-
*/
|
|
63
|
-
getCollection(name: string): Collection;
|
|
64
|
-
hasCollection(name: string): boolean;
|
|
65
|
-
removeCollection(name: string): void;
|
|
66
|
-
getModel<M extends Model>(name: string): ModelCtor<M>;
|
|
67
|
-
getRepository<R extends Repository>(name: string): R;
|
|
68
|
-
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
|
|
69
|
-
addPendingField(field: RelationField): void;
|
|
70
|
-
removePendingField(field: RelationField): void;
|
|
71
|
-
registerFieldTypes(fieldTypes: MapOf<typeof Field>): void;
|
|
72
|
-
registerModels(models: MapOf<ModelCtor<any>>): void;
|
|
73
|
-
registerRepositories(repositories: MapOf<RepositoryType>): void;
|
|
74
|
-
initOperators(): void;
|
|
75
|
-
registerOperators(operators: MapOf<OperatorFunc>): void;
|
|
76
|
-
buildField(options: any, context: FieldContext): any;
|
|
77
|
-
sync(options?: SyncOptions): Promise<Sequelize>;
|
|
78
|
-
clean(options: CleanOptions): Promise<void>;
|
|
79
|
-
isSqliteMemory(): boolean;
|
|
80
|
-
auth(options?: QueryOptions & {
|
|
81
|
-
repeat?: number;
|
|
82
|
-
}): Promise<any>;
|
|
83
|
-
reconnect(): Promise<void>;
|
|
84
|
-
closed(): any;
|
|
85
|
-
close(): Promise<void>;
|
|
86
|
-
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
87
|
-
import(options: {
|
|
88
|
-
directory: string;
|
|
89
|
-
extensions?: ImportFileExtension[];
|
|
90
|
-
}): Promise<Map<string, Collection>>;
|
|
91
|
-
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
92
|
-
}
|
|
93
|
-
export declare function extend(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions): {
|
|
94
|
-
collectionOptions: CollectionOptions;
|
|
95
|
-
mergeOptions: MergeOptions;
|
|
96
|
-
extend: boolean;
|
|
97
|
-
};
|
|
98
|
-
export declare const defineCollection: (collectionOptions: CollectionOptions) => CollectionOptions;
|
|
99
|
-
export declare const extendCollection: (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
|
|
100
|
-
collectionOptions: CollectionOptions;
|
|
101
|
-
mergeOptions: MergeOptions;
|
|
102
|
-
extend: boolean;
|
|
103
|
-
};
|
|
104
|
-
export default Database;
|
package/esm/database.js
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
|
-
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
|
22
|
-
import { EventEmitter } from 'events';
|
|
23
|
-
import lodash from 'lodash';
|
|
24
|
-
import { Op, Sequelize, Utils } from 'sequelize';
|
|
25
|
-
import { Collection } from './collection';
|
|
26
|
-
import { ImporterReader } from './collection-importer';
|
|
27
|
-
import * as FieldTypes from './fields';
|
|
28
|
-
import { ModelHook } from './model-hook';
|
|
29
|
-
import extendOperators from './operators';
|
|
30
|
-
export class Database extends EventEmitter {
|
|
31
|
-
constructor(options) {
|
|
32
|
-
super();
|
|
33
|
-
this.fieldTypes = new Map();
|
|
34
|
-
this.models = new Map();
|
|
35
|
-
this.repositories = new Map();
|
|
36
|
-
this.operators = new Map();
|
|
37
|
-
this.collections = new Map();
|
|
38
|
-
this.pendingFields = new Map();
|
|
39
|
-
this.modelCollection = new Map();
|
|
40
|
-
this.delayCollectionExtend = new Map();
|
|
41
|
-
if (options instanceof Sequelize) {
|
|
42
|
-
this.sequelize = options;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
const opts = Object.assign({ sync: {
|
|
46
|
-
alter: {
|
|
47
|
-
drop: false,
|
|
48
|
-
},
|
|
49
|
-
force: false,
|
|
50
|
-
} }, options);
|
|
51
|
-
this.sequelize = new Sequelize(opts);
|
|
52
|
-
this.options = opts;
|
|
53
|
-
}
|
|
54
|
-
this.collections = new Map();
|
|
55
|
-
this.modelHook = new ModelHook(this);
|
|
56
|
-
this.on('afterDefineCollection', (collection) => {
|
|
57
|
-
var _a, _b;
|
|
58
|
-
// after collection defined, call bind method on pending fields
|
|
59
|
-
(_a = this.pendingFields.get(collection.name)) === null || _a === void 0 ? void 0 : _a.forEach((field) => field.bind());
|
|
60
|
-
(_b = this.delayCollectionExtend.get(collection.name)) === null || _b === void 0 ? void 0 : _b.forEach((collectionExtend) => {
|
|
61
|
-
collection.updateOptions(collectionExtend.collectionOptions, collectionExtend.mergeOptions);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
// register database field types
|
|
65
|
-
for (const [name, field] of Object.entries(FieldTypes)) {
|
|
66
|
-
if (['Field', 'RelationField'].includes(name)) {
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
let key = name.replace(/Field$/g, '');
|
|
70
|
-
key = key.substring(0, 1).toLowerCase() + key.substring(1);
|
|
71
|
-
this.registerFieldTypes({
|
|
72
|
-
[key]: field,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
this.initOperators();
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Add collection to database
|
|
79
|
-
* @param options
|
|
80
|
-
*/
|
|
81
|
-
collection(options) {
|
|
82
|
-
this.emit('beforeDefineCollection', options);
|
|
83
|
-
const collection = new Collection(options, {
|
|
84
|
-
database: this,
|
|
85
|
-
});
|
|
86
|
-
this.collections.set(collection.name, collection);
|
|
87
|
-
this.modelCollection.set(collection.model, collection);
|
|
88
|
-
this.emit('afterDefineCollection', collection);
|
|
89
|
-
return collection;
|
|
90
|
-
}
|
|
91
|
-
getTablePrefix() {
|
|
92
|
-
return this.options.tablePrefix || '';
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* get exists collection by its name
|
|
96
|
-
* @param name
|
|
97
|
-
*/
|
|
98
|
-
getCollection(name) {
|
|
99
|
-
return this.collections.get(name);
|
|
100
|
-
}
|
|
101
|
-
hasCollection(name) {
|
|
102
|
-
return this.collections.has(name);
|
|
103
|
-
}
|
|
104
|
-
removeCollection(name) {
|
|
105
|
-
const collection = this.collections.get(name);
|
|
106
|
-
this.emit('beforeRemoveCollection', collection);
|
|
107
|
-
const result = this.collections.delete(name);
|
|
108
|
-
if (result) {
|
|
109
|
-
this.emit('afterRemoveCollection', collection);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
getModel(name) {
|
|
113
|
-
return this.getCollection(name).model;
|
|
114
|
-
}
|
|
115
|
-
getRepository(name, relationId) {
|
|
116
|
-
var _a, _b, _c;
|
|
117
|
-
if (relationId) {
|
|
118
|
-
const [collection, relation] = name.split('.');
|
|
119
|
-
return (_b = (_a = this.getRepository(collection)) === null || _a === void 0 ? void 0 : _a.relation(relation)) === null || _b === void 0 ? void 0 : _b.of(relationId);
|
|
120
|
-
}
|
|
121
|
-
return (_c = this.getCollection(name)) === null || _c === void 0 ? void 0 : _c.repository;
|
|
122
|
-
}
|
|
123
|
-
addPendingField(field) {
|
|
124
|
-
const associating = this.pendingFields;
|
|
125
|
-
const items = this.pendingFields.get(field.target) || [];
|
|
126
|
-
items.push(field);
|
|
127
|
-
associating.set(field.target, items);
|
|
128
|
-
}
|
|
129
|
-
removePendingField(field) {
|
|
130
|
-
const items = this.pendingFields.get(field.target) || [];
|
|
131
|
-
const index = items.indexOf(field);
|
|
132
|
-
if (index !== -1) {
|
|
133
|
-
delete items[index];
|
|
134
|
-
this.pendingFields.set(field.target, items);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
registerFieldTypes(fieldTypes) {
|
|
138
|
-
for (const [type, fieldType] of Object.entries(fieldTypes)) {
|
|
139
|
-
this.fieldTypes.set(type, fieldType);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
registerModels(models) {
|
|
143
|
-
for (const [type, schemaType] of Object.entries(models)) {
|
|
144
|
-
this.models.set(type, schemaType);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
registerRepositories(repositories) {
|
|
148
|
-
for (const [type, schemaType] of Object.entries(repositories)) {
|
|
149
|
-
this.repositories.set(type, schemaType);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
initOperators() {
|
|
153
|
-
const operators = new Map();
|
|
154
|
-
// Sequelize 内置
|
|
155
|
-
for (const key in Op) {
|
|
156
|
-
operators.set('$' + key, Op[key]);
|
|
157
|
-
const val = Utils.underscoredIf(key, true);
|
|
158
|
-
operators.set('$' + val, Op[key]);
|
|
159
|
-
operators.set('$' + val.replace(/_/g, ''), Op[key]);
|
|
160
|
-
}
|
|
161
|
-
this.operators = operators;
|
|
162
|
-
this.registerOperators(Object.assign({}, extendOperators));
|
|
163
|
-
}
|
|
164
|
-
registerOperators(operators) {
|
|
165
|
-
for (const [key, operator] of Object.entries(operators)) {
|
|
166
|
-
this.operators.set(key, operator);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
buildField(options, context) {
|
|
170
|
-
const { type } = options;
|
|
171
|
-
const Field = this.fieldTypes.get(type);
|
|
172
|
-
if (!Field) {
|
|
173
|
-
throw Error(`unsupported field type ${type}`);
|
|
174
|
-
}
|
|
175
|
-
return new Field(options, context);
|
|
176
|
-
}
|
|
177
|
-
sync(options) {
|
|
178
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
-
const isMySQL = this.sequelize.getDialect() === 'mysql';
|
|
180
|
-
if (isMySQL) {
|
|
181
|
-
yield this.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', null);
|
|
182
|
-
}
|
|
183
|
-
const result = yield this.sequelize.sync(options);
|
|
184
|
-
if (isMySQL) {
|
|
185
|
-
yield this.sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null);
|
|
186
|
-
}
|
|
187
|
-
return result;
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
clean(options) {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
const { drop } = options, others = __rest(options, ["drop"]);
|
|
193
|
-
if (drop) {
|
|
194
|
-
yield this.sequelize.getQueryInterface().dropAllTables(others);
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
isSqliteMemory() {
|
|
199
|
-
return this.sequelize.getDialect() === 'sqlite' && lodash.get(this.options, 'storage') == ':memory:';
|
|
200
|
-
}
|
|
201
|
-
auth(options = {}) {
|
|
202
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
-
const { repeat = 10 } = options, others = __rest(options, ["repeat"]);
|
|
204
|
-
const delay = (ms) => new Promise((yea) => setTimeout(yea, ms));
|
|
205
|
-
let count = 0;
|
|
206
|
-
const authenticate = () => __awaiter(this, void 0, void 0, function* () {
|
|
207
|
-
try {
|
|
208
|
-
yield this.sequelize.authenticate(others);
|
|
209
|
-
console.log('Connection has been established successfully.');
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
catch (error) {
|
|
213
|
-
console.log('reconnecting...', count);
|
|
214
|
-
if (count >= repeat) {
|
|
215
|
-
throw error;
|
|
216
|
-
}
|
|
217
|
-
++count;
|
|
218
|
-
yield delay(500);
|
|
219
|
-
return yield authenticate();
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
return yield authenticate();
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
reconnect() {
|
|
226
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
227
|
-
if (this.isSqliteMemory()) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
// @ts-ignore
|
|
231
|
-
const ConnectionManager = this.sequelize.dialect.connectionManager.constructor;
|
|
232
|
-
// @ts-ignore
|
|
233
|
-
const connectionManager = new ConnectionManager(this.sequelize.dialect, this.sequelize);
|
|
234
|
-
// @ts-ignore
|
|
235
|
-
this.sequelize.dialect.connectionManager = connectionManager;
|
|
236
|
-
// @ts-ignore
|
|
237
|
-
this.sequelize.connectionManager = connectionManager;
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
closed() {
|
|
241
|
-
// @ts-ignore
|
|
242
|
-
return this.sequelize.connectionManager.pool._draining;
|
|
243
|
-
}
|
|
244
|
-
close() {
|
|
245
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
-
if (this.isSqliteMemory()) {
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
return this.sequelize.close();
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
on(event, listener) {
|
|
253
|
-
const modelEventName = this.modelHook.isModelHook(event);
|
|
254
|
-
if (modelEventName && !this.modelHook.hasBindEvent(modelEventName)) {
|
|
255
|
-
this.sequelize.addHook(modelEventName, this.modelHook.sequelizeHookBuilder(modelEventName));
|
|
256
|
-
this.modelHook.bindEvent(modelEventName);
|
|
257
|
-
}
|
|
258
|
-
return super.on(event, listener);
|
|
259
|
-
}
|
|
260
|
-
import(options) {
|
|
261
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
-
const reader = new ImporterReader(options.directory, options.extensions);
|
|
263
|
-
const modules = yield reader.read();
|
|
264
|
-
const result = new Map();
|
|
265
|
-
for (const module of modules) {
|
|
266
|
-
if (module.extend) {
|
|
267
|
-
const collectionName = module.collectionOptions.name;
|
|
268
|
-
const existCollection = this.getCollection(collectionName);
|
|
269
|
-
if (existCollection) {
|
|
270
|
-
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
274
|
-
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
const collection = this.collection(module);
|
|
279
|
-
result.set(collection.name, collection);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return result;
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
export function extend(collectionOptions, mergeOptions) {
|
|
287
|
-
return {
|
|
288
|
-
collectionOptions,
|
|
289
|
-
mergeOptions,
|
|
290
|
-
extend: true,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
export const defineCollection = (collectionOptions) => {
|
|
294
|
-
return collectionOptions;
|
|
295
|
-
};
|
|
296
|
-
export const extendCollection = (collectionOptions, mergeOptions) => {
|
|
297
|
-
return {
|
|
298
|
-
collectionOptions,
|
|
299
|
-
mergeOptions,
|
|
300
|
-
extend: true,
|
|
301
|
-
};
|
|
302
|
-
};
|
|
303
|
-
applyMixins(Database, [AsyncEmitter]);
|
|
304
|
-
export default Database;
|
|
305
|
-
//# sourceMappingURL=database.js.map
|