@nocobase/database 0.9.1-alpha.2 → 0.9.2-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 (168) hide show
  1. package/lib/collection-group-manager.d.ts +13 -0
  2. package/lib/collection-group-manager.js +91 -0
  3. package/lib/collection-importer.js +0 -24
  4. package/lib/collection.d.ts +24 -3
  5. package/lib/collection.js +176 -236
  6. package/lib/database-utils/index.js +3 -15
  7. package/lib/database.d.ts +3 -0
  8. package/lib/database.js +160 -298
  9. package/lib/decorators/must-have-filter-decorator.js +0 -7
  10. package/lib/decorators/transaction-decorator.js +5 -18
  11. package/lib/errors/identifier-error.js +0 -3
  12. package/lib/features/ReferencesMap.js +1 -14
  13. package/lib/features/referential-integrity-check.js +7 -21
  14. package/lib/field-repository/array-field-repository.js +5 -45
  15. package/lib/fields/array-field.js +0 -13
  16. package/lib/fields/belongs-to-field.js +24 -50
  17. package/lib/fields/belongs-to-many-field.js +23 -47
  18. package/lib/fields/boolean-field.js +0 -7
  19. package/lib/fields/context-field.js +2 -23
  20. package/lib/fields/date-field.d.ts +4 -0
  21. package/lib/fields/date-field.js +15 -7
  22. package/lib/fields/field.js +32 -85
  23. package/lib/fields/has-many-field.js +16 -49
  24. package/lib/fields/has-one-field.js +18 -52
  25. package/lib/fields/index.js +0 -44
  26. package/lib/fields/json-field.js +0 -12
  27. package/lib/fields/number-field.js +0 -23
  28. package/lib/fields/password-field.js +8 -35
  29. package/lib/fields/radio-field.js +0 -18
  30. package/lib/fields/relation-field.js +4 -16
  31. package/lib/fields/set-field.js +0 -8
  32. package/lib/fields/sort-field.js +84 -73
  33. package/lib/fields/string-field.js +0 -7
  34. package/lib/fields/text-field.js +0 -7
  35. package/lib/fields/time-field.js +0 -7
  36. package/lib/fields/uid-field.js +4 -22
  37. package/lib/fields/uuid-field.js +3 -12
  38. package/lib/fields/virtual-field.js +0 -7
  39. package/lib/filter-match.js +7 -22
  40. package/lib/filter-parser.js +37 -101
  41. package/lib/index.d.ts +3 -0
  42. package/lib/index.js +36 -42
  43. package/lib/inherited-collection.js +15 -62
  44. package/lib/inherited-map.js +7 -48
  45. package/lib/listeners/adjacency-list.d.ts +3 -0
  46. package/lib/listeners/adjacency-list.js +91 -0
  47. package/lib/listeners/index.d.ts +2 -0
  48. package/lib/listeners/index.js +12 -0
  49. package/lib/magic-attribute-model.js +58 -114
  50. package/lib/migration.js +7 -28
  51. package/lib/mock-database.d.ts +4 -4
  52. package/lib/mock-database.js +15 -18
  53. package/lib/model-hook.js +4 -35
  54. package/lib/model.js +12 -54
  55. package/lib/operators/array.js +2 -32
  56. package/lib/operators/association.js +0 -6
  57. package/lib/operators/boolean.js +0 -6
  58. package/lib/operators/child-collection.d.ts +2 -0
  59. package/lib/operators/child-collection.js +32 -0
  60. package/lib/operators/date.js +123 -60
  61. package/lib/operators/empty.js +3 -32
  62. package/lib/operators/eq.d.ts +2 -0
  63. package/lib/operators/eq.js +26 -0
  64. package/lib/operators/index.js +4 -7
  65. package/lib/operators/ne.js +5 -5
  66. package/lib/operators/notIn.js +0 -5
  67. package/lib/operators/string.js +0 -11
  68. package/lib/operators/utils.js +0 -6
  69. package/lib/options-parser.d.ts +1 -1
  70. package/lib/options-parser.js +47 -107
  71. package/lib/playground.js +0 -4
  72. package/lib/query-interface/mysql-query-interface.d.ts +11 -0
  73. package/lib/query-interface/mysql-query-interface.js +59 -10
  74. package/lib/query-interface/postgres-query-interface.d.ts +8 -0
  75. package/lib/query-interface/postgres-query-interface.js +70 -12
  76. package/lib/query-interface/query-interface-builder.js +0 -5
  77. package/lib/query-interface/query-interface.d.ts +12 -0
  78. package/lib/query-interface/query-interface.js +33 -3
  79. package/lib/query-interface/sqlite-query-interface.d.ts +11 -0
  80. package/lib/query-interface/sqlite-query-interface.js +61 -10
  81. package/lib/relation-repository/belongs-to-many-repository.js +21 -78
  82. package/lib/relation-repository/belongs-to-repository.js +0 -3
  83. package/lib/relation-repository/hasmany-repository.js +8 -44
  84. package/lib/relation-repository/hasone-repository.js +0 -3
  85. package/lib/relation-repository/multiple-relation-repository.js +14 -68
  86. package/lib/relation-repository/relation-repository.js +5 -42
  87. package/lib/relation-repository/single-relation-repository.js +5 -43
  88. package/lib/repository.d.ts +1 -0
  89. package/lib/repository.js +36 -182
  90. package/lib/sql-parser/index.js +10527 -0
  91. package/lib/sql-parser/sql.pegjs +1297 -0
  92. package/lib/sync-runner.js +19 -54
  93. package/lib/update-associations.js +58 -157
  94. package/lib/update-guard.js +10 -49
  95. package/lib/utils.js +6 -54
  96. package/lib/value-parsers/array-value-parser.js +3 -21
  97. package/lib/value-parsers/base-value-parser.js +0 -13
  98. package/lib/value-parsers/boolean-value-parser.js +4 -10
  99. package/lib/value-parsers/date-value-parser.js +0 -23
  100. package/lib/value-parsers/index.js +0 -10
  101. package/lib/value-parsers/json-value-parser.js +0 -7
  102. package/lib/value-parsers/number-value-parser.js +0 -9
  103. package/lib/value-parsers/string-value-parser.js +3 -20
  104. package/lib/value-parsers/to-many-value-parser.js +1 -42
  105. package/lib/value-parsers/to-one-value-parser.js +0 -14
  106. package/lib/view/field-type-map.d.ts +47 -0
  107. package/lib/view/field-type-map.js +56 -0
  108. package/lib/view/view-inference.d.ts +31 -0
  109. package/lib/view/view-inference.js +92 -0
  110. package/lib/view-collection.d.ts +6 -0
  111. package/lib/view-collection.js +24 -0
  112. package/package.json +4 -4
  113. package/src/__tests__/collection.test.ts +44 -0
  114. package/src/__tests__/fields/date.test.ts +75 -0
  115. package/src/__tests__/fields/sort-field.test.ts +100 -0
  116. package/src/__tests__/filter.test.ts +3 -3
  117. package/src/__tests__/group.test.ts +50 -0
  118. package/src/__tests__/inhertits/collection-inherits.test.ts +114 -0
  119. package/src/__tests__/operator/date-operator.test.ts +244 -98
  120. package/src/__tests__/operator/eq.test.ts +76 -0
  121. package/src/__tests__/operator/ne.test.ts +19 -1
  122. package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +82 -0
  123. package/src/__tests__/repository/find.test.ts +33 -0
  124. package/src/__tests__/repository.test.ts +88 -0
  125. package/src/__tests__/sql-parser.test.ts +13 -0
  126. package/src/__tests__/tree.test.ts +217 -0
  127. package/src/__tests__/view/list-view.test.ts +34 -0
  128. package/src/__tests__/view/view-collection.test.ts +199 -0
  129. package/src/__tests__/view/view-inference.test.ts +145 -0
  130. package/src/__tests__/view/view-repository.test.ts +67 -0
  131. package/src/collection-group-manager.ts +94 -0
  132. package/src/collection.ts +108 -14
  133. package/src/database-utils/index.ts +1 -0
  134. package/src/database.ts +79 -12
  135. package/src/features/ReferencesMap.ts +3 -2
  136. package/src/fields/belongs-to-many-field.ts +15 -2
  137. package/src/fields/date-field.ts +18 -0
  138. package/src/fields/field.ts +16 -8
  139. package/src/fields/json-field.ts +1 -0
  140. package/src/fields/sort-field.ts +90 -29
  141. package/src/filter-parser.ts +1 -0
  142. package/src/index.ts +3 -1
  143. package/src/listeners/adjacency-list.ts +60 -0
  144. package/src/listeners/index.ts +7 -0
  145. package/src/mock-database.ts +14 -2
  146. package/src/model.ts +4 -0
  147. package/src/operators/child-collection.ts +24 -0
  148. package/src/operators/date.ts +108 -24
  149. package/src/operators/eq.ts +14 -0
  150. package/src/operators/index.ts +2 -0
  151. package/src/operators/ne.ts +12 -7
  152. package/src/options-parser.ts +25 -11
  153. package/src/query-interface/mysql-query-interface.ts +53 -1
  154. package/src/query-interface/postgres-query-interface.ts +84 -3
  155. package/src/query-interface/query-interface.ts +31 -0
  156. package/src/query-interface/sqlite-query-interface.ts +62 -1
  157. package/src/relation-repository/belongs-to-many-repository.ts +20 -1
  158. package/src/relation-repository/hasmany-repository.ts +5 -3
  159. package/src/relation-repository/multiple-relation-repository.ts +9 -1
  160. package/src/repository.ts +6 -13
  161. package/src/sql-parser/index.js +10698 -0
  162. package/src/sql-parser/readme.md +2 -0
  163. package/src/sql-parser/sql.pegjs +1297 -0
  164. package/src/sync-runner.ts +13 -15
  165. package/src/update-associations.ts +26 -22
  166. package/src/view/field-type-map.ts +56 -0
  167. package/src/view/view-inference.ts +106 -0
  168. package/src/view-collection.ts +21 -0
