@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.
Files changed (280) hide show
  1. package/lib/collection-importer.js +101 -67
  2. package/lib/collection.js +339 -210
  3. package/lib/database.js +488 -304
  4. package/lib/fields/array-field.js +45 -25
  5. package/lib/fields/belongs-to-field.js +101 -54
  6. package/lib/fields/belongs-to-many-field.js +98 -53
  7. package/lib/fields/boolean-field.js +24 -9
  8. package/lib/fields/context-field.js +77 -45
  9. package/lib/fields/date-field.js +24 -9
  10. package/lib/fields/field.js +114 -75
  11. package/lib/fields/has-inverse-field.js +4 -2
  12. package/lib/fields/has-many-field.js +105 -56
  13. package/lib/fields/has-one-field.js +105 -54
  14. package/lib/fields/index.js +277 -33
  15. package/lib/fields/json-field.js +36 -16
  16. package/lib/fields/number-field.js +53 -26
  17. package/lib/fields/password-field.js +118 -73
  18. package/lib/fields/radio-field.js +75 -47
  19. package/lib/fields/relation-field.js +41 -28
  20. package/lib/fields/sort-field.js +165 -89
  21. package/lib/fields/string-field.js +24 -9
  22. package/lib/fields/text-field.js +24 -9
  23. package/lib/fields/time-field.js +24 -9
  24. package/lib/fields/uid-field.js +57 -28
  25. package/lib/fields/uuid-field.js +36 -12
  26. package/lib/fields/virtual-field.js +24 -9
  27. package/lib/filter-parser.js +288 -179
  28. package/lib/index.js +210 -29
  29. package/lib/magic-attribute-model.js +123 -71
  30. package/lib/mock-database.js +68 -34
  31. package/lib/model-hook.js +101 -59
  32. package/lib/model.js +116 -81
  33. package/lib/operators/array.js +136 -96
  34. package/lib/operators/association.js +30 -14
  35. package/lib/operators/date.js +78 -34
  36. package/lib/operators/empty.js +113 -75
  37. package/lib/operators/index.js +15 -3
  38. package/lib/operators/ne.js +27 -12
  39. package/lib/operators/notIn.js +27 -12
  40. package/lib/operators/string.js +56 -35
  41. package/lib/operators/utils.js +18 -10
  42. package/lib/options-parser.js +323 -215
  43. package/lib/playground.js +66 -53
  44. package/lib/relation-repository/belongs-to-many-repository.js +281 -198
  45. package/lib/relation-repository/belongs-to-repository.js +10 -6
  46. package/lib/relation-repository/hasmany-repository.js +168 -121
  47. package/lib/relation-repository/hasone-repository.js +10 -6
  48. package/lib/relation-repository/multiple-relation-repository.js +263 -148
  49. package/lib/relation-repository/relation-repository.js +155 -97
  50. package/lib/relation-repository/single-relation-repository.js +138 -99
  51. package/lib/relation-repository/types.js +4 -2
  52. package/lib/repository.js +465 -291
  53. package/lib/transaction-decorator.js +80 -67
  54. package/lib/update-associations.js +525 -321
  55. package/lib/update-guard.js +160 -117
  56. package/package.json +3 -8
  57. package/src/database.ts +9 -3
  58. package/src/mock-database.ts +1 -1
  59. package/esm/collection-importer.d.ts +0 -7
  60. package/esm/collection-importer.js +0 -49
  61. package/esm/collection-importer.js.map +0 -1
  62. package/esm/collection.d.ts +0 -73
  63. package/esm/collection.js +0 -224
  64. package/esm/collection.js.map +0 -1
  65. package/esm/database.d.ts +0 -104
  66. package/esm/database.js +0 -305
  67. package/esm/database.js.map +0 -1
  68. package/esm/fields/array-field.d.ts +0 -11
  69. package/esm/fields/array-field.js +0 -26
  70. package/esm/fields/array-field.js.map +0 -1
  71. package/esm/fields/belongs-to-field.d.ts +0 -12
  72. package/esm/fields/belongs-to-field.js +0 -57
  73. package/esm/fields/belongs-to-field.js.map +0 -1
  74. package/esm/fields/belongs-to-many-field.d.ts +0 -11
  75. package/esm/fields/belongs-to-many-field.js +0 -55
  76. package/esm/fields/belongs-to-many-field.js.map +0 -1
  77. package/esm/fields/boolean-field.d.ts +0 -8
  78. package/esm/fields/boolean-field.js +0 -8
  79. package/esm/fields/boolean-field.js.map +0 -1
  80. package/esm/fields/context-field.d.ts +0 -13
  81. package/esm/fields/context-field.js +0 -43
  82. package/esm/fields/context-field.js.map +0 -1
  83. package/esm/fields/date-field.d.ts +0 -8
  84. package/esm/fields/date-field.js +0 -8
  85. package/esm/fields/date-field.js.map +0 -1
  86. package/esm/fields/field.d.ts +0 -37
  87. package/esm/fields/field.js +0 -74
  88. package/esm/fields/field.js.map +0 -1
  89. package/esm/fields/has-inverse-field.d.ts +0 -4
  90. package/esm/fields/has-inverse-field.js +0 -2
  91. package/esm/fields/has-inverse-field.js.map +0 -1
  92. package/esm/fields/has-many-field.d.ts +0 -64
  93. package/esm/fields/has-many-field.js +0 -58
  94. package/esm/fields/has-many-field.js.map +0 -1
  95. package/esm/fields/has-one-field.d.ts +0 -64
  96. package/esm/fields/has-one-field.js +0 -57
  97. package/esm/fields/has-one-field.js.map +0 -1
  98. package/esm/fields/index.d.ts +0 -42
  99. package/esm/fields/index.js +0 -22
  100. package/esm/fields/index.js.map +0 -1
  101. package/esm/fields/json-field.d.ts +0 -14
  102. package/esm/fields/json-field.js +0 -17
  103. package/esm/fields/json-field.js.map +0 -1
  104. package/esm/fields/number-field.d.ts +0 -32
  105. package/esm/fields/number-field.js +0 -28
  106. package/esm/fields/number-field.js.map +0 -1
  107. package/esm/fields/password-field.d.ts +0 -21
  108. package/esm/fields/password-field.js +0 -71
  109. package/esm/fields/password-field.js.map +0 -1
  110. package/esm/fields/radio-field.d.ts +0 -14
  111. package/esm/fields/radio-field.js +0 -49
  112. package/esm/fields/radio-field.js.map +0 -1
  113. package/esm/fields/relation-field.d.ts +0 -20
  114. package/esm/fields/relation-field.js +0 -27
  115. package/esm/fields/relation-field.js.map +0 -1
  116. package/esm/fields/sort-field.d.ts +0 -16
  117. package/esm/fields/sort-field.js +0 -90
  118. package/esm/fields/sort-field.js.map +0 -1
  119. package/esm/fields/string-field.d.ts +0 -8
  120. package/esm/fields/string-field.js +0 -8
  121. package/esm/fields/string-field.js.map +0 -1
  122. package/esm/fields/text-field.d.ts +0 -8
  123. package/esm/fields/text-field.js +0 -8
  124. package/esm/fields/text-field.js.map +0 -1
  125. package/esm/fields/time-field.d.ts +0 -8
  126. package/esm/fields/time-field.js +0 -8
  127. package/esm/fields/time-field.js.map +0 -1
  128. package/esm/fields/uid-field.d.ts +0 -10
  129. package/esm/fields/uid-field.js +0 -27
  130. package/esm/fields/uid-field.js.map +0 -1
  131. package/esm/fields/uuid-field.d.ts +0 -9
  132. package/esm/fields/uuid-field.js +0 -11
  133. package/esm/fields/uuid-field.js.map +0 -1
  134. package/esm/fields/virtual-field.d.ts +0 -8
  135. package/esm/fields/virtual-field.js +0 -8
  136. package/esm/fields/virtual-field.js.map +0 -1
  137. package/esm/filter-parser.d.ts +0 -27
  138. package/esm/filter-parser.js +0 -185
  139. package/esm/filter-parser.js.map +0 -1
  140. package/esm/index.d.ts +0 -15
  141. package/esm/index.js +0 -16
  142. package/esm/index.js.map +0 -1
  143. package/esm/magic-attribute-model.d.ts +0 -7
  144. package/esm/magic-attribute-model.js +0 -70
  145. package/esm/magic-attribute-model.js.map +0 -1
  146. package/esm/mock-database.d.ts +0 -22
  147. package/esm/mock-database.js +0 -34
  148. package/esm/mock-database.js.map +0 -1
  149. package/esm/model-hook.d.ts +0 -12
  150. package/esm/model-hook.js +0 -60
  151. package/esm/model-hook.js.map +0 -1
  152. package/esm/model.d.ts +0 -15
  153. package/esm/model.js +0 -80
  154. package/esm/model.js.map +0 -1
  155. package/esm/operators/array.d.ts +0 -26
  156. package/esm/operators/array.js +0 -105
  157. package/esm/operators/array.js.map +0 -1
  158. package/esm/operators/association.d.ts +0 -10
  159. package/esm/operators/association.js +0 -14
  160. package/esm/operators/association.js.map +0 -1
  161. package/esm/operators/date.d.ts +0 -34
  162. package/esm/operators/date.js +0 -35
  163. package/esm/operators/date.js.map +0 -1
  164. package/esm/operators/empty.d.ts +0 -28
  165. package/esm/operators/empty.js +0 -58
  166. package/esm/operators/empty.js.map +0 -1
  167. package/esm/operators/index.d.ts +0 -2
  168. package/esm/operators/index.js +0 -2
  169. package/esm/operators/index.js.map +0 -1
  170. package/esm/operators/ne.d.ts +0 -10
  171. package/esm/operators/ne.js +0 -12
  172. package/esm/operators/ne.js.map +0 -1
  173. package/esm/operators/notIn.d.ts +0 -10
  174. package/esm/operators/notIn.js +0 -12
  175. package/esm/operators/notIn.js.map +0 -1
  176. package/esm/operators/string.d.ts +0 -21
  177. package/esm/operators/string.js +0 -35
  178. package/esm/operators/string.js.map +0 -1
  179. package/esm/operators/utils.d.ts +0 -4
  180. package/esm/operators/utils.js +0 -11
  181. package/esm/operators/utils.js.map +0 -1
  182. package/esm/options-parser.d.ts +0 -31
  183. package/esm/options-parser.js +0 -225
  184. package/esm/options-parser.js.map +0 -1
  185. package/esm/playground.d.ts +0 -1
  186. package/esm/playground.js +0 -53
  187. package/esm/playground.js.map +0 -1
  188. package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
  189. package/esm/relation-repository/belongs-to-many-repository.js +0 -199
  190. package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
  191. package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
  192. package/esm/relation-repository/belongs-to-repository.js +0 -4
  193. package/esm/relation-repository/belongs-to-repository.js.map +0 -1
  194. package/esm/relation-repository/hasmany-repository.d.ts +0 -23
  195. package/esm/relation-repository/hasmany-repository.js +0 -125
  196. package/esm/relation-repository/hasmany-repository.js.map +0 -1
  197. package/esm/relation-repository/hasone-repository.d.ts +0 -17
  198. package/esm/relation-repository/hasone-repository.js +0 -4
  199. package/esm/relation-repository/hasone-repository.js.map +0 -1
  200. package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
  201. package/esm/relation-repository/multiple-relation-repository.js +0 -149
  202. package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
  203. package/esm/relation-repository/relation-repository.d.ts +0 -32
  204. package/esm/relation-repository/relation-repository.js +0 -95
  205. package/esm/relation-repository/relation-repository.js.map +0 -1
  206. package/esm/relation-repository/single-relation-repository.d.ts +0 -23
  207. package/esm/relation-repository/single-relation-repository.js +0 -96
  208. package/esm/relation-repository/single-relation-repository.js.map +0 -1
  209. package/esm/relation-repository/types.d.ts +0 -7
  210. package/esm/relation-repository/types.js +0 -2
  211. package/esm/relation-repository/types.js.map +0 -1
  212. package/esm/repository.d.ts +0 -165
  213. package/esm/repository.js +0 -276
  214. package/esm/repository.js.map +0 -1
  215. package/esm/transaction-decorator.d.ts +0 -1
  216. package/esm/transaction-decorator.js +0 -63
  217. package/esm/transaction-decorator.js.map +0 -1
  218. package/esm/update-associations.d.ts +0 -60
  219. package/esm/update-associations.js +0 -362
  220. package/esm/update-associations.js.map +0 -1
  221. package/esm/update-guard.d.ts +0 -26
  222. package/esm/update-guard.js +0 -122
  223. package/esm/update-guard.js.map +0 -1
  224. package/lib/collection-importer.js.map +0 -1
  225. package/lib/collection.js.map +0 -1
  226. package/lib/database.js.map +0 -1
  227. package/lib/fields/array-field.js.map +0 -1
  228. package/lib/fields/belongs-to-field.js.map +0 -1
  229. package/lib/fields/belongs-to-many-field.js.map +0 -1
  230. package/lib/fields/boolean-field.js.map +0 -1
  231. package/lib/fields/context-field.js.map +0 -1
  232. package/lib/fields/date-field.js.map +0 -1
  233. package/lib/fields/field.js.map +0 -1
  234. package/lib/fields/has-inverse-field.js.map +0 -1
  235. package/lib/fields/has-many-field.js.map +0 -1
  236. package/lib/fields/has-one-field.js.map +0 -1
  237. package/lib/fields/index.js.map +0 -1
  238. package/lib/fields/json-field.js.map +0 -1
  239. package/lib/fields/number-field.js.map +0 -1
  240. package/lib/fields/password-field.js.map +0 -1
  241. package/lib/fields/radio-field.js.map +0 -1
  242. package/lib/fields/relation-field.js.map +0 -1
  243. package/lib/fields/sort-field.js.map +0 -1
  244. package/lib/fields/string-field.js.map +0 -1
  245. package/lib/fields/text-field.js.map +0 -1
  246. package/lib/fields/time-field.js.map +0 -1
  247. package/lib/fields/uid-field.js.map +0 -1
  248. package/lib/fields/uuid-field.js.map +0 -1
  249. package/lib/fields/virtual-field.js.map +0 -1
  250. package/lib/filter-parser.js.map +0 -1
  251. package/lib/index.js.map +0 -1
  252. package/lib/magic-attribute-model.js.map +0 -1
  253. package/lib/mock-database.js.map +0 -1
  254. package/lib/model-hook.js.map +0 -1
  255. package/lib/model.js.map +0 -1
  256. package/lib/operators/array.js.map +0 -1
  257. package/lib/operators/association.js.map +0 -1
  258. package/lib/operators/date.js.map +0 -1
  259. package/lib/operators/empty.js.map +0 -1
  260. package/lib/operators/index.js.map +0 -1
  261. package/lib/operators/ne.js.map +0 -1
  262. package/lib/operators/notIn.js.map +0 -1
  263. package/lib/operators/string.js.map +0 -1
  264. package/lib/operators/utils.js.map +0 -1
  265. package/lib/options-parser.js.map +0 -1
  266. package/lib/playground.js.map +0 -1
  267. package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
  268. package/lib/relation-repository/belongs-to-repository.js.map +0 -1
  269. package/lib/relation-repository/hasmany-repository.js.map +0 -1
  270. package/lib/relation-repository/hasone-repository.js.map +0 -1
  271. package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
  272. package/lib/relation-repository/relation-repository.js.map +0 -1
  273. package/lib/relation-repository/single-relation-repository.js.map +0 -1
  274. package/lib/relation-repository/types.js.map +0 -1
  275. package/lib/repository.js.map +0 -1
  276. package/lib/transaction-decorator.js.map +0 -1
  277. package/lib/update-associations.js.map +0 -1
  278. package/lib/update-guard.js.map +0 -1
  279. package/tsconfig.build.json +0 -9
  280. package/tsconfig.json +0 -5
