@nocobase/database 0.7.0-alpha.4 → 0.7.0-alpha.58

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 (298) hide show
  1. package/lib/collection-importer.js +101 -67
  2. package/lib/collection.js +339 -210
  3. package/lib/database.d.ts +4 -1
  4. package/lib/database.js +489 -275
  5. package/lib/fields/array-field.js +45 -25
  6. package/lib/fields/belongs-to-field.js +101 -54
  7. package/lib/fields/belongs-to-many-field.js +98 -53
  8. package/lib/fields/boolean-field.js +24 -9
  9. package/lib/fields/context-field.js +77 -42
  10. package/lib/fields/date-field.js +24 -9
  11. package/lib/fields/field.js +114 -75
  12. package/lib/fields/has-inverse-field.js +4 -2
  13. package/lib/fields/has-many-field.js +105 -56
  14. package/lib/fields/has-one-field.js +105 -54
  15. package/lib/fields/index.d.ts +3 -1
  16. package/lib/fields/index.js +277 -32
  17. package/lib/fields/json-field.js +36 -16
  18. package/lib/fields/number-field.js +53 -26
  19. package/lib/fields/password-field.js +118 -73
  20. package/lib/fields/radio-field.js +75 -47
  21. package/lib/fields/relation-field.js +41 -28
  22. package/lib/fields/sort-field.js +165 -89
  23. package/lib/fields/string-field.js +24 -9
  24. package/lib/fields/text-field.js +24 -9
  25. package/lib/fields/time-field.js +24 -9
  26. package/lib/fields/uid-field.js +57 -28
  27. package/lib/fields/uuid-field.d.ts +9 -0
  28. package/lib/fields/uuid-field.js +39 -0
  29. package/lib/fields/virtual-field.js +24 -9
  30. package/lib/filter-parser.js +288 -179
  31. package/lib/index.js +210 -29
  32. package/lib/magic-attribute-model.js +123 -71
  33. package/lib/mock-database.js +68 -34
  34. package/lib/model-hook.js +101 -60
  35. package/lib/model.js +116 -81
  36. package/lib/operators/array.js +136 -96
  37. package/lib/operators/association.js +30 -14
  38. package/lib/operators/boolean.d.ts +13 -0
  39. package/lib/operators/boolean.js +35 -0
  40. package/lib/operators/date.js +78 -34
  41. package/lib/operators/empty.js +113 -75
  42. package/lib/operators/index.js +15 -3
  43. package/lib/operators/ne.js +27 -12
  44. package/{esm/operators/ne.d.ts → lib/operators/notIn.d.ts} +2 -2
  45. package/lib/operators/notIn.js +29 -0
  46. package/lib/operators/string.js +56 -35
  47. package/lib/operators/utils.js +18 -10
  48. package/lib/options-parser.js +323 -215
  49. package/lib/playground.js +66 -53
  50. package/lib/relation-repository/belongs-to-many-repository.js +281 -198
  51. package/lib/relation-repository/belongs-to-repository.js +10 -6
  52. package/lib/relation-repository/hasmany-repository.js +168 -121
  53. package/lib/relation-repository/hasone-repository.js +10 -6
  54. package/lib/relation-repository/multiple-relation-repository.d.ts +3 -3
  55. package/lib/relation-repository/multiple-relation-repository.js +263 -148
  56. package/lib/relation-repository/relation-repository.d.ts +1 -1
  57. package/lib/relation-repository/relation-repository.js +155 -95
  58. package/lib/relation-repository/single-relation-repository.d.ts +6 -6
  59. package/lib/relation-repository/single-relation-repository.js +138 -99
  60. package/lib/relation-repository/types.js +4 -2
  61. package/lib/repository.d.ts +4 -7
  62. package/lib/repository.js +465 -291
  63. package/lib/transaction-decorator.js +80 -67
  64. package/lib/update-associations.d.ts +1 -2
  65. package/lib/update-associations.js +525 -321
  66. package/lib/update-guard.js +160 -117
  67. package/package.json +3 -8
  68. package/src/__tests__/fields/uuid-field.test.ts +30 -0
  69. package/src/__tests__/operator/notIn.test.ts +33 -0
  70. package/src/__tests__/option-parser.test.ts +20 -0
  71. package/src/database.ts +51 -4
  72. package/src/fields/context-field.ts +1 -1
  73. package/src/fields/index.ts +3 -0
  74. package/src/fields/uuid-field.ts +21 -0
  75. package/src/mock-database.ts +1 -1
  76. package/src/model-hook.ts +1 -1
  77. package/src/operators/boolean.ts +18 -0
  78. package/src/operators/index.ts +2 -0
  79. package/src/operators/notIn.ts +12 -0
  80. package/src/options-parser.ts +1 -1
  81. package/src/relation-repository/multiple-relation-repository.ts +8 -6
  82. package/src/relation-repository/relation-repository.ts +11 -6
  83. package/src/relation-repository/single-relation-repository.ts +6 -6
  84. package/src/repository.ts +4 -6
  85. package/src/update-associations.ts +1 -2
  86. package/esm/collection-importer.d.ts +0 -7
  87. package/esm/collection-importer.js +0 -49
  88. package/esm/collection-importer.js.map +0 -1
  89. package/esm/collection.d.ts +0 -73
  90. package/esm/collection.js +0 -224
  91. package/esm/collection.js.map +0 -1
  92. package/esm/database.d.ts +0 -101
  93. package/esm/database.js +0 -275
  94. package/esm/database.js.map +0 -1
  95. package/esm/fields/array-field.d.ts +0 -11
  96. package/esm/fields/array-field.js +0 -26
  97. package/esm/fields/array-field.js.map +0 -1
  98. package/esm/fields/belongs-to-field.d.ts +0 -12
  99. package/esm/fields/belongs-to-field.js +0 -57
  100. package/esm/fields/belongs-to-field.js.map +0 -1
  101. package/esm/fields/belongs-to-many-field.d.ts +0 -11
  102. package/esm/fields/belongs-to-many-field.js +0 -55
  103. package/esm/fields/belongs-to-many-field.js.map +0 -1
  104. package/esm/fields/boolean-field.d.ts +0 -8
  105. package/esm/fields/boolean-field.js +0 -8
  106. package/esm/fields/boolean-field.js.map +0 -1
  107. package/esm/fields/context-field.d.ts +0 -13
  108. package/esm/fields/context-field.js +0 -43
  109. package/esm/fields/context-field.js.map +0 -1
  110. package/esm/fields/date-field.d.ts +0 -8
  111. package/esm/fields/date-field.js +0 -8
  112. package/esm/fields/date-field.js.map +0 -1
  113. package/esm/fields/field.d.ts +0 -37
  114. package/esm/fields/field.js +0 -74
  115. package/esm/fields/field.js.map +0 -1
  116. package/esm/fields/has-inverse-field.d.ts +0 -4
  117. package/esm/fields/has-inverse-field.js +0 -2
  118. package/esm/fields/has-inverse-field.js.map +0 -1
  119. package/esm/fields/has-many-field.d.ts +0 -64
  120. package/esm/fields/has-many-field.js +0 -58
  121. package/esm/fields/has-many-field.js.map +0 -1
  122. package/esm/fields/has-one-field.d.ts +0 -64
  123. package/esm/fields/has-one-field.js +0 -57
  124. package/esm/fields/has-one-field.js.map +0 -1
  125. package/esm/fields/index.d.ts +0 -40
  126. package/esm/fields/index.js +0 -21
  127. package/esm/fields/index.js.map +0 -1
  128. package/esm/fields/json-field.d.ts +0 -14
  129. package/esm/fields/json-field.js +0 -17
  130. package/esm/fields/json-field.js.map +0 -1
  131. package/esm/fields/number-field.d.ts +0 -32
  132. package/esm/fields/number-field.js +0 -28
  133. package/esm/fields/number-field.js.map +0 -1
  134. package/esm/fields/password-field.d.ts +0 -21
  135. package/esm/fields/password-field.js +0 -71
  136. package/esm/fields/password-field.js.map +0 -1
  137. package/esm/fields/radio-field.d.ts +0 -14
  138. package/esm/fields/radio-field.js +0 -49
  139. package/esm/fields/radio-field.js.map +0 -1
  140. package/esm/fields/relation-field.d.ts +0 -20
  141. package/esm/fields/relation-field.js +0 -27
  142. package/esm/fields/relation-field.js.map +0 -1
  143. package/esm/fields/sort-field.d.ts +0 -16
  144. package/esm/fields/sort-field.js +0 -90
  145. package/esm/fields/sort-field.js.map +0 -1
  146. package/esm/fields/string-field.d.ts +0 -8
  147. package/esm/fields/string-field.js +0 -8
  148. package/esm/fields/string-field.js.map +0 -1
  149. package/esm/fields/text-field.d.ts +0 -8
  150. package/esm/fields/text-field.js +0 -8
  151. package/esm/fields/text-field.js.map +0 -1
  152. package/esm/fields/time-field.d.ts +0 -8
  153. package/esm/fields/time-field.js +0 -8
  154. package/esm/fields/time-field.js.map +0 -1
  155. package/esm/fields/uid-field.d.ts +0 -10
  156. package/esm/fields/uid-field.js +0 -27
  157. package/esm/fields/uid-field.js.map +0 -1
  158. package/esm/fields/virtual-field.d.ts +0 -8
  159. package/esm/fields/virtual-field.js +0 -8
  160. package/esm/fields/virtual-field.js.map +0 -1
  161. package/esm/filter-parser.d.ts +0 -27
  162. package/esm/filter-parser.js +0 -185
  163. package/esm/filter-parser.js.map +0 -1
  164. package/esm/index.d.ts +0 -15
  165. package/esm/index.js +0 -16
  166. package/esm/index.js.map +0 -1
  167. package/esm/magic-attribute-model.d.ts +0 -7
  168. package/esm/magic-attribute-model.js +0 -70
  169. package/esm/magic-attribute-model.js.map +0 -1
  170. package/esm/mock-database.d.ts +0 -22
  171. package/esm/mock-database.js +0 -34
  172. package/esm/mock-database.js.map +0 -1
  173. package/esm/model-hook.d.ts +0 -12
  174. package/esm/model-hook.js +0 -61
  175. package/esm/model-hook.js.map +0 -1
  176. package/esm/model.d.ts +0 -15
  177. package/esm/model.js +0 -80
  178. package/esm/model.js.map +0 -1
  179. package/esm/operators/array.d.ts +0 -26
  180. package/esm/operators/array.js +0 -105
  181. package/esm/operators/array.js.map +0 -1
  182. package/esm/operators/association.d.ts +0 -10
  183. package/esm/operators/association.js +0 -14
  184. package/esm/operators/association.js.map +0 -1
  185. package/esm/operators/date.d.ts +0 -34
  186. package/esm/operators/date.js +0 -35
  187. package/esm/operators/date.js.map +0 -1
  188. package/esm/operators/empty.d.ts +0 -28
  189. package/esm/operators/empty.js +0 -58
  190. package/esm/operators/empty.js.map +0 -1
  191. package/esm/operators/index.d.ts +0 -2
  192. package/esm/operators/index.js +0 -2
  193. package/esm/operators/index.js.map +0 -1
  194. package/esm/operators/ne.js +0 -12
  195. package/esm/operators/ne.js.map +0 -1
  196. package/esm/operators/string.d.ts +0 -21
  197. package/esm/operators/string.js +0 -35
  198. package/esm/operators/string.js.map +0 -1
  199. package/esm/operators/utils.d.ts +0 -4
  200. package/esm/operators/utils.js +0 -11
  201. package/esm/operators/utils.js.map +0 -1
  202. package/esm/options-parser.d.ts +0 -31
  203. package/esm/options-parser.js +0 -225
  204. package/esm/options-parser.js.map +0 -1
  205. package/esm/playground.d.ts +0 -1
  206. package/esm/playground.js +0 -53
  207. package/esm/playground.js.map +0 -1
  208. package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
  209. package/esm/relation-repository/belongs-to-many-repository.js +0 -199
  210. package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
  211. package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
  212. package/esm/relation-repository/belongs-to-repository.js +0 -4
  213. package/esm/relation-repository/belongs-to-repository.js.map +0 -1
  214. package/esm/relation-repository/hasmany-repository.d.ts +0 -23
  215. package/esm/relation-repository/hasmany-repository.js +0 -125
  216. package/esm/relation-repository/hasmany-repository.js.map +0 -1
  217. package/esm/relation-repository/hasone-repository.d.ts +0 -17
  218. package/esm/relation-repository/hasone-repository.js +0 -4
  219. package/esm/relation-repository/hasone-repository.js.map +0 -1
  220. package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
  221. package/esm/relation-repository/multiple-relation-repository.js +0 -149
  222. package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
  223. package/esm/relation-repository/relation-repository.d.ts +0 -32
  224. package/esm/relation-repository/relation-repository.js +0 -93
  225. package/esm/relation-repository/relation-repository.js.map +0 -1
  226. package/esm/relation-repository/single-relation-repository.d.ts +0 -23
  227. package/esm/relation-repository/single-relation-repository.js +0 -96
  228. package/esm/relation-repository/single-relation-repository.js.map +0 -1
  229. package/esm/relation-repository/types.d.ts +0 -7
  230. package/esm/relation-repository/types.js +0 -2
  231. package/esm/relation-repository/types.js.map +0 -1
  232. package/esm/repository.d.ts +0 -165
  233. package/esm/repository.js +0 -276
  234. package/esm/repository.js.map +0 -1
  235. package/esm/transaction-decorator.d.ts +0 -1
  236. package/esm/transaction-decorator.js +0 -63
  237. package/esm/transaction-decorator.js.map +0 -1
  238. package/esm/update-associations.d.ts +0 -60
  239. package/esm/update-associations.js +0 -362
  240. package/esm/update-associations.js.map +0 -1
  241. package/esm/update-guard.d.ts +0 -26
  242. package/esm/update-guard.js +0 -122
  243. package/esm/update-guard.js.map +0 -1
  244. package/lib/collection-importer.js.map +0 -1
  245. package/lib/collection.js.map +0 -1
  246. package/lib/database.js.map +0 -1
  247. package/lib/fields/array-field.js.map +0 -1
  248. package/lib/fields/belongs-to-field.js.map +0 -1
  249. package/lib/fields/belongs-to-many-field.js.map +0 -1
  250. package/lib/fields/boolean-field.js.map +0 -1
  251. package/lib/fields/context-field.js.map +0 -1
  252. package/lib/fields/date-field.js.map +0 -1
  253. package/lib/fields/field.js.map +0 -1
  254. package/lib/fields/has-inverse-field.js.map +0 -1
  255. package/lib/fields/has-many-field.js.map +0 -1
  256. package/lib/fields/has-one-field.js.map +0 -1
  257. package/lib/fields/index.js.map +0 -1
  258. package/lib/fields/json-field.js.map +0 -1
  259. package/lib/fields/number-field.js.map +0 -1
  260. package/lib/fields/password-field.js.map +0 -1
  261. package/lib/fields/radio-field.js.map +0 -1
  262. package/lib/fields/relation-field.js.map +0 -1
  263. package/lib/fields/sort-field.js.map +0 -1
  264. package/lib/fields/string-field.js.map +0 -1
  265. package/lib/fields/text-field.js.map +0 -1
  266. package/lib/fields/time-field.js.map +0 -1
  267. package/lib/fields/uid-field.js.map +0 -1
  268. package/lib/fields/virtual-field.js.map +0 -1
  269. package/lib/filter-parser.js.map +0 -1
  270. package/lib/index.js.map +0 -1
  271. package/lib/magic-attribute-model.js.map +0 -1
  272. package/lib/mock-database.js.map +0 -1
  273. package/lib/model-hook.js.map +0 -1
  274. package/lib/model.js.map +0 -1
  275. package/lib/operators/array.js.map +0 -1
  276. package/lib/operators/association.js.map +0 -1
  277. package/lib/operators/date.js.map +0 -1
  278. package/lib/operators/empty.js.map +0 -1
  279. package/lib/operators/index.js.map +0 -1
  280. package/lib/operators/ne.js.map +0 -1
  281. package/lib/operators/string.js.map +0 -1
  282. package/lib/operators/utils.js.map +0 -1
  283. package/lib/options-parser.js.map +0 -1
  284. package/lib/playground.js.map +0 -1
  285. package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
  286. package/lib/relation-repository/belongs-to-repository.js.map +0 -1
  287. package/lib/relation-repository/hasmany-repository.js.map +0 -1
  288. package/lib/relation-repository/hasone-repository.js.map +0 -1
  289. package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
  290. package/lib/relation-repository/relation-repository.js.map +0 -1
  291. package/lib/relation-repository/single-relation-repository.js.map +0 -1
  292. package/lib/relation-repository/types.js.map +0 -1
  293. package/lib/repository.js.map +0 -1
  294. package/lib/transaction-decorator.js.map +0 -1
  295. package/lib/update-associations.js.map +0 -1
  296. package/lib/update-guard.js.map +0 -1
  297. package/tsconfig.build.json +0 -9
  298. package/tsconfig.json +0 -5