@@ -4,114 +4,97 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.BelongsToManyField = void 0;
7
-
8
7
  function _lodash() {
9
8
  const data = require("lodash");
10
-
11
9
  _lodash = function _lodash() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _sequelize() {
19
15
  const data = require("sequelize");
20
-
21
16
  _sequelize = function _sequelize() {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  var _utils = require("../utils");
29
-
30
22
  var _belongsToField = require("./belongs-to-field");
31
-
32
23
  var _relationField = require("./relation-field");
33
-
34
24
  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; }
35
-
36
25
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
37
-
38
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
39
-
26
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
28
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
40
29
  class BelongsToManyField extends _relationField.RelationField {
41
30
  get dataType() {
42
31
  return 'BelongsToMany';
43
32
  }
44
-
45
33
  get through() {
46
34
  return this.options.through || _sequelize().Utils.camelize([this.context.collection.model.name, this.target].map(name => name.toLowerCase()).sort().join('_'));
47
35
  }
48
-
49
36
  get otherKey() {
50
37
  return this.options.otherKey;
51
38
  }
52
-
53
39
  references(association) {
54
40
  const db = this.context.database;
55
41
  const onDelete = this.options.onDelete || 'CASCADE';
56
- return [_belongsToField.BelongsToField.toReference(db, association.toSource, onDelete), _belongsToField.BelongsToField.toReference(db, association.toTarget, onDelete)];
42
+ const targetAssociation = association.toTarget;
43
+ if (association.targetKey) {
44
+ targetAssociation.targetKey = association.targetKey;
45
+ }
46
+ const sourceAssociation = association.toSource;
47
+ if (association.sourceKey) {
48
+ sourceAssociation.targetKey = association.sourceKey;
49
+ }
50
+ return [_belongsToField.BelongsToField.toReference(db, targetAssociation, onDelete), _belongsToField.BelongsToField.toReference(db, sourceAssociation, onDelete)];
57
51
  }
58
-
59
52
  bind() {
60
53
  const _this$context = this.context,
61
- database = _this$context.database,
62
- collection = _this$context.collection;
54
+ database = _this$context.database,
55
+ collection = _this$context.collection;
63
56
  const Target = this.TargetModel;
64
-
65
57
  if (!Target) {
66
58
  database.addPendingField(this);
67
59
  return false;
68
60
  }
69
-
70
61
  const through = this.through;
71
62
  let Through;
72
-
73
63
  if (database.hasCollection(through)) {
74
64
  Through = database.getCollection(through);
75
65
  } else {
76
66
  const throughCollectionOptions = {
77
67
  name: through
78
68
  };
79
-
69
+ // set through collection schema
80
70
  if (this.collection.collectionSchema()) {
81
71
  throughCollectionOptions['schema'] = this.collection.collectionSchema();
82
72
  }
83
-
84
73
  Through = database.collection(throughCollectionOptions);
85
74
  Object.defineProperty(Through.model, 'isThrough', {
86
75
  value: true
87
76
  });
88
77
  }
89
-
90
78
  if (!this.options.onDelete) {
91
79
  this.options.onDelete = 'CASCADE';
92
80
  }
93
-
94
81
  const association = collection.model.belongsToMany(Target, _objectSpread(_objectSpread({
95
82
  constraints: false
96
83
  }, (0, _lodash().omit)(this.options, ['name', 'type', 'target'])), {}, {
97
84
  as: this.name,
98
85
  through: Through.model
99
- })); // 建立关系之后从 pending 列表中删除
100
-
86
+ }));
87
+ // 建立关系之后从 pending 列表中删除
101
88
  database.removePendingField(this);
102
-
103
89
  if (!this.options.foreignKey) {
104
90
  this.options.foreignKey = association.foreignKey;
105
91
  }
106
-
107
92
  if (!this.options.sourceKey) {
108
93
  this.options.sourceKey = association.sourceKey;
109
94
  }
110
-
111
95
  if (!this.options.otherKey) {
112
96
  this.options.otherKey = association.otherKey;
113
97
  }
114
-
115
98
  try {
116
99
  (0, _utils.checkIdentifier)(this.options.foreignKey);
117
100
  (0, _utils.checkIdentifier)(this.options.otherKey);
@@ -119,35 +102,28 @@ class BelongsToManyField extends _relationField.RelationField {
119
102
  this.unbind();
120
103
  throw error;
121
104
  }
122
-
123
105
  if (!this.options.through) {
124
106
  this.options.through = this.through;
125
107
  }
126
-
127
108
  Through.addIndex([this.options.foreignKey]);
128
109
  Through.addIndex([this.options.otherKey]);
129
110
  this.references(association).forEach(reference => this.database.referenceMap.addReference(reference));
130
111
  return true;
131
112
  }
132
-
133
113
  unbind() {
134
114
  const _this$context2 = this.context,
135
- database = _this$context2.database,
136
- collection = _this$context2.collection;
137
- const Through = database.getCollection(this.through); // 如果关系字段还没建立就删除了,也同步删除待建立关联的关系字段
138
-
139
- database.removePendingField(this); // 删掉 model 的关联字段
140
-
115
+ database = _this$context2.database,
116
+ collection = _this$context2.collection;
117
+ const Through = database.getCollection(this.through);
118
+ // 如果关系字段还没建立就删除了,也同步删除待建立关联的关系字段
119
+ database.removePendingField(this);
120
+ // 删掉 model 的关联字段
141
121
  const association = collection.model.associations[this.name];
142
-
143
122
  if (association) {
144
123
  this.references(association).forEach(reference => this.database.referenceMap.removeReference(reference));
145
124
  }
146
-
147
125
  this.clearAccessors();
148
126
  delete collection.model.associations[this.name];
149
127
  }
150
-
151
128
  }