package/lib/collection.js CHANGED
@@ -1,231 +1,360 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __rest = (this && this.__rest) || function (s, e) {
12
- var t = {};
13
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
- t[p] = s[p];
15
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
- t[p[i]] = s[p[i]];
19
- }
20
- return t;
21
- };
22
- var __importDefault = (this && this.__importDefault) || function (mod) {
23
- return (mod && mod.__esModule) ? mod : { "default": mod };
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
26
6
  exports.Collection = void 0;
27
- const deepmerge_1 = __importDefault(require("deepmerge"));
28
- const events_1 = require("events");
29
- const lodash_1 = __importDefault(require("lodash"));
30
- const model_1 = require("./model");
31
- const repository_1 = require("./repository");
32
- class Collection extends events_1.EventEmitter {
33
- constructor(options, context) {
34
- super();
35
- this.fields = new Map();
36
- this.context = context;
37
- this.options = options;
38
- this.bindFieldEventListener();
39
- this.modelInit();
40
- this.setFields(options.fields);
41
- this.setRepository(options.repository);
42
- this.setSortable(options.sortable);
43
- }
44
- get filterTargetKey() {
45
- return lodash_1.default.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
46
- }
47
- get name() {
48
- return this.options.name;
49
- }
50
- sequelizeModelOptions() {
51
- const { name, tableName } = this.options;
52
- return Object.assign(Object.assign({}, lodash_1.default.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), { modelName: name, sequelize: this.context.database.sequelize, tableName: tableName || name });
7
+
8
+ function _deepmerge() {
9
+ const data = _interopRequireDefault(require("deepmerge"));
10
+
11
+ _deepmerge = function _deepmerge() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ function _events() {
19
+ const data = require("events");
20
+
21
+ _events = function _events() {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
27
+
28
+ function _lodash() {
29
+ const data = _interopRequireDefault(require("lodash"));
30
+
31
+ _lodash = function _lodash() {
32
+ return data;
33
+ };
34
+
35
+ return data;
36
+ }
37
+
38
+ var _model = require("./model");
39
+
40
+ var _repository = require("./repository");
41
+
42
+ const _excluded = ["name"],
43
+ _excluded2 = ["name"];
44
+
45
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
46
+
47
+ 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); } }
48
+
49
+ 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); }); }; }
50
+
51
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
52
+
53
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
54
+
55
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
56
+
57
+ 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); }
58
+
59
+ 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; }
60
+
61
+ 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; }
62
+
63
+ 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; }
64
+
65
+ 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; }
66
+
67
+ class Collection extends _events().EventEmitter {
68
+ get filterTargetKey() {
69
+ return _lodash().default.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
70
+ }
71
+
72
+ get name() {
73
+ return this.options.name;
74
+ }
75
+
76
+ constructor(options, context) {
77
+ super();
78
+ this.options = void 0;
79
+ this.context = void 0;
80
+ this.isThrough = void 0;
81
+ this.fields = new Map();
82
+ this.model = void 0;
83
+ this.repository = void 0;
84
+ this.context = context;
85
+ this.options = options;
86
+ this.bindFieldEventListener();
87
+ this.modelInit();
88
+ this.setFields(options.fields);
89
+ this.setRepository(options.repository);
90
+ this.setSortable(options.sortable);
91
+ }
92
+
93
+ sequelizeModelOptions() {
94
+ const _this$options = this.options,
95
+ name = _this$options.name,
96
+ tableName = _this$options.tableName;
97
+ return _objectSpread(_objectSpread({}, _lodash().default.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), {}, {
98
+ modelName: name,
99
+ sequelize: this.context.database.sequelize,
100
+ tableName: tableName || name
101
+ });
102
+ }
103
+ /**
104
+ * TODO
105
+ */
106
+
107
+
108
+ modelInit() {
109
+ if (this.model) {
110
+ return;
53
111
  }
54
- /**
55
- * TODO
56
- */
57
- modelInit() {
58
- if (this.model) {
59
- return;
60
- }
61
- const { name, model, autoGenId = true } = this.options;
62
- let M = model_1.Model;
63
- if (this.context.database.sequelize.isDefined(name)) {
64
- const m = this.context.database.sequelize.model(name);
65
- if (m.isThrough) {
66
- // @ts-ignore
67
- this.model = m;
68
- // @ts-ignore
69
- this.model.database = this.context.database;
70
- // @ts-ignore
71
- this.model.collection = this;
72
- return;
73
- }
74
- }
75
- if (typeof model === 'string') {
76
- M = this.context.database.models.get(model) || model_1.Model;
77
- }
78
- else if (model) {
79
- M = model;
80
- }
81
- // @ts-ignore
82
- this.model = class extends M {
83
- };
84
- this.model.init(null, this.sequelizeModelOptions());
85
- if (!autoGenId) {
86
- this.model.removeAttribute('id');
87
- }
88
- // @ts-ignore
89
- this.model.database = this.context.database;
112
+
113
+ const _this$options2 = this.options,
114
+ name = _this$options2.name,
115
+ model = _this$options2.model,
116
+ _this$options2$autoGe = _this$options2.autoGenId,
117
+ autoGenId = _this$options2$autoGe === void 0 ? true : _this$options2$autoGe;
118
+ let M = _model.Model;
119
+
120
+ if (this.context.database.sequelize.isDefined(name)) {
121
+ const m = this.context.database.sequelize.model(name);
122
+
123
+ if (m.isThrough) {
90
124
  // @ts-ignore
125
+ this.model = m; // @ts-ignore
126
+
127
+ this.model.database = this.context.database; // @ts-ignore
128
+
91
129
  this.model.collection = this;
130
+ return;
131
+ }
92
132
  }
93
- setRepository(repository) {
94
- let repo = repository_1.Repository;
95
- if (typeof repository === 'string') {
96
- repo = this.context.database.repositories.get(repository) || repository_1.Repository;
97
- }
98
- this.repository = new repo(this);
133
+
134
+ if (typeof model === 'string') {
135
+ M = this.context.database.models.get(model) || _model.Model;
136
+ } else if (model) {
137
+ M = model;
138
+ } // @ts-ignore
139
+
140
+
141
+ this.model = class extends M {};
142
+ this.model.init(null, this.sequelizeModelOptions());
143
+
144
+ if (!autoGenId) {
145
+ this.model.removeAttribute('id');
146
+ } // @ts-ignore
147
+
148
+
149
+ this.model.database = this.context.database; // @ts-ignore
150
+
151
+ this.model.collection = this;
152
+ }
153
+
154
+ setRepository(repository) {
155
+ let repo = _repository.Repository;
156
+
157
+ if (typeof repository === 'string') {
158
+ repo = this.context.database.repositories.get(repository) || _repository.Repository;
99
159
  }
100
- bindFieldEventListener() {
101
- this.on('field.afterAdd', (field) => {
102
- field.bind();
103
- });
104
- this.on('field.afterRemove', (field) => field.unbind());
160
+
161
+ this.repository = new repo(this);
162
+ }
163
+
164
+ bindFieldEventListener() {
165
+ this.on('field.afterAdd', field => {
166
+ field.bind();
167
+ });
168
+ this.on('field.afterRemove', field => field.unbind());
169
+ }
170
+
171
+ forEachField(callback) {
172
+ return [...this.fields.values()].forEach(callback);
173
+ }
174
+
175
+ findField(callback) {
176
+ return [...this.fields.values()].find(callback);
177
+ }
178
+
179
+ hasField(name) {
180
+ return this.fields.has(name);
181
+ }
182
+
183
+ getField(name) {
184
+ return this.fields.get(name);
185
+ }
186
+
187
+ addField(name, options) {
188
+ return this.setField(name, options);
189
+ }
190
+
191
+ setField(name, options) {
192
+ const database = this.context.database;
193
+ const field = database.buildField(_objectSpread({
194
+ name
195
+ }, options), _objectSpread(_objectSpread({}, this.context), {}, {
196
+ collection: this
197
+ }));
198
+ this.removeField(name);
199
+ this.fields.set(name, field);
200
+ this.emit('field.afterAdd', field);
201
+ return field;
202
+ }
203
+
204
+ setFields(fields, resetFields = true) {
205
+ if (!Array.isArray(fields)) {
206
+ return;
105
207
  }
106
- forEachField(callback) {
107
- return [...this.fields.values()].forEach(callback);
208
+
209
+ if (resetFields) {
210
+ this.resetFields();
108
211
  }
109
- findField(callback) {
110
- return [...this.fields.values()].find(callback);
212
+
213
+ var _iterator = _createForOfIteratorHelper(fields),
214
+ _step;
215
+
216
+ try {
217
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
218
+ const _ref = _step.value;
219
+
220
+ const name = _ref.name,
221
+ options = _objectWithoutProperties(_ref, _excluded);
222
+
223
+ this.addField(name, options);
224
+ }
225
+ } catch (err) {
226
+ _iterator.e(err);
227
+ } finally {
228
+ _iterator.f();
111
229
  }
112
- hasField(name) {
113
- return this.fields.has(name);
230
+ }
231
+
232
+ resetFields() {
233
+ const fieldNames = this.fields.keys();
234
+
235
+ var _iterator2 = _createForOfIteratorHelper(fieldNames),
236
+ _step2;
237
+
238
+ try {
239
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
240
+ const fieldName = _step2.value;
241
+ this.removeField(fieldName);
242
+ }
243
+ } catch (err) {
244
+ _iterator2.e(err);
245
+ } finally {
246
+ _iterator2.f();
114
247
  }
115
- getField(name) {
116
- return this.fields.get(name);
248
+ }
249
+
250
+ removeField(name) {
251
+ if (!this.fields.has(name)) {
252
+ return;
117
253
  }
118
- addField(name, options) {
119
- return this.setField(name, options);
254
+
255
+ const field = this.fields.get(name);
256
+ const bool = this.fields.delete(name);
257
+
258
+ if (bool) {
259
+ this.emit('field.afterRemove', field);
120
260
  }
121
- setField(name, options) {
122
- const { database } = this.context;
123
- const field = database.buildField(Object.assign({ name }, options), Object.assign(Object.assign({}, this.context), { collection: this }));
124
- this.removeField(name);
125
- this.fields.set(name, field);
126
- this.emit('field.afterAdd', field);
127
- return field;
261
+
262
+ return bool;
263
+ }
264
+ /**
265
+ * TODO
266
+ */
267
+
268
+
269
+ updateOptions(options, mergeOptions) {
270
+ let newOptions = _lodash().default.cloneDeep(options);
271
+
272
+ newOptions = (0, _deepmerge().default)(this.options, newOptions, mergeOptions);
273
+ this.context.database.emit('beforeUpdateCollection', this, newOptions);
274
+ this.setFields(options.fields, false);
275
+ this.setRepository(options.repository);
276
+ this.context.database.emit('afterUpdateCollection', this);
277
+ return this;
278
+ }
279
+
280
+ setSortable(sortable) {
281
+ if (!sortable) {
282
+ return;
128
283
  }
129
- setFields(fields, resetFields = true) {
130
- if (!Array.isArray(fields)) {
131
- return;
132
- }
133
- if (resetFields) {
134
- this.resetFields();
135
- }
136
- for (let _a of fields) {
137
- const { name } = _a, options = __rest(_a, ["name"]);
138
- this.addField(name, options);
139
- }
284
+
285
+ if (sortable === true) {
286
+ this.setField('sort', {
287
+ type: 'sort',
288
+ hidden: true
289
+ });
140
290
  }
141
- resetFields() {
142
- const fieldNames = this.fields.keys();
143
- for (const fieldName of fieldNames) {
144
- this.removeField(fieldName);
145
- }
291
+
292
+ if (typeof sortable === 'string') {
293
+ this.setField(sortable, {
294
+ type: 'sort',
295
+ hidden: true
296
+ });
297
+ } else if (typeof sortable === 'object') {
298
+ const name = sortable.name,
299
+ opts = _objectWithoutProperties(sortable, _excluded2);
300
+
301
+ this.setField(name || 'sort', _objectSpread({
302
+ type: 'sort',
303
+ hidden: true
304
+ }, opts));
146
305
  }
147
- removeField(name) {
148
- if (!this.fields.has(name)) {
149
- return;
150
- }
151
- const field = this.fields.get(name);
152
- const bool = this.fields.delete(name);
153
- if (bool) {
154
- this.emit('field.afterRemove', field);
155
- }
156
- return bool;
306
+ }
307
+ /**
308
+ * TODO
309
+ *
310
+ * @param name
311
+ * @param options
312
+ */
313
+
314
+
315
+ updateField(name, options) {
316
+ if (!this.hasField(name)) {
317
+ throw new Error(`field ${name} not exists`);
157
318
  }
158
- /**
159
- * TODO
160
- */
161
- updateOptions(options, mergeOptions) {
162
- let newOptions = lodash_1.default.cloneDeep(options);
163
- newOptions = (0, deepmerge_1.default)(this.options, newOptions, mergeOptions);
164
- this.context.database.emit('beforeUpdateCollection', this, newOptions);
165
- this.setFields(options.fields, false);
166
- this.setRepository(options.repository);
167
- this.context.database.emit('afterUpdateCollection', this);
168
- return this;
319
+
320
+ if (options.name && options.name !== name) {
321
+ this.removeField(name);
169
322
  }
170
- setSortable(sortable) {
171
- if (!sortable) {
172
- return;
323
+
324
+ this.setField(options.name || name, options);
325
+ }
326
+
327
+ sync(syncOptions) {
328
+ var _this = this;
329
+
330
+ return _asyncToGenerator(function* () {
331
+ const modelNames = [_this.model.name];
332
+ const associations = _this.model.associations;
333
+
334
+ for (const associationKey in associations) {
335
+ const association = associations[associationKey];
336
+ modelNames.push(association.target.name);
337
+
338
+ if (association.through) {
339
+ modelNames.push(association.through.model.name);
173
340
  }
174
- if (sortable === true) {
175
- this.setField('sort', {
176
- type: 'sort',
177
- hidden: true,
178
- });
341
+ }
342
+
343
+ const models = []; // @ts-ignore
344
+
345
+ _this.context.database.sequelize.modelManager.forEachModel(model => {
346
+ if (modelNames.includes(model.name)) {
347
+ models.push(model);
179
348
  }
180
- if (typeof sortable === 'string') {
181
- this.setField(sortable, {
182
- type: 'sort',
183
- hidden: true,
184
- });
185
- }
186
- else if (typeof sortable === 'object') {
187
- const { name } = sortable, opts = __rest(sortable, ["name"]);
188
- this.setField(name || 'sort', Object.assign({ type: 'sort', hidden: true }, opts));
189
- }
190
- }
191
- /**
192
- * TODO
193
- *
194
- * @param name
195
- * @param options
196
- */
197
- updateField(name, options) {
198
- if (!this.hasField(name)) {
199
- throw new Error(`field ${name} not exists`);
200
- }
201
- if (options.name && options.name !== name) {
202
- this.removeField(name);
203
- }
204
- this.setField(options.name || name, options);
205
- }
206
- sync(syncOptions) {
207
- return __awaiter(this, void 0, void 0, function* () {
208
- const modelNames = [this.model.name];
209
- const associations = this.model.associations;
210
- for (const associationKey in associations) {
211
- const association = associations[associationKey];
212
- modelNames.push(association.target.name);
213
- if (association.through) {
214
- modelNames.push(association.through.model.name);
215
- }
216
- }
217
- const models = [];
218
- // @ts-ignore
219
- this.context.database.sequelize.modelManager.forEachModel((model) => {
220
- if (modelNames.includes(model.name)) {
221
- models.push(model);
222
- }
223
- });
224
- for (const model of models) {
225
- yield model.sync(syncOptions);
226
- }
227
- });
228
- }
349
+ });
350
+
351
+ for (var _i = 0, _models = models; _i < _models.length; _i++) {
352
+ const model = _models[_i];
353
+ yield model.sync(syncOptions);
354
+ }
355
+ })();
356
+ }
357
+
229
358
  }
230
- exports.Collection = Collection;
231
- //# sourceMappingURL=collection.js.map
359
+
360
+ exports.Collection = Collection;