@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.
Files changed (97) hide show
  1. package/lib/collection.d.ts +7 -2
  2. package/lib/collection.js +20 -8
  3. package/lib/database.d.ts +2 -2
  4. package/lib/database.js +6 -4
  5. package/lib/decorators/must-have-filter-decorator.d.ts +2 -0
  6. package/lib/decorators/must-have-filter-decorator.js +25 -0
  7. package/lib/{transaction-decorator.d.ts → decorators/transaction-decorator.d.ts} +0 -0
  8. package/lib/{transaction-decorator.js → decorators/transaction-decorator.js} +1 -4
  9. package/lib/errors/identifier-error.d.ts +3 -0
  10. package/lib/errors/identifier-error.js +16 -0
  11. package/lib/fields/belongs-to-field.js +9 -0
  12. package/lib/fields/belongs-to-many-field.js +10 -0
  13. package/lib/fields/date-field.d.ts +1 -1
  14. package/lib/fields/date-field.js +1 -1
  15. package/lib/fields/field.d.ts +1 -1
  16. package/lib/fields/field.js +2 -3
  17. package/lib/fields/formula-field.d.ts +5 -5
  18. package/lib/fields/formula-field.js +123 -110
  19. package/lib/fields/has-many-field.js +9 -0
  20. package/lib/fields/has-one-field.js +9 -0
  21. package/lib/fields/index.d.ts +3 -1
  22. package/lib/fields/index.js +34 -1
  23. package/lib/fields/number-field.d.ts +6 -0
  24. package/lib/fields/number-field.js +10 -1
  25. package/lib/fields/radio-field.d.ts +3 -1
  26. package/lib/fields/radio-field.js +14 -11
  27. package/lib/fields/sequence-field.d.ts +31 -0
  28. package/lib/fields/sequence-field.js +299 -0
  29. package/lib/fields/sort-field.d.ts +4 -4
  30. package/lib/fields/sort-field.js +93 -80
  31. package/lib/filter-parser.js +10 -2
  32. package/lib/index.d.ts +1 -1
  33. package/lib/index.js +7 -0
  34. package/lib/magic-attribute-model.d.ts +1 -0
  35. package/lib/magic-attribute-model.js +207 -6
  36. package/lib/mock-database.js +1 -1
  37. package/lib/operators/array.js +17 -9
  38. package/lib/operators/ne.d.ts +4 -0
  39. package/lib/operators/ne.js +3 -1
  40. package/lib/relation-repository/belongs-to-many-repository.js +6 -0
  41. package/lib/relation-repository/multiple-relation-repository.js +32 -9
  42. package/lib/relation-repository/relation-repository.js +7 -1
  43. package/lib/relation-repository/single-relation-repository.js +28 -0
  44. package/lib/repository.d.ts +5 -3
  45. package/lib/repository.js +40 -9
  46. package/lib/update-associations.js +48 -71
  47. package/lib/utils.d.ts +9 -0
  48. package/lib/utils.js +126 -0
  49. package/package.json +5 -3
  50. package/src/__tests__/collection.test.ts +47 -0
  51. package/src/__tests__/database.test.ts +2 -0
  52. package/src/__tests__/field-options/inddex.test.ts +43 -0
  53. package/src/__tests__/fields/array.test.ts +66 -0
  54. package/src/__tests__/fields/belongs-to-field.test.ts +35 -0
  55. package/src/__tests__/fields/belongs-to-many-field.test.ts +45 -0
  56. package/src/__tests__/fields/has-many-field.test.ts +22 -0
  57. package/src/__tests__/fields/has-one-field.test.ts +21 -0
  58. package/src/__tests__/fields/sequence-field.test.ts +455 -0
  59. package/src/__tests__/magic-attribute-model.test.ts +24 -0
  60. package/src/__tests__/operator/ne.test.ts +12 -0
  61. package/src/__tests__/relation-repository/appends.test.ts +64 -0
  62. package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +23 -0
  63. package/src/__tests__/relation-repository/has-many-repository.test.ts +20 -0
  64. package/src/__tests__/relation-repository/hasone-repository.test.ts +68 -1
  65. package/src/__tests__/repository/find.test.ts +35 -0
  66. package/src/__tests__/repository/update.test.ts +35 -0
  67. package/src/__tests__/repository.test.ts +15 -0
  68. package/src/collection.ts +28 -13
  69. package/src/database.ts +11 -7
  70. package/src/decorators/must-have-filter-decorator.ts +17 -0
  71. package/src/{transaction-decorator.ts → decorators/transaction-decorator.ts} +1 -2
  72. package/src/errors/identifier-error.ts +6 -0
  73. package/src/fields/belongs-to-field.ts +9 -0
  74. package/src/fields/belongs-to-many-field.ts +15 -0
  75. package/src/fields/date-field.ts +1 -1
  76. package/src/fields/field.ts +3 -3
  77. package/src/fields/formula-field.ts +13 -13
  78. package/src/fields/has-many-field.ts +10 -0
  79. package/src/fields/has-one-field.ts +13 -0
  80. package/src/fields/index.ts +5 -2
  81. package/src/fields/number-field.ts +10 -0
  82. package/src/fields/radio-field.ts +22 -24
  83. package/src/fields/sequence-field.ts +200 -0
  84. package/src/fields/sort-field.ts +9 -9
  85. package/src/filter-parser.ts +6 -5
  86. package/src/index.ts +1 -1
  87. package/src/magic-attribute-model.ts +188 -6
  88. package/src/mock-database.ts +1 -1
  89. package/src/operators/array.ts +17 -10
  90. package/src/operators/ne.ts +10 -6
  91. package/src/relation-repository/belongs-to-many-repository.ts +4 -0
  92. package/src/relation-repository/multiple-relation-repository.ts +26 -11
  93. package/src/relation-repository/relation-repository.ts +5 -1
  94. package/src/relation-repository/single-relation-repository.ts +27 -1
  95. package/src/repository.ts +37 -10
  96. package/src/update-associations.ts +41 -53
  97. package/src/utils.ts +71 -0