@@ -1,232 +1,340 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
6
  exports.OptionsParser = void 0;
7
- const sequelize_1 = require("sequelize");
8
- const filter_parser_1 = __importDefault(require("./filter-parser"));
7
+
8
+ function _sequelize() {
9
+ const data = require("sequelize");
10
+
11
+ _sequelize = function _sequelize() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ var _filterParser = _interopRequireDefault(require("./filter-parser"));
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
22
+ 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; } } }; }
23
+
24
+ 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); }
25
+
26
+ 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; }
27
+
28
+ 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; }
29
+
30
+ 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; }
31
+
32
+ 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; }
33
+
9
34
  const debug = require('debug')('noco-database');
35
+
10
36
  class OptionsParser {
11
- constructor(options, context) {
12
- const { collection } = context;
13
- this.collection = collection;
14
- this.model = collection.model;
15
- this.options = options;
16
- this.database = collection.context.database;
17
- this.filterParser = new filter_parser_1.default(options === null || options === void 0 ? void 0 : options.filter, {
18
- collection,
19
- app: {
20
- ctx: options === null || options === void 0 ? void 0 : options.context,
21
- },
22
- });
23
- this.context = context;
37
+ constructor(options, context) {
38
+ this.options = void 0;
39
+ this.database = void 0;
40
+ this.collection = void 0;
41
+ this.model = void 0;
42
+ this.filterParser = void 0;
43
+ this.context = void 0;
44
+ const collection = context.collection;
45
+ this.collection = collection;
46
+ this.model = collection.model;
47
+ this.options = options;
48
+ this.database = collection.context.database;
49
+ this.filterParser = new _filterParser.default(options === null || options === void 0 ? void 0 : options.filter, {
50
+ collection,
51
+ app: {
52
+ ctx: options === null || options === void 0 ? void 0 : options.context
53
+ }
54
+ });
55
+ this.context = context;
56
+ }
57
+
58
+ isAssociation(key) {
59
+ return this.model.associations[key] !== undefined;
60
+ }
61
+
62
+ isAssociationPath(path) {
63
+ return this.isAssociation(path.split('.')[0]);
64
+ }
65
+
66
+ toSequelizeParams() {
67
+ var _this$options;
68
+
69
+ const queryParams = this.filterParser.toSequelizeParams();
70
+
71
+ if ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.filterByTk) {
72
+ queryParams.where = {
73
+ [_sequelize().Op.and]: [queryParams.where, {
74
+ [this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk
75
+ }]
76
+ };
24
77
  }
25
- isAssociation(key) {
26
- return this.model.associations[key] !== undefined;
78
+
79
+ return this.parseSort(this.parseFields(queryParams));
80
+ }
81
+ /**
82
+ * parser sort options
83
+ * @param filterParams
84
+ * @protected
85
+ */
86
+
87
+
88
+ parseSort(filterParams) {
89
+ var _this$options2;
90
+
91
+ let sort = ((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.sort) || [];
92
+
93
+ if (typeof sort === 'string') {
94
+ sort = sort.split(',');
27
95
  }
28
- isAssociationPath(path) {
29
- return this.isAssociation(path.split('.')[0]);
30
- }
31
- toSequelizeParams() {
32
- var _a;
33
- const queryParams = this.filterParser.toSequelizeParams();
34
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.filterByTk) {
35
- queryParams.where = {
36
- [sequelize_1.Op.and]: [
37
- queryParams.where,
38
- {
39
- [this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk,
40
- },
41
- ],
42
- };
96
+
97
+ const orderParams = sort.map(sortKey => {
98
+ const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';
99
+ const sortField = sortKey.replace('-', '').split('.'); // handle sort by association
100
+
101
+ if (sortField.length > 1) {
102
+ let associationModel = this.model;
103
+
104
+ for (let i = 0; i < sortField.length - 1; i++) {
105
+ const associationKey = sortField[i];
106
+ sortField[i] = associationModel.associations[associationKey].target;
107
+ associationModel = sortField[i];
43
108
  }
44
- return this.parseSort(this.parseFields(queryParams));
109
+ }
110
+
111
+ sortField.push(direction);
112
+ return sortField;
113
+ });
114
+
115
+ if (orderParams.length > 0) {
116
+ return _objectSpread({
117
+ order: orderParams
118
+ }, filterParams);
45
119
  }
46
- /**
47
- * parser sort options
48
- * @param filterParams
49
- * @protected
50
- */
51
- parseSort(filterParams) {
52
- var _a;
53
- let sort = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.sort) || [];
54
- if (typeof sort === 'string') {
55
- sort = sort.split(',');
56
- }
57
- const orderParams = sort.map((sortKey) => {
58
- const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';
59
- const sortField = sortKey.replace('-', '').split('.');
60
- // handle sort by association
61
- if (sortField.length > 1) {
62
- let associationModel = this.model;
63
- for (let i = 0; i < sortField.length - 1; i++) {
64
- const associationKey = sortField[i];
65
- sortField[i] = associationModel.associations[associationKey].target;
66
- associationModel = sortField[i];
67
- }
68
- }
69
- sortField.push(direction);
70
- return sortField;
71
- });
72
- if (orderParams.length > 0) {
73
- return Object.assign({ order: orderParams }, filterParams);
120
+
121
+ return filterParams;
122
+ }
123
+
124
+ parseFields(filterParams) {
125
+ var _this$options3, _this$options4, _this$options5;
126
+
127
+ const appends = ((_this$options3 = this.options) === null || _this$options3 === void 0 ? void 0 : _this$options3.appends) || [];
128
+ const except = [];
129
+ let attributes = {
130
+ include: [],
131
+ exclude: []
132
+ }; // out put all fields by default
133
+
134
+ if ((_this$options4 = this.options) === null || _this$options4 === void 0 ? void 0 : _this$options4.fields) {
135
+ // 将fields拆分为 attributes appends
136
+ var _iterator = _createForOfIteratorHelper(this.options.fields),
137
+ _step;
138
+
139
+ try {
140
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
141
+ const field = _step.value;
142
+
143
+ if (this.isAssociationPath(field)) {
144
+ // field is association field
145
+ appends.push(field);
146
+ } else {
147
+ // field is model attribute, change attributes to array type
148
+ if (!Array.isArray(attributes)) attributes = [];
149
+ attributes.push(field);
150
+ }
74
151
  }
75
- return filterParams;
152
+ } catch (err) {
153
+ _iterator.e(err);
154
+ } finally {
155
+ _iterator.f();
156
+ }
76
157
  }
77
- parseFields(filterParams) {
78
- var _a, _b, _c;
79
- const appends = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.appends) || [];
80
- const except = [];
81
- let attributes = {
82
- include: [],
83
- exclude: [],
84
- }; // out put all fields by default
85
- if ((_b = this.options) === null || _b === void 0 ? void 0 : _b.fields) {
86
- // 将fields拆分为 attributes 和 appends
87
- for (const field of this.options.fields) {
88
- if (this.isAssociationPath(field)) {
89
- // field is association field
90
- appends.push(field);
91
- }
92
- else {
93
- // field is model attribute, change attributes to array type
94
- if (!Array.isArray(attributes))
95
- attributes = [];
96
- attributes.push(field);
97
- }
98
- }
99
- }
100
- if ((_c = this.options) === null || _c === void 0 ? void 0 : _c.except) {
101
- for (const exceptKey of this.options.except) {
102
- if (this.isAssociationPath(exceptKey)) {
103
- // except association field
104
- except.push(exceptKey);
105
- }
106
- else {
107
- // if attributes is array form, ignore except
108
- if (Array.isArray(attributes))
109
- continue;
110
- attributes.exclude.push(exceptKey);
111
- }
112
- }
158
+
159
+ if ((_this$options5 = this.options) === null || _this$options5 === void 0 ? void 0 : _this$options5.except) {
160
+ var _iterator2 = _createForOfIteratorHelper(this.options.except),
161
+ _step2;
162
+
163
+ try {
164
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
165
+ const exceptKey = _step2.value;
166
+
167
+ if (this.isAssociationPath(exceptKey)) {
168
+ // except association field
169
+ except.push(exceptKey);
170
+ } else {
171
+ // if attributes is array form, ignore except
172
+ if (Array.isArray(attributes)) continue;
173
+ attributes.exclude.push(exceptKey);
174
+ }
113
175
  }
114
- return Object.assign({ attributes }, this.parseExcept(except, this.parseAppends(appends, filterParams)));
176
+ } catch (err) {
177
+ _iterator2.e(err);
178
+ } finally {
179
+ _iterator2.f();
180
+ }
115
181
  }
116
- parseExcept(except, filterParams) {
117
- if (!except)
118
- return filterParams;
119
- const setExcept = (queryParams, except) => {
120
- // split exceptKey to path form
121
- // posts.comments.content => ['posts', 'comments', 'content']
122
- // then set except on include attributes
123
- const exceptPath = except.split('.');
124
- const association = exceptPath[0];
125
- const lastLevel = exceptPath.length <= 2;
126
- let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == association);
127
- if (existIncludeIndex == -1) {
128
- // if include not exists, ignore this except
129
- return;
130
- }
131
- if (lastLevel) {
132
- // if it not have exclude form
133
- if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {
134
- return;
135
- }
136
- else {
137
- if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {
138
- queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];
139
- }
140
- queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);
141
- }
142
- }
143
- else {
144
- setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));
145
- }
146
- };
147
- for (const exceptKey of except) {
148
- setExcept(filterParams, exceptKey);
182
+
183
+ return _objectSpread({
184
+ attributes
185
+ }, this.parseExcept(except, this.parseAppends(appends, filterParams)));
186
+ }
187
+
188
+ parseExcept(except, filterParams) {
189
+ if (!except) return filterParams;
190
+
191
+ const setExcept = (queryParams, except) => {
192
+ // split exceptKey to path form
193
+ // posts.comments.content => ['posts', 'comments', 'content']
194
+ // then set except on include attributes
195
+ const exceptPath = except.split('.');
196
+ const association = exceptPath[0];
197
+ const lastLevel = exceptPath.length <= 2;
198
+ let existIncludeIndex = queryParams['include'].findIndex(include => include['association'] == association);
199
+
200
+ if (existIncludeIndex == -1) {
201
+ // if include not exists, ignore this except
202
+ return;
203
+ }
204
+
205
+ if (lastLevel) {
206
+ // if it not have exclude form
207
+ if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {
208
+ return;
209
+ } else {
210
+ if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {
211
+ queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];
212
+ }
213
+
214
+ queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);
149
215
  }
150
- return filterParams;
216
+ } else {
217
+ setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));
218
+ }
219
+ };
220
+
221
+ var _iterator3 = _createForOfIteratorHelper(except),
222
+ _step3;
223
+
224
+ try {
225
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
226
+ const exceptKey = _step3.value;
227
+ setExcept(filterParams, exceptKey);
228
+ }
229
+ } catch (err) {
230
+ _iterator3.e(err);
231
+ } finally {
232
+ _iterator3.f();
151
233
  }
