@nocobase/database 0.7.4-alpha.4 → 0.7.5-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 +7 -2
- package/lib/collection.js +20 -8
- package/lib/database.d.ts +2 -2
- package/lib/database.js +6 -4
- package/lib/decorators/must-have-filter-decorator.d.ts +2 -0
- package/lib/decorators/must-have-filter-decorator.js +25 -0
- package/lib/{transaction-decorator.d.ts → decorators/transaction-decorator.d.ts} +0 -0
- package/lib/{transaction-decorator.js → decorators/transaction-decorator.js} +1 -4
- package/lib/errors/identifier-error.d.ts +3 -0
- package/lib/errors/identifier-error.js +16 -0
- package/lib/fields/belongs-to-field.js +9 -0
- package/lib/fields/belongs-to-many-field.js +10 -0
- package/lib/fields/date-field.d.ts +1 -1
- package/lib/fields/date-field.js +1 -1
- package/lib/fields/field.d.ts +1 -1
- package/lib/fields/field.js +2 -3
- package/lib/fields/formula-field.d.ts +5 -5
- package/lib/fields/formula-field.js +123 -110
- package/lib/fields/has-many-field.js +9 -0
- package/lib/fields/has-one-field.js +9 -0
- package/lib/fields/index.d.ts +3 -1
- package/lib/fields/index.js +34 -1
- package/lib/fields/number-field.d.ts +6 -0
- package/lib/fields/number-field.js +10 -1
- package/lib/fields/radio-field.d.ts +3 -1
- package/lib/fields/radio-field.js +14 -11
- package/lib/fields/sequence-field.d.ts +31 -0
- package/lib/fields/sequence-field.js +299 -0
- package/lib/fields/sort-field.d.ts +4 -4
- package/lib/fields/sort-field.js +93 -80
- package/lib/filter-parser.js +10 -2
- package/lib/index.d.ts +1 -1
- package/lib/index.js +7 -0
- package/lib/magic-attribute-model.d.ts +1 -0
- package/lib/magic-attribute-model.js +207 -6
- package/lib/mock-database.js +1 -1
- package/lib/operators/array.js +17 -9
- package/lib/operators/ne.d.ts +4 -0
- package/lib/operators/ne.js +3 -1
- package/lib/relation-repository/belongs-to-many-repository.js +6 -0
- package/lib/relation-repository/multiple-relation-repository.js +32 -9
- package/lib/relation-repository/relation-repository.js +7 -1
- package/lib/relation-repository/single-relation-repository.js +28 -0
- package/lib/repository.d.ts +5 -3
- package/lib/repository.js +40 -9
- package/lib/update-associations.js +48 -71
- package/lib/utils.d.ts +9 -0
- package/lib/utils.js +126 -0
- package/package.json +5 -3
- package/src/__tests__/collection.test.ts +47 -0
- package/src/__tests__/database.test.ts +2 -0
- package/src/__tests__/field-options/inddex.test.ts +43 -0
- package/src/__tests__/fields/array.test.ts +66 -0
- package/src/__tests__/fields/belongs-to-field.test.ts +35 -0
- package/src/__tests__/fields/belongs-to-many-field.test.ts +45 -0
- package/src/__tests__/fields/has-many-field.test.ts +22 -0
- package/src/__tests__/fields/has-one-field.test.ts +21 -0
- package/src/__tests__/fields/sequence-field.test.ts +455 -0
- package/src/__tests__/magic-attribute-model.test.ts +24 -0
- package/src/__tests__/operator/ne.test.ts +12 -0
- package/src/__tests__/relation-repository/appends.test.ts +64 -0
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +23 -0
- package/src/__tests__/relation-repository/has-many-repository.test.ts +20 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +68 -1
- package/src/__tests__/repository/find.test.ts +35 -0
- package/src/__tests__/repository/update.test.ts +35 -0
- package/src/__tests__/repository.test.ts +15 -0
- package/src/collection.ts +28 -13
- package/src/database.ts +11 -7
- package/src/decorators/must-have-filter-decorator.ts +17 -0
- package/src/{transaction-decorator.ts → decorators/transaction-decorator.ts} +1 -2
- package/src/errors/identifier-error.ts +6 -0
- package/src/fields/belongs-to-field.ts +9 -0
- package/src/fields/belongs-to-many-field.ts +15 -0
- package/src/fields/date-field.ts +1 -1
- package/src/fields/field.ts +3 -3
- package/src/fields/formula-field.ts +13 -13
- package/src/fields/has-many-field.ts +10 -0
- package/src/fields/has-one-field.ts +13 -0
- package/src/fields/index.ts +5 -2
- package/src/fields/number-field.ts +10 -0
- package/src/fields/radio-field.ts +22 -24
- package/src/fields/sequence-field.ts +200 -0
- package/src/fields/sort-field.ts +9 -9
- package/src/filter-parser.ts +6 -5
- package/src/index.ts +1 -1
- package/src/magic-attribute-model.ts +188 -6
- package/src/mock-database.ts +1 -1
- package/src/operators/array.ts +17 -10
- package/src/operators/ne.ts +10 -6
- package/src/relation-repository/belongs-to-many-repository.ts +4 -0
- package/src/relation-repository/multiple-relation-repository.ts +26 -11
- package/src/relation-repository/relation-repository.ts +5 -1
- package/src/relation-repository/single-relation-repository.ts +27 -1
- package/src/repository.ts +37 -10
- package/src/update-associations.ts +41 -53
- package/src/utils.ts +71 -0
package/lib/collection.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
42
42
|
get name(): string;
|
|
43
43
|
get db(): Database;
|
|
44
44
|
constructor(options: CollectionOptions, context?: CollectionContext);
|
|
45
|
+
private checkOptions;
|
|
45
46
|
private sequelizeModelOptions;
|
|
46
47
|
/**
|
|
47
48
|
* TODO
|
|
@@ -60,7 +61,7 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
60
61
|
remove(): void;
|
|
61
62
|
removeFromDb(options?: QueryInterfaceDropTableOptions): Promise<void>;
|
|
62
63
|
existsInDb(options?: Transactionable): Promise<boolean>;
|
|
63
|
-
removeField(name:
|
|
64
|
+
removeField(name: string): void | Field;
|
|
64
65
|
/**
|
|
65
66
|
* TODO
|
|
66
67
|
*/
|
|
@@ -73,7 +74,11 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
73
74
|
* @param options
|
|
74
75
|
*/
|
|
75
76
|
updateField(name: string, options: FieldOptions): void;
|
|
76
|
-
addIndex(index:
|
|
77
|
+
addIndex(index: string | string[] | {
|
|
78
|
+
fields: string[];
|
|
79
|
+
unique?: boolean;
|
|
80
|
+
[key: string]: any;
|
|
81
|
+
}): void;
|
|
77
82
|
removeIndex(fields: any): void;
|
|
78
83
|
sync(syncOptions?: SyncOptions): Promise<void>;
|
|
79
84
|
}
|
package/lib/collection.js
CHANGED
|
@@ -49,6 +49,8 @@ var _model = require("./model");
|
|
|
49
49
|
|
|
50
50
|
var _repository = require("./repository");
|
|
51
51
|
|
|
52
|
+
var _utils = require("./utils");
|
|
53
|
+
|
|
52
54
|
const _excluded = ["name"],
|
|
53
55
|
_excluded2 = ["name"];
|
|
54
56
|
|
|
@@ -95,6 +97,7 @@ class Collection extends _events().EventEmitter {
|
|
|
95
97
|
this.fields = new Map();
|
|
96
98
|
this.model = void 0;
|
|
97
99
|
this.repository = void 0;
|
|
100
|
+
this.checkOptions(options);
|
|
98
101
|
this.context = context;
|
|
99
102
|
this.options = options;
|
|
100
103
|
this.bindFieldEventListener();
|
|
@@ -104,6 +107,10 @@ class Collection extends _events().EventEmitter {
|
|
|
104
107
|
this.setSortable(options.sortable);
|
|
105
108
|
}
|
|
106
109
|
|
|
110
|
+
checkOptions(options) {
|
|
111
|
+
(0, _utils.checkIdentifier)(options.name);
|
|
112
|
+
}
|
|
113
|
+
|
|
107
114
|
sequelizeModelOptions() {
|
|
108
115
|
const _this$options = this.options,
|
|
109
116
|
name = _this$options.name,
|
|
@@ -176,9 +183,7 @@ class Collection extends _events().EventEmitter {
|
|
|
176
183
|
}
|
|
177
184
|
|
|
178
185
|
bindFieldEventListener() {
|
|
179
|
-
this.on('field.afterAdd', field =>
|
|
180
|
-
field.bind();
|
|
181
|
-
});
|
|
186
|
+
this.on('field.afterAdd', field => field.bind());
|
|
182
187
|
this.on('field.afterRemove', field => field.unbind());
|
|
183
188
|
}
|
|
184
189
|
|
|
@@ -203,6 +208,7 @@ class Collection extends _events().EventEmitter {
|
|
|
203
208
|
}
|
|
204
209
|
|
|
205
210
|
setField(name, options) {
|
|
211
|
+
(0, _utils.checkIdentifier)(name);
|
|
206
212
|
const database = this.context.database;
|
|
207
213
|
const field = database.buildField(_objectSpread({
|
|
208
214
|
name
|
|
@@ -432,7 +438,13 @@ class Collection extends _events().EventEmitter {
|
|
|
432
438
|
const tableName = this.model.getTableName(); // @ts-ignore
|
|
433
439
|
|
|
434
440
|
this.model._indexes = this.model.options.indexes // @ts-ignore
|
|
435
|
-
.map(index => _sequelize().Utils.nameIndex(this.model._conformIndex(index), tableName))
|
|
441
|
+
.map(index => _sequelize().Utils.nameIndex(this.model._conformIndex(index), tableName)).map(item => {
|
|
442
|
+
if (item.name && item.name.length > 63) {
|
|
443
|
+
item.name = 'i_' + (0, _utils.md5)(item.name);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return item;
|
|
447
|
+
});
|
|
436
448
|
}
|
|
437
449
|
|
|
438
450
|
removeIndex(fields) {
|
|
@@ -452,22 +464,22 @@ class Collection extends _events().EventEmitter {
|
|
|
452
464
|
var _this3 = this;
|
|
453
465
|
|
|
454
466
|
return _asyncToGenerator(function* () {
|
|
455
|
-
const modelNames = [_this3.model.name];
|
|
467
|
+
const modelNames = new Set([_this3.model.name]);
|
|
456
468
|
const associations = _this3.model.associations;
|
|
457
469
|
|
|
458
470
|
for (const associationKey in associations) {
|
|
459
471
|
const association = associations[associationKey];
|
|
460
|
-
modelNames.
|
|
472
|
+
modelNames.add(association.target.name);
|
|
461
473
|
|
|
462
474
|
if (association.through) {
|
|
463
|
-
modelNames.
|
|
475
|
+
modelNames.add(association.through.model.name);
|
|
464
476
|
}
|
|
465
477
|
}
|
|
466
478
|
|
|
467
479
|
const models = []; // @ts-ignore
|
|
468
480
|
|
|
469
481
|
_this3.context.database.sequelize.modelManager.forEachModel(model => {
|
|
470
|
-
if (modelNames.
|
|
482
|
+
if (modelNames.has(model.name)) {
|
|
471
483
|
models.push(model);
|
|
472
484
|
}
|
|
473
485
|
});
|
package/lib/database.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { Collection, CollectionOptions, RepositoryType } from './collection';
|
|
|
8
8
|
import { ImportFileExtension } from './collection-importer';
|
|
9
9
|
import * as FieldTypes from './fields';
|
|
10
10
|
import { Field, FieldContext, RelationField } from './fields';
|
|
11
|
-
import { Migrations } from './migration';
|
|
11
|
+
import { MigrationItem, Migrations } from './migration';
|
|
12
12
|
import { Model } from './model';
|
|
13
13
|
import { ModelHook } from './model-hook';
|
|
14
14
|
import { RelationRepository } from './relation-repository/relation-repository';
|
|
@@ -67,7 +67,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
67
67
|
mergeOptions?: any;
|
|
68
68
|
}[]>;
|
|
69
69
|
constructor(options: DatabaseOptions);
|
|
70
|
-
addMigration(item:
|
|
70
|
+
addMigration(item: MigrationItem): void;
|
|
71
71
|
addMigrations(options: AddMigrationsOptions): void;
|
|
72
72
|
inDialect(...dialect: string[]): boolean;
|
|
73
73
|
private requireModule;
|
package/lib/database.js
CHANGED
|
@@ -152,14 +152,16 @@ class DatabaseVersion {
|
|
|
152
152
|
},
|
|
153
153
|
mysql: {
|
|
154
154
|
sql: 'select version() as version',
|
|
155
|
-
get: v =>
|
|
155
|
+
get: v => {
|
|
156
|
+
const m = /([\d+\.]+)/.exec(v);
|
|
157
|
+
return m[0];
|
|
158
|
+
}
|
|
156
159
|
},
|
|
157
160
|
postgres: {
|
|
158
161
|
sql: 'select version() as version',
|
|
159
162
|
get: v => {
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
return _semver().default.minVersion(keys.shift()).version;
|
|
163
|
+
const m = /([\d+\.]+)/.exec(v);
|
|
164
|
+
return _semver().default.minVersion(m[0]).version;
|
|
163
165
|
}
|
|
164
166
|
}
|
|
165
167
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
const mustHaveFilter = () => (target, propertyKey, descriptor) => {
|
|
9
|
+
const oldValue = descriptor.value;
|
|
10
|
+
|
|
11
|
+
descriptor.value = function () {
|
|
12
|
+
const options = arguments[0];
|
|
13
|
+
|
|
14
|
+
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
|
+
throw new Error(`must provide filter or filterByTk for ${propertyKey} call, or set forceUpdate to true`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return oldValue.apply(this, arguments);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return descriptor;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
var _default = mustHaveFilter;
|
|
25
|
+
exports.default = _default;
|
|
File without changes
|
|
@@ -63,13 +63,10 @@ function transactionWrapperBuilder(transactionGenerator) {
|
|
|
63
63
|
throw new Error(`please provide transactionInjector for ${name} call`);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
const results = yield oldValue.
|
|
66
|
+
const results = yield oldValue.call(this, callArguments);
|
|
67
67
|
yield transaction.commit();
|
|
68
68
|
return results;
|
|
69
69
|
} catch (err) {
|
|
70
|
-
console.error({
|
|
71
|
-
err
|
|
72
|
-
});
|
|
73
70
|
yield transaction.rollback();
|
|
74
71
|
throw err;
|
|
75
72
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.IdentifierError = void 0;
|
|
7
|
+
|
|
8
|
+
class IdentifierError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'IdentifierError';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.IdentifierError = IdentifierError;
|
|
@@ -25,6 +25,8 @@ function _sequelize() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
var _utils = require("../utils");
|
|
29
|
+
|
|
28
30
|
var _relationField = require("./relation-field");
|
|
29
31
|
|
|
30
32
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
@@ -71,6 +73,13 @@ class BelongsToField extends _relationField.RelationField {
|
|
|
71
73
|
this.options.foreignKey = association.foreignKey;
|
|
72
74
|
}
|
|
73
75
|
|
|
76
|
+
try {
|
|
77
|
+
(0, _utils.checkIdentifier)(this.options.foreignKey);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
this.unbind();
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
|
|
74
83
|
if (!this.options.sourceKey) {
|
|
75
84
|
// @ts-ignore
|
|
76
85
|
this.options.sourceKey = association.sourceKey;
|
|
@@ -25,6 +25,8 @@ function _sequelize() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
var _utils = require("../utils");
|
|
29
|
+
|
|
28
30
|
var _relationField = require("./relation-field");
|
|
29
31
|
|
|
30
32
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
@@ -84,6 +86,14 @@ class BelongsToManyField extends _relationField.RelationField {
|
|
|
84
86
|
this.options.otherKey = association.otherKey;
|
|
85
87
|
}
|
|
86
88
|
|
|
89
|
+
try {
|
|
90
|
+
(0, _utils.checkIdentifier)(this.options.foreignKey);
|
|
91
|
+
(0, _utils.checkIdentifier)(this.options.otherKey);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
this.unbind();
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
|
|
87
97
|
if (!this.options.through) {
|
|
88
98
|
this.options.through = this.through;
|
|
89
99
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DataTypes } from 'sequelize';
|
|
2
2
|
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
3
|
export declare class DateField extends Field {
|
|
4
|
-
get dataType(): DataTypes.
|
|
4
|
+
get dataType(): DataTypes.DateDataType;
|
|
5
5
|
}
|
|
6
6
|
export interface DateFieldOptions extends BaseColumnFieldOptions {
|
|
7
7
|
type: 'date';
|
package/lib/fields/date-field.js
CHANGED
package/lib/fields/field.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export declare abstract class Field {
|
|
|
29
29
|
on(eventName: string, listener: (...args: any[]) => void): this;
|
|
30
30
|
off(eventName: string, listener: (...args: any[]) => void): this;
|
|
31
31
|
get(name: string): any;
|
|
32
|
-
remove(): Field;
|
|
32
|
+
remove(): void | Field;
|
|
33
33
|
removeFromDb(options?: QueryInterfaceOptions): Promise<void>;
|
|
34
34
|
existsInDb(options?: Transactionable): Promise<boolean>;
|
|
35
35
|
merge(obj: any): void;
|
package/lib/fields/field.js
CHANGED
|
@@ -62,8 +62,7 @@ class Field {
|
|
|
62
62
|
this.collection = context.collection;
|
|
63
63
|
this.options = options || {};
|
|
64
64
|
this.init();
|
|
65
|
-
}
|
|
66
|
-
|
|
65
|
+
}
|
|
67
66
|
|
|
68
67
|
sync(syncOptions) {
|
|
69
68
|
var _this = this;
|
|
@@ -219,7 +218,7 @@ class Field {
|
|
|
219
218
|
const model = this.context.collection.model;
|
|
220
219
|
model.removeAttribute(this.name);
|
|
221
220
|
|
|
222
|
-
if (this.options.index) {
|
|
221
|
+
if (this.options.index || this.options.unique) {
|
|
223
222
|
this.context.collection.removeIndex([this.name]);
|
|
224
223
|
}
|
|
225
224
|
}
|
|
@@ -3,13 +3,13 @@ import { BaseColumnFieldOptions, Field } from './field';
|
|
|
3
3
|
export declare class FormulaField extends Field {
|
|
4
4
|
get dataType(): DataTypes.FloatDataTypeConstructor;
|
|
5
5
|
caculate(expression: any, scope: any): any;
|
|
6
|
-
initFieldData({ transaction }: {
|
|
6
|
+
initFieldData: ({ transaction }: {
|
|
7
7
|
transaction: any;
|
|
8
|
-
})
|
|
9
|
-
caculateField(instance: any)
|
|
10
|
-
updateFieldData(instance: any, { transaction }: {
|
|
8
|
+
}) => Promise<void>;
|
|
9
|
+
caculateField: (instance: any) => Promise<void>;
|
|
10
|
+
updateFieldData: (instance: any, { transaction }: {
|
|
11
11
|
transaction: any;
|
|
12
|
-
})
|
|
12
|
+
}) => Promise<void>;
|
|
13
13
|
bind(): void;
|
|
14
14
|
unbind(): void;
|
|
15
15
|
}
|
|
@@ -42,141 +42,154 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
42
42
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
43
43
|
|
|
44
44
|
class FormulaField extends _field.Field {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
45
|
+
constructor(...args) {
|
|
46
|
+
var _this;
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
super(...args);
|
|
49
|
+
_this = this;
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
this.initFieldData = /*#__PURE__*/function () {
|
|
52
|
+
var _ref = _asyncToGenerator(function* ({
|
|
53
|
+
transaction
|
|
54
|
+
}) {
|
|
55
|
+
const _this$options = _this.options,
|
|
56
|
+
expression = _this$options.expression,
|
|
57
|
+
name = _this$options.name;
|
|
58
|
+
const records = yield _this.collection.repository.find({
|
|
59
|
+
order: [_this.collection.model.primaryKeyAttribute],
|
|
60
|
+
transaction
|
|
61
|
+
});
|
|
56
62
|
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
var _iterator = _createForOfIteratorHelper(records),
|
|
64
|
+
_step;
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
try {
|
|
67
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
68
|
+
const record = _step.value;
|
|
69
|
+
const scope = record.toJSON();
|
|
70
|
+
|
|
71
|
+
const result = _this.caculate(expression, scope);
|
|
72
|
+
|
|
73
|
+
if (result) {
|
|
74
|
+
yield record.update({
|
|
75
|
+
[name]: result
|
|
76
|
+
}, {
|
|
77
|
+
transaction,
|
|
78
|
+
silent: true,
|
|
79
|
+
hooks: false
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
_iterator.e(err);
|
|
85
|
+
} finally {
|
|
86
|
+
_iterator.f();
|
|
87
|
+
}
|
|
72
88
|
});
|
|
73
89
|
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
return function (_x) {
|
|
91
|
+
return _ref.apply(this, arguments);
|
|
92
|
+
};
|
|
93
|
+
}();
|
|
94
|
+
|
|
95
|
+
this.caculateField = /*#__PURE__*/function () {
|
|
96
|
+
var _ref2 = _asyncToGenerator(function* (instance) {
|
|
97
|
+
const _this$options2 = _this.options,
|
|
98
|
+
expression = _this$options2.expression,
|
|
99
|
+
name = _this$options2.name;
|
|
100
|
+
const scope = instance.toJSON();
|
|
101
|
+
let result;
|
|
76
102
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
103
|
+
try {
|
|
104
|
+
result = math().evaluate(expression, scope);
|
|
105
|
+
result = math().round(result, 9);
|
|
106
|
+
} catch (_unused) {}
|
|
81
107
|
|
|
82
|
-
|
|
108
|
+
if (result) {
|
|
109
|
+
instance.set(name, result);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
83
112
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
113
|
+
return function (_x2) {
|
|
114
|
+
return _ref2.apply(this, arguments);
|
|
115
|
+
};
|
|
116
|
+
}();
|
|
117
|
+
|
|
118
|
+
this.updateFieldData = /*#__PURE__*/function () {
|
|
119
|
+
var _ref3 = _asyncToGenerator(function* (instance, {
|
|
120
|
+
transaction
|
|
121
|
+
}) {
|
|
122
|
+
if (_this.collection.name === instance.collectionName && instance.name === _this.options.name) {
|
|
123
|
+
_this.options = Object.assign(_this.options, instance.options);
|
|
124
|
+
const _this$options3 = _this.options,
|
|
125
|
+
name = _this$options3.name,
|
|
126
|
+
expression = _this$options3.expression;
|
|
127
|
+
const records = yield _this.collection.repository.find({
|
|
128
|
+
order: [_this.collection.model.primaryKeyAttribute],
|
|
129
|
+
transaction
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
var _iterator2 = _createForOfIteratorHelper(records),
|
|
133
|
+
_step2;
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
137
|
+
const record = _step2.value;
|
|
138
|
+
const scope = record.toJSON();
|
|
139
|
+
|
|
140
|
+
const result = _this.caculate(expression, scope);
|
|
141
|
+
|
|
142
|
+
yield record.update({
|
|
143
|
+
[name]: result
|
|
144
|
+
}, {
|
|
145
|
+
transaction,
|
|
146
|
+
silent: true,
|
|
147
|
+
hooks: false
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
} catch (err) {
|
|
151
|
+
_iterator2.e(err);
|
|
152
|
+
} finally {
|
|
153
|
+
_iterator2.f();
|
|
92
154
|
}
|
|
93
155
|
}
|
|
94
|
-
}
|
|
95
|
-
_iterator.e(err);
|
|
96
|
-
} finally {
|
|
97
|
-
_iterator.f();
|
|
98
|
-
}
|
|
99
|
-
})();
|
|
100
|
-
}
|
|
156
|
+
});
|
|
101
157
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const _this2$options = _this2.options,
|
|
107
|
-
expression = _this2$options.expression,
|
|
108
|
-
name = _this2$options.name;
|
|
109
|
-
const scope = instance.toJSON();
|
|
110
|
-
let result;
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
result = math().evaluate(expression, scope);
|
|
114
|
-
result = math().round(result, 9);
|
|
115
|
-
} catch (_unused2) {}
|
|
116
|
-
|
|
117
|
-
if (result) {
|
|
118
|
-
instance.set(name, result);
|
|
119
|
-
}
|
|
120
|
-
})();
|
|
158
|
+
return function (_x3, _x4) {
|
|
159
|
+
return _ref3.apply(this, arguments);
|
|
160
|
+
};
|
|
161
|
+
}();
|
|
121
162
|
}
|
|
122
163
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
var _this3 = this;
|
|
127
|
-
|
|
128
|
-
return _asyncToGenerator(function* () {
|
|
129
|
-
if (_this3.collection.name === instance.collectionName && instance.name === _this3.options.name) {
|
|
130
|
-
_this3.options = Object.assign(_this3.options, instance.options);
|
|
131
|
-
const _this3$options = _this3.options,
|
|
132
|
-
name = _this3$options.name,
|
|
133
|
-
expression = _this3$options.expression;
|
|
134
|
-
const records = yield _this3.collection.repository.find({
|
|
135
|
-
order: [_this3.collection.model.primaryKeyAttribute],
|
|
136
|
-
transaction
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
var _iterator2 = _createForOfIteratorHelper(records),
|
|
140
|
-
_step2;
|
|
164
|
+
get dataType() {
|
|
165
|
+
return _sequelize().DataTypes.FLOAT;
|
|
166
|
+
}
|
|
141
167
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const record = _step2.value;
|
|
145
|
-
const scope = record.toJSON();
|
|
168
|
+
caculate(expression, scope) {
|
|
169
|
+
let result = null;
|
|
146
170
|
|
|
147
|
-
|
|
171
|
+
try {
|
|
172
|
+
result = math().evaluate(expression, scope);
|
|
173
|
+
result = math().round(result, 9);
|
|
174
|
+
} catch (_unused2) {}
|
|
148
175
|
|
|
149
|
-
|
|
150
|
-
[name]: result
|
|
151
|
-
}, {
|
|
152
|
-
transaction,
|
|
153
|
-
silent: true,
|
|
154
|
-
hooks: false
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
} catch (err) {
|
|
158
|
-
_iterator2.e(err);
|
|
159
|
-
} finally {
|
|
160
|
-
_iterator2.f();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
})();
|
|
176
|
+
return result;
|
|
164
177
|
}
|
|
165
178
|
|
|
166
179
|
bind() {
|
|
167
180
|
super.bind();
|
|
168
|
-
this.on('afterSync', this.initFieldData
|
|
169
|
-
|
|
170
|
-
this.on('
|
|
171
|
-
this.on('
|
|
181
|
+
this.on('afterSync', this.initFieldData); // TODO: should not depends on fields table (which is defined by other plugin)
|
|
182
|
+
|
|
183
|
+
this.database.on('fields.afterUpdate', this.updateFieldData);
|
|
184
|
+
this.on('beforeSave', this.caculateField);
|
|
172
185
|
}
|
|
173
186
|
|
|
174
187
|
unbind() {
|
|
175
188
|
super.unbind();
|
|
176
|
-
this.off('
|
|
177
|
-
|
|
178
|
-
this.database.off('fields.afterUpdate', this.updateFieldData
|
|
179
|
-
this.off('afterSync', this.initFieldData
|
|
189
|
+
this.off('beforeSave', this.caculateField); // TODO: should not depends on fields table
|
|
190
|
+
|
|
191
|
+
this.database.off('fields.afterUpdate', this.updateFieldData);
|
|
192
|
+
this.off('afterSync', this.initFieldData);
|
|
180
193
|
}
|
|
181
194
|
|
|
182
195
|
}
|
|
@@ -25,6 +25,8 @@ function _sequelize() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
var _utils = require("../utils");
|
|
29
|
+
|
|
28
30
|
var _relationField = require("./relation-field");
|
|
29
31
|
|
|
30
32
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
@@ -73,6 +75,13 @@ class HasManyField extends _relationField.RelationField {
|
|
|
73
75
|
this.options.foreignKey = association.foreignKey;
|
|
74
76
|
}
|
|
75
77
|
|
|
78
|
+
try {
|
|
79
|
+
(0, _utils.checkIdentifier)(this.options.foreignKey);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
this.unbind();
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
|
|
76
85
|
if (!this.options.sourceKey) {
|
|
77
86
|
// @ts-ignore
|
|
78
87
|
this.options.sourceKey = association.sourceKey;
|
|
@@ -25,6 +25,8 @@ function _sequelize() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
var _utils = require("../utils");
|
|
29
|
+
|
|
28
30
|
var _relationField = require("./relation-field");
|
|
29
31
|
|
|
30
32
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
@@ -74,6 +76,13 @@ class HasOneField extends _relationField.RelationField {
|
|
|
74
76
|
this.options.foreignKey = association.foreignKey;
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
try {
|
|
80
|
+
(0, _utils.checkIdentifier)(this.options.foreignKey);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
this.unbind();
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
|
|
77
86
|
if (!this.options.sourceKey) {
|
|
78
87
|
// @ts-ignore
|
|
79
88
|
this.options.sourceKey = association.sourceKey;
|