152
-
153
129
  exports.BelongsToManyField = BelongsToManyField;
@@ -4,24 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.BooleanField = void 0;
7
-
8
7
  function _sequelize() {
9
8
  const data = require("sequelize");
10
-
11
9
  _sequelize = function _sequelize() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _field = require("./field");
19
-
20
15
  class BooleanField extends _field.Field {
21
16
  get dataType() {
22
17
  return _sequelize().DataTypes.BOOLEAN;
23
18
  }
24
-
25
19
  }
26
-
27
20
  exports.BooleanField = BooleanField;
@@ -4,83 +4,62 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ContextField = void 0;
7
-
8
7
  function _lodash() {
9
8
  const data = _interopRequireDefault(require("lodash"));
10
-
11
9
  _lodash = function _lodash() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _sequelize() {
19
15
  const data = require("sequelize");
20
-
21
16
  _sequelize = function _sequelize() {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  var _field = require("./field");
29
-
30
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
-
32
23
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
33
-
34
24
  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); }); }; }
35
-
36
25
  class ContextField extends _field.Field {
37
26
  constructor(...args) {
38
27
  var _this;
39
-
40
28
  super(...args);
41
29
  _this = this;
42
-
43
30
  this.listener = /*#__PURE__*/function () {
44
31
  var _ref = _asyncToGenerator(function* (model, options) {
45
32
  const _this$options = _this.options,
46
- name = _this$options.name,
47
- dataIndex = _this$options.dataIndex;
33
+ name = _this$options.name,
34
+ dataIndex = _this$options.dataIndex;
48
35
  const context = options.context;
49
36
  model.set(name, _lodash().default.get(context, dataIndex));
50
37
  model.changed(name, true);
51
38
  });
52
-
53
39
  return function (_x, _x2) {
54
40
  return _ref.apply(this, arguments);
55
41
  };
56
42
  }();
57
43
  }
58
-
59
44
  get dataType() {
60
45
  const type = this.options.dataType || 'string';
61
46
  return _sequelize().DataTypes[type.toUpperCase()] || _sequelize().DataTypes.STRING;
62
47
  }
63
-
64
48
  bind() {
65
49
  super.bind();
66
50
  const createOnly = this.options.createOnly;
67
51
  this.on('beforeCreate', this.listener);
68
-
69
52
  if (!createOnly) {
70
53
  this.on('beforeUpdate', this.listener);
71
54
  }
72
55
  }
73
-
74
56
  unbind() {
75
57
  super.unbind();
76
58
  const createOnly = this.options.createOnly;
77
59
  this.off('beforeCreate', this.listener);
78
-
79
60
  if (!createOnly) {
80
61
  this.off('beforeUpdate', this.listener);
81
62
  }
82
63
  }
83
-
84
64
  }