152
- parseAppends(appends, filterParams) {
153
- if (!appends)
154
- return filterParams;
155
- /**
156
- * set include params
157
- * @param includeRoot
158
- * @param appends
159
- */
160
- const setInclude = (model, queryParams, append) => {
161
- const appendFields = append.split('.');
162
- const appendAssociation = appendFields[0];
163
- const associations = model.associations;
164
- // if append length less or equal 2
165
- // example:
166
- // appends: ['posts']
167
- // appends: ['posts.title']
168
- // All of these can be seen as last level
169
- let lastLevel = false;
170
- if (appendFields.length == 1) {
171
- lastLevel = true;
172
- }
173
- if (appendFields.length == 2) {
174
- const associationModel = associations[appendFields[0]].target;
175
- if (associationModel.rawAttributes[appendFields[1]]) {
176
- lastLevel = true;
177
- }
178
- }
179
- // find association index
180
- if (queryParams['include'] == undefined) {
181
- queryParams['include'] = [];
182
- }
183
- let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == appendAssociation);
184
- // if association not exist, create it
185
- if (existIncludeIndex == -1) {
186
- // association not exists
187
- queryParams['include'].push({
188
- association: appendAssociation,
189
- });
190
- existIncludeIndex = 0;
191
- }
192
- // end appends
193
- // without nests association
194
- if (lastLevel) {
195
- // get exist association attributes
196
- let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
197
- include: [], // all fields are output by default
198
- };
199
- // if need set attribute
200
- if (appendFields.length == 2) {
201
- if (!Array.isArray(attributes)) {
202
- attributes = [];
203
- }
204
- const attributeName = appendFields[1];
205
- // push field to it
206
- attributes.push(attributeName);
207
- }
208
- else {
209
- // if attributes is empty array, change it to object
210
- if (Array.isArray(attributes) && attributes.length == 0) {
211
- attributes = {
212
- include: [],
213
- };
214
- }
215
- }
216
- // set new attributes
217
- queryParams['include'][existIncludeIndex] = Object.assign(Object.assign({}, queryParams['include'][existIncludeIndex]), { attributes });
218
- }
219
- else {
220
- setInclude(model.associations[queryParams['include'][existIncludeIndex].association].target, queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
221
- }
222
- };
223
- // handle every appends
224
- for (const append of appends) {
225
- setInclude(this.model, filterParams, append);
234
+
235
+ return filterParams;
236
+ }
237
+
238
+ parseAppends(appends, filterParams) {
239
+ if (!appends) return filterParams;
240
+ /**
241
+ * set include params
242
+ * @param includeRoot
243
+ * @param appends
244
+ */
245
+
246
+ const setInclude = (model, queryParams, append) => {
247
+ const appendFields = append.split('.');
248
+ const appendAssociation = appendFields[0];
249
+ const associations = model.associations; // if append length less or equal 2
250
+ // example:
251
+ // appends: ['posts']
252
+ // appends: ['posts.title']
253
+ // All of these can be seen as last level
254
+
255
+ let lastLevel = false;
256
+
257
+ if (appendFields.length == 1) {
258
+ lastLevel = true;
259
+ }
260
+
261
+ if (appendFields.length == 2) {
262
+ const associationModel = associations[appendFields[0]].target;
263
+
264
+ if (associationModel.rawAttributes[appendFields[1]]) {
265
+ lastLevel = true;
226
266
  }
227
- debug('filter params: %o', filterParams);
228
- return filterParams;
267
+ } // find association index
268
+
269
+
270
+ if (queryParams['include'] == undefined) {
271
+ queryParams['include'] = [];
272
+ }
273
+
274
+ let existIncludeIndex = queryParams['include'].findIndex(include => include['association'] == appendAssociation); // if association not exist, create it
275
+
276
+ if (existIncludeIndex == -1) {
277
+ // association not exists
278
+ queryParams['include'].push({
279
+ association: appendAssociation
280
+ });
281
+ existIncludeIndex = queryParams['include'].length - 1;
282
+ } // end appends
283
+ // without nests association
284
+
285
+
286
+ if (lastLevel) {
287
+ // get exist association attributes
288
+ let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
289
+ include: [] // all fields are output by default
290
+
291
+ }; // if need set attribute
292
+
293
+ if (appendFields.length == 2) {
294
+ if (!Array.isArray(attributes)) {
295
+ attributes = [];
296
+ }
297
+
298
+ const attributeName = appendFields[1]; // push field to it
299
+
300
+ attributes.push(attributeName);
301
+ } else {
302
+ // if attributes is empty array, change it to object
303
+ if (Array.isArray(attributes) && attributes.length == 0) {
304
+ attributes = {
305
+ include: []
306
+ };
307
+ }
308
+ } // set new attributes
309
+
310
+
311
+ queryParams['include'][existIncludeIndex] = _objectSpread(_objectSpread({}, queryParams['include'][existIncludeIndex]), {}, {
312
+ attributes
313
+ });
314
+ } else {
315
+ setInclude(model.associations[queryParams['include'][existIncludeIndex].association].target, queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
316
+ }
317
+ }; // handle every appends
318
+
319
+
320
+ var _iterator4 = _createForOfIteratorHelper(appends),
321
+ _step4;
322
+
323
+ try {
324
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
325
+ const append = _step4.value;
326
+ setInclude(this.model, filterParams, append);
327
+ }
328
+ } catch (err) {
329
+ _iterator4.e(err);
330
+ } finally {
331
+ _iterator4.f();
229
332
  }
333
+
334
+ debug('filter params: %o', filterParams);
335
+ return filterParams;
336
+ }
337
+
230
338
  }
231
- exports.OptionsParser = OptionsParser;
232
- //# sourceMappingURL=options-parser.js.map
339
+
340
+ exports.OptionsParser = OptionsParser;