@@ -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: any): Field;
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: any): void;
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.push(association.target.name);
472
+ modelNames.add(association.target.name);
461
473
 
462
474
  if (association.through) {
463
- modelNames.push(association.through.model.name);
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.includes(model.name)) {
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: any): void;
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 => 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 keys = v.split(/\s|,/);
161
- keys.shift();
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,2 @@
1
+ declare const mustHaveFilter: () => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2
+ export default mustHaveFilter;
@@ -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;
@@ -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.apply(this, [callArguments]);
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,3 @@
1
+ export declare class IdentifierError extends Error {
2
+ constructor(message: string);
3
+ }
@@ -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.DateDataTypeConstructor;
4
+ get dataType(): DataTypes.DateDataType;
5
5
  }
6
6
  export interface DateFieldOptions extends BaseColumnFieldOptions {
7
7
  type: 'date';
@@ -19,7 +19,7 @@ var _field = require("./field");
19
19
 
20
20
  class DateField extends _field.Field {
21
21
  get dataType() {
22
- return _sequelize().DataTypes.DATE;
22
+ return _sequelize().DataTypes.DATE(3);
23
23
  }
24
24
 
25
25
  }
@@ -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;
@@ -62,8 +62,7 @@ class Field {
62
62
  this.collection = context.collection;
63
63
  this.options = options || {};
64
64
  this.init();
65
- } // TODO
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
- }): Promise<void>;
9
- caculateField(instance: any): Promise<void>;
10
- updateFieldData(instance: any, { transaction }: {
8
+ }) => Promise<void>;
9
+ caculateField: (instance: any) => Promise<void>;
10
+ updateFieldData: (instance: any, { transaction }: {
11
11
  transaction: any;
12
- }): Promise<void>;
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
- get dataType() {
46
- return _sequelize().DataTypes.FLOAT;
47
- }
45
+ constructor(...args) {
46
+ var _this;
48
47
 
49
- caculate(expression, scope) {
50
- let result = null;
48
+ super(...args);
49
+ _this = this;
51
50
 
52
- try {
53
- result = math().evaluate(expression, scope);
54
- result = math().round(result, 9);
55
- } catch (_unused) {}
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
- return result;
58
- }
63
+ var _iterator = _createForOfIteratorHelper(records),
64
+ _step;
59
65
 
60
- initFieldData({
61
- transaction
62
- }) {
63
- var _this = this;
64
-
65
- return _asyncToGenerator(function* () {
66
- const _this$options = _this.options,
67
- expression = _this$options.expression,
68
- name = _this$options.name;
69
- const records = yield _this.collection.repository.find({
70
- order: [_this.collection.model.primaryKeyAttribute],
71
- transaction
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
- var _iterator = _createForOfIteratorHelper(records),
75
- _step;
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
- try {
78
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
79
- const record = _step.value;
80
- const scope = record.toJSON();
103
+ try {
104
+ result = math().evaluate(expression, scope);
105
+ result = math().round(result, 9);
106
+ } catch (_unused) {}
81
107
 
82
- const result = _this.caculate(expression, scope);
108
+ if (result) {
109
+ instance.set(name, result);
110
+ }
111
+ });
83
112
 
84
- if (result) {
85
- yield record.update({
86
- [name]: result
87
- }, {
88
- transaction,
89
- silent: true,
90
- hooks: false
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
- } catch (err) {
95
- _iterator.e(err);
96
- } finally {
97
- _iterator.f();
98
- }
99
- })();
100
- }
156
+ });
101
157
 
102
- caculateField(instance) {
103
- var _this2 = this;
104
-
105
- return _asyncToGenerator(function* () {
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
- updateFieldData(instance, {
124
- transaction
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
- try {
143
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
144
- const record = _step2.value;
145
- const scope = record.toJSON();
168
+ caculate(expression, scope) {
169
+ let result = null;
146
170
 
147
- const result = _this3.caculate(expression, scope);
171
+ try {
172
+ result = math().evaluate(expression, scope);
173
+ result = math().round(result, 9);
174
+ } catch (_unused2) {}
148
175
 
149
- yield record.update({
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.bind(this));
169
- this.database.on('fields.afterUpdate', this.updateFieldData.bind(this));
170
- this.on('beforeCreate', this.caculateField.bind(this));
171
- this.on('beforeUpdate', this.caculateField.bind(this));
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('beforeCreate', this.caculateField.bind(this));
177
- this.off('beforeUpdate', this.caculateField.bind(this));
178
- this.database.off('fields.afterUpdate', this.updateFieldData.bind(this));
179
- this.off('afterSync', this.initFieldData.bind(this));
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;