85
-
86
65
  exports.ContextField = ContextField;
@@ -2,6 +2,10 @@ import { DataTypes } from 'sequelize';
2
2
  import { BaseColumnFieldOptions, Field } from './field';
3
3
  export declare class DateField extends Field {
4
4
  get dataType(): DataTypes.DateDataType;
5
+ get timezone(): string;
6
+ getProps(): any;
7
+ isDateOnly(): boolean;
8
+ isGMT(): any;
5
9
  }
6
10
  export interface DateFieldOptions extends BaseColumnFieldOptions {
7
11
  type: 'date';
@@ -4,24 +4,32 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.DateField = void 0;
7
-
8
7
  function _sequelize() {
9
8
  const data = require("sequelize");
10
-
11
9
  _sequelize = function _sequelize() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _field = require("./field");
19
-
20
15
  class DateField extends _field.Field {
21
16
  get dataType() {
22
17
  return _sequelize().DataTypes.DATE(3);
23
18
  }
24
-
19
+ get timezone() {
20
+ return this.isGMT() ? '+00:00' : null;
21
+ }
22
+ getProps() {
23
+ var _this$options, _this$options$uiSchem;
24
+ return ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : (_this$options$uiSchem = _this$options.uiSchema) === null || _this$options$uiSchem === void 0 ? void 0 : _this$options$uiSchem['x-component-props']) || {};
25
+ }
26
+ isDateOnly() {
27
+ const props = this.getProps();
28
+ return !props.showTime;
29
+ }
30
+ isGMT() {
31
+ const props = this.getProps();
32
+ return props.gmt;
33
+ }
25
34
  }
26
-
27
35
  exports.DateField = DateField;
@@ -4,43 +4,28 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Field = void 0;
7
-
8
7
  function _lodash() {
9
8
  const data = _interopRequireDefault(require("lodash"));
10
-
11
9
  _lodash = function _lodash() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _utils = require("../utils");
19
-
20
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
16
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
23
-
24
17
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
25
-
26
18
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
27
-
28
19
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
29
-
30
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
31
-
20
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
32
21
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
33
-
34
22
  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; }
35
-
36
23
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
37
-
38
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
39
-
24
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
26
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
40
27
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
41
-
42
28
  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
-
44
29
  class Field {
45
30
  constructor(options, context) {
46
31
  this.options = void 0;
@@ -53,18 +38,14 @@ class Field {
53
38
  this.options = options || {};
54
39
  this.init();
55
40
  }
56
-
57
41
  get name() {
58
42
  return this.options.name;
59
43
  }
60
-
61
44
  get type() {
62
45
  return this.options.type;
63
46
  }
64
-
65
47
  sync(syncOptions) {
66
48
  var _this = this;
67
-
68
49
  return _asyncToGenerator(function* () {
69
50
  yield _this.collection.sync(_objectSpread(_objectSpread({}, syncOptions), {}, {
70
51
  force: false,
@@ -74,91 +55,69 @@ class Field {
74
55
  }));
75
56
  })();
76
57
  }
77
-
78
- init() {// code
58
+ init() {
59
+ // code
79
60
  }
80
-
81
61
  on(eventName, listener) {
82
62
  this.database.on(`${this.collection.name}.${eventName}`, listener);
83
63
  return this;
84
64
  }
85
-
86
65
  off(eventName, listener) {
87
66
  this.database.off(`${this.collection.name}.${eventName}`, listener);
88
67
  return this;
89
68
  }
90
-
91
69
  get(name) {
92
70
  return this.options[name];
93
71
  }
94
-
95
72
  remove() {
96
73
  this.collection.removeIndex([this.name]);
97
74
  return this.collection.removeField(this.name);
98
75
  }
99
-
100
76
  columnName() {
101
77
  if (this.options.field) {
102
78
  return this.options.field;
103
79
  }
104
-
105
80
  if (this.database.options.underscored) {
106
81
  return (0, _utils.snakeCase)(this.name);
107
82
  }
108
-
109
83
  return this.name;
110
84
  }
111
-
112
85
  removeFromDb(options) {
113
86
  var _this2 = this;
114
-
115
87
  return _asyncToGenerator(function* () {
116
88
  const attribute = _this2.collection.model.rawAttributes[_this2.name];
117
-
118
89
  if (!attribute) {
119
- _this2.remove(); // console.log('field is not attribute');
120
-
121
-
90
+ _this2.remove();
91
+ // console.log('field is not attribute');
122
92
  return;
123
93
  }
124
-
125
94
  if (_this2.collection.isInherited() && _this2.collection.parentFields().has(_this2.name)) {
126
95
  return;
127
96
  }
128
-
129
97
  if (_this2.collection.model._virtualAttributes.has(_this2.name)) {
130
- _this2.remove(); // console.log('field is virtual attribute');
131
-
132
-
98
+ _this2.remove();
99
+ // console.log('field is virtual attribute');
133
100
  return;
134
101
  }
135
-
136
102
  if (_this2.collection.model.primaryKeyAttributes.includes(_this2.name)) {
137
103
  // 主键不能删除
138
104
  return;
139
105
  }
140
-
141
106
  if (_this2.collection.model.options.timestamps !== false) {
142
107
  // timestamps 相关字段不删除
143
108
  let timestampsFields = ['createdAt', 'updatedAt', 'deletedAt'];
144
-
145
109
  if (_this2.database.options.underscored) {
146
110
  timestampsFields = timestampsFields.map(field => (0, _utils.snakeCase)(field));
147
111
  }
148
-
149
112
  if (timestampsFields.includes(_this2.columnName())) {
150
113
  _this2.collection.fields.delete(_this2.name);
151
-
152
114
  return;
153
115
  }
154
- } // 排序字段通过 sortable 控制
155
-
156
-
116
+ }
117
+ // 排序字段通过 sortable 控制
157
118
  const sortable = _this2.collection.options.sortable;
158
-
159
119
  if (sortable) {
160
120
  let sortField;
161
-
162
121
  if (sortable === true) {
163
122
  sortField = 'sort';
164
123
  } else if (typeof sortable === 'string') {
@@ -166,108 +125,96 @@ class Field {
166
125
  } else if (sortable.name) {
167
126
  sortField = sortable.name || 'sort';
168
127
  }
169
-
170
128
  if (_this2.name === sortField) {
171
129
  return;
172
130
  }
173
- } // if (this.options.field && this.name !== this.options.field) {
131
+ }
132
+ // if (this.options.field && this.name !== this.options.field) {
174
133
  // // field 指向的是真实的字段名,如果与 name 不一样,说明字段只是引用
175
134
  // this.remove();
176
135
  // return;
177
136
  // }
178
-
179
-
137
+ if (_this2.collection.isView()) {
138
+ _this2.remove();
139
+ return;
140
+ }
180
141
  const columnReferencesCount = _lodash().default.filter(_this2.collection.model.rawAttributes, attr => attr.field == _this2.columnName()).length;
181
-
182
142
  if ((yield _this2.existsInDb({
183
143
  transaction: options === null || options === void 0 ? void 0 : options.transaction
184
144
  })) && columnReferencesCount == 1) {
185
145
  const queryInterface = _this2.database.sequelize.getQueryInterface();
186
-
187
- yield queryInterface.removeColumn(_this2.collection.addSchemaTableName(), _this2.columnName(), options);
146
+ yield queryInterface.removeColumn(_this2.collection.getTableNameWithSchema(), _this2.columnName(), options);
188
147
  }
189
-
190
148
  _this2.remove();
191
149
  })();
192
150
  }
193
-
194
151
  existsInDb(options) {
195
152
  var _this3 = this;
196
-
197
153
  return _asyncToGenerator(function* () {
198
154
  const opts = {
199
155
  transaction: options === null || options === void 0 ? void 0 : options.transaction
200
156
  };
201
157
  let sql;
202
-
203
158
  if (_this3.database.sequelize.getDialect() === 'sqlite') {
204
- sql = `SELECT * from pragma_table_info('${_this3.collection.model.tableName}') WHERE name = '${_this3.columnName()}'`;
159
+ sql = `SELECT *
160
+ from pragma_table_info('${_this3.collection.model.tableName}')
161
+ WHERE name = '${_this3.columnName()}'`;
205
162
  } else if (_this3.database.inDialect('mysql')) {
206
163
  sql = `
207
164
  select column_name
208
165
  from INFORMATION_SCHEMA.COLUMNS
209
- where TABLE_SCHEMA='${_this3.database.options.database}' AND TABLE_NAME='${_this3.collection.model.tableName}' AND column_name='${_this3.columnName()}'
166
+ where TABLE_SCHEMA = '${_this3.database.options.database}'
167
+ AND TABLE_NAME = '${_this3.collection.model.tableName}'
168
+ AND column_name = '${_this3.columnName()}'
210
169
  `;
211
170
  } else {
212
171
  sql = `
213
172
  select column_name
214
173
  from INFORMATION_SCHEMA.COLUMNS
215
- where TABLE_NAME='${_this3.collection.model.tableName}' AND column_name='${_this3.columnName()}' AND table_schema='${_this3.collection.collectionSchema() || 'public'}'
174
+ where TABLE_NAME = '${_this3.collection.model.tableName}'
175
+ AND column_name = '${_this3.columnName()}'
176
+ AND table_schema = '${_this3.collection.collectionSchema() || 'public'}'
216
177
  `;
217
178
  }
218
-
219
179
  const _yield$_this3$databas = yield _this3.database.sequelize.query(sql, opts),
220
- _yield$_this3$databas2 = _slicedToArray(_yield$_this3$databas, 1),
221
- rows = _yield$_this3$databas2[0];
222
-
180
+ _yield$_this3$databas2 = _slicedToArray(_yield$_this3$databas, 1),
181
+ rows = _yield$_this3$databas2[0];
223
182
  return rows.length > 0;
224
183
  })();
225
184
  }
226
-
227
185
  merge(obj) {
228
186
  Object.assign(this.options, obj);
229
187
  }
230
-
231
188
  bind() {
232
189
  const model = this.context.collection.model;
233
- model.rawAttributes[this.name] = this.toSequelize(); // @ts-ignore
234
-
190
+ model.rawAttributes[this.name] = this.toSequelize();
191
+ // @ts-ignore
235
192
  model.refreshAttributes();
236
-
237
193
  if (this.options.index) {
238
194
  this.context.collection.addIndex([this.name]);
239
195
  }
240
196
  }
241
-
242
197
  unbind() {
243
198
  const model = this.context.collection.model;
244
199
  model.removeAttribute(this.name);
245
-
246
200
  if (this.options.index || this.options.unique) {
247
201
  this.context.collection.removeIndex([this.name]);
248
202
  }
249
203
  }
250
-
251
204
  toSequelize() {
252
205
  const opts = _lodash().default.omit(this.options, ['name']);
253
-
254
206
  if (this.dataType) {
255
207
  Object.assign(opts, {
256
208
  type: this.dataType
257
209
  });
258
210
  }
259
-
260
211
  return opts;
261
212
  }
262
-
263
213
  isSqlite() {
264
214
  return this.database.sequelize.getDialect() === 'sqlite';
265
215
  }
266
-
267
216
  typeToString() {
268
217
  return this.dataType.toString();
269
218
  }
270
-
271
219
  }
272
-
273
220
  exports.Field = Field;