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

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 (290) 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/date.js +78 -34
  39. package/lib/operators/empty.js +113 -75
  40. package/lib/operators/index.js +15 -3
  41. package/lib/operators/ne.js +27 -12
  42. package/{esm/operators/ne.d.ts → lib/operators/notIn.d.ts} +2 -2
  43. package/lib/operators/notIn.js +29 -0
  44. package/lib/operators/string.js +56 -35
  45. package/lib/operators/utils.js +18 -10
  46. package/lib/options-parser.js +323 -215
  47. package/lib/playground.js +66 -53
  48. package/lib/relation-repository/belongs-to-many-repository.js +281 -198
  49. package/lib/relation-repository/belongs-to-repository.js +10 -6
  50. package/lib/relation-repository/hasmany-repository.js +168 -121
  51. package/lib/relation-repository/hasone-repository.js +10 -6
  52. package/lib/relation-repository/multiple-relation-repository.js +263 -148
  53. package/lib/relation-repository/relation-repository.d.ts +1 -1
  54. package/lib/relation-repository/relation-repository.js +155 -95
  55. package/lib/relation-repository/single-relation-repository.js +138 -99
  56. package/lib/relation-repository/types.js +4 -2
  57. package/lib/repository.d.ts +1 -1
  58. package/lib/repository.js +465 -291
  59. package/lib/transaction-decorator.js +80 -67
  60. package/lib/update-associations.js +525 -321
  61. package/lib/update-guard.js +160 -117
  62. package/package.json +3 -8
  63. package/src/__tests__/fields/uuid-field.test.ts +30 -0
  64. package/src/__tests__/operator/notIn.test.ts +33 -0
  65. package/src/__tests__/option-parser.test.ts +20 -0
  66. package/src/database.ts +51 -4
  67. package/src/fields/context-field.ts +1 -1
  68. package/src/fields/index.ts +3 -0
  69. package/src/fields/uuid-field.ts +21 -0
  70. package/src/mock-database.ts +1 -1
  71. package/src/model-hook.ts +1 -1
  72. package/src/operators/index.ts +1 -0
  73. package/src/operators/notIn.ts +12 -0
  74. package/src/options-parser.ts +1 -1
  75. package/src/relation-repository/multiple-relation-repository.ts +6 -3
  76. package/src/relation-repository/relation-repository.ts +11 -6
  77. package/src/repository.ts +1 -1
  78. package/esm/collection-importer.d.ts +0 -7
  79. package/esm/collection-importer.js +0 -49
  80. package/esm/collection-importer.js.map +0 -1
  81. package/esm/collection.d.ts +0 -73
  82. package/esm/collection.js +0 -224
  83. package/esm/collection.js.map +0 -1
  84. package/esm/database.d.ts +0 -101
  85. package/esm/database.js +0 -275
  86. package/esm/database.js.map +0 -1
  87. package/esm/fields/array-field.d.ts +0 -11
  88. package/esm/fields/array-field.js +0 -26
  89. package/esm/fields/array-field.js.map +0 -1
  90. package/esm/fields/belongs-to-field.d.ts +0 -12
  91. package/esm/fields/belongs-to-field.js +0 -57
  92. package/esm/fields/belongs-to-field.js.map +0 -1
  93. package/esm/fields/belongs-to-many-field.d.ts +0 -11
  94. package/esm/fields/belongs-to-many-field.js +0 -55
  95. package/esm/fields/belongs-to-many-field.js.map +0 -1
  96. package/esm/fields/boolean-field.d.ts +0 -8
  97. package/esm/fields/boolean-field.js +0 -8
  98. package/esm/fields/boolean-field.js.map +0 -1
  99. package/esm/fields/context-field.d.ts +0 -13
  100. package/esm/fields/context-field.js +0 -43
  101. package/esm/fields/context-field.js.map +0 -1
  102. package/esm/fields/date-field.d.ts +0 -8
  103. package/esm/fields/date-field.js +0 -8
  104. package/esm/fields/date-field.js.map +0 -1
  105. package/esm/fields/field.d.ts +0 -37
  106. package/esm/fields/field.js +0 -74
  107. package/esm/fields/field.js.map +0 -1
  108. package/esm/fields/has-inverse-field.d.ts +0 -4
  109. package/esm/fields/has-inverse-field.js +0 -2
  110. package/esm/fields/has-inverse-field.js.map +0 -1
  111. package/esm/fields/has-many-field.d.ts +0 -64
  112. package/esm/fields/has-many-field.js +0 -58
  113. package/esm/fields/has-many-field.js.map +0 -1
  114. package/esm/fields/has-one-field.d.ts +0 -64
  115. package/esm/fields/has-one-field.js +0 -57
  116. package/esm/fields/has-one-field.js.map +0 -1
  117. package/esm/fields/index.d.ts +0 -40
  118. package/esm/fields/index.js +0 -21
  119. package/esm/fields/index.js.map +0 -1
  120. package/esm/fields/json-field.d.ts +0 -14
  121. package/esm/fields/json-field.js +0 -17
  122. package/esm/fields/json-field.js.map +0 -1
  123. package/esm/fields/number-field.d.ts +0 -32
  124. package/esm/fields/number-field.js +0 -28
  125. package/esm/fields/number-field.js.map +0 -1
  126. package/esm/fields/password-field.d.ts +0 -21
  127. package/esm/fields/password-field.js +0 -71
  128. package/esm/fields/password-field.js.map +0 -1
  129. package/esm/fields/radio-field.d.ts +0 -14
  130. package/esm/fields/radio-field.js +0 -49
  131. package/esm/fields/radio-field.js.map +0 -1
  132. package/esm/fields/relation-field.d.ts +0 -20
  133. package/esm/fields/relation-field.js +0 -27
  134. package/esm/fields/relation-field.js.map +0 -1
  135. package/esm/fields/sort-field.d.ts +0 -16
  136. package/esm/fields/sort-field.js +0 -90
  137. package/esm/fields/sort-field.js.map +0 -1
  138. package/esm/fields/string-field.d.ts +0 -8
  139. package/esm/fields/string-field.js +0 -8
  140. package/esm/fields/string-field.js.map +0 -1
  141. package/esm/fields/text-field.d.ts +0 -8
  142. package/esm/fields/text-field.js +0 -8
  143. package/esm/fields/text-field.js.map +0 -1
  144. package/esm/fields/time-field.d.ts +0 -8
  145. package/esm/fields/time-field.js +0 -8
  146. package/esm/fields/time-field.js.map +0 -1
  147. package/esm/fields/uid-field.d.ts +0 -10
  148. package/esm/fields/uid-field.js +0 -27
  149. package/esm/fields/uid-field.js.map +0 -1
  150. package/esm/fields/virtual-field.d.ts +0 -8
  151. package/esm/fields/virtual-field.js +0 -8
  152. package/esm/fields/virtual-field.js.map +0 -1
  153. package/esm/filter-parser.d.ts +0 -27
  154. package/esm/filter-parser.js +0 -185
  155. package/esm/filter-parser.js.map +0 -1
  156. package/esm/index.d.ts +0 -15
  157. package/esm/index.js +0 -16
  158. package/esm/index.js.map +0 -1
  159. package/esm/magic-attribute-model.d.ts +0 -7
  160. package/esm/magic-attribute-model.js +0 -70
  161. package/esm/magic-attribute-model.js.map +0 -1
  162. package/esm/mock-database.d.ts +0 -22
  163. package/esm/mock-database.js +0 -34
  164. package/esm/mock-database.js.map +0 -1
  165. package/esm/model-hook.d.ts +0 -12
  166. package/esm/model-hook.js +0 -61
  167. package/esm/model-hook.js.map +0 -1
  168. package/esm/model.d.ts +0 -15
  169. package/esm/model.js +0 -80
  170. package/esm/model.js.map +0 -1
  171. package/esm/operators/array.d.ts +0 -26
  172. package/esm/operators/array.js +0 -105
  173. package/esm/operators/array.js.map +0 -1
  174. package/esm/operators/association.d.ts +0 -10
  175. package/esm/operators/association.js +0 -14
  176. package/esm/operators/association.js.map +0 -1
  177. package/esm/operators/date.d.ts +0 -34
  178. package/esm/operators/date.js +0 -35
  179. package/esm/operators/date.js.map +0 -1
  180. package/esm/operators/empty.d.ts +0 -28
  181. package/esm/operators/empty.js +0 -58
  182. package/esm/operators/empty.js.map +0 -1
  183. package/esm/operators/index.d.ts +0 -2
  184. package/esm/operators/index.js +0 -2
  185. package/esm/operators/index.js.map +0 -1
  186. package/esm/operators/ne.js +0 -12
  187. package/esm/operators/ne.js.map +0 -1
  188. package/esm/operators/string.d.ts +0 -21
  189. package/esm/operators/string.js +0 -35
  190. package/esm/operators/string.js.map +0 -1
  191. package/esm/operators/utils.d.ts +0 -4
  192. package/esm/operators/utils.js +0 -11
  193. package/esm/operators/utils.js.map +0 -1
  194. package/esm/options-parser.d.ts +0 -31
  195. package/esm/options-parser.js +0 -225
  196. package/esm/options-parser.js.map +0 -1
  197. package/esm/playground.d.ts +0 -1
  198. package/esm/playground.js +0 -53
  199. package/esm/playground.js.map +0 -1
  200. package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
  201. package/esm/relation-repository/belongs-to-many-repository.js +0 -199
  202. package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
  203. package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
  204. package/esm/relation-repository/belongs-to-repository.js +0 -4
  205. package/esm/relation-repository/belongs-to-repository.js.map +0 -1
  206. package/esm/relation-repository/hasmany-repository.d.ts +0 -23
  207. package/esm/relation-repository/hasmany-repository.js +0 -125
  208. package/esm/relation-repository/hasmany-repository.js.map +0 -1
  209. package/esm/relation-repository/hasone-repository.d.ts +0 -17
  210. package/esm/relation-repository/hasone-repository.js +0 -4
  211. package/esm/relation-repository/hasone-repository.js.map +0 -1
  212. package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
  213. package/esm/relation-repository/multiple-relation-repository.js +0 -149
  214. package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
  215. package/esm/relation-repository/relation-repository.d.ts +0 -32
  216. package/esm/relation-repository/relation-repository.js +0 -93
  217. package/esm/relation-repository/relation-repository.js.map +0 -1
  218. package/esm/relation-repository/single-relation-repository.d.ts +0 -23
  219. package/esm/relation-repository/single-relation-repository.js +0 -96
  220. package/esm/relation-repository/single-relation-repository.js.map +0 -1
  221. package/esm/relation-repository/types.d.ts +0 -7
  222. package/esm/relation-repository/types.js +0 -2
  223. package/esm/relation-repository/types.js.map +0 -1
  224. package/esm/repository.d.ts +0 -165
  225. package/esm/repository.js +0 -276
  226. package/esm/repository.js.map +0 -1
  227. package/esm/transaction-decorator.d.ts +0 -1
  228. package/esm/transaction-decorator.js +0 -63
  229. package/esm/transaction-decorator.js.map +0 -1
  230. package/esm/update-associations.d.ts +0 -60
  231. package/esm/update-associations.js +0 -362
  232. package/esm/update-associations.js.map +0 -1
  233. package/esm/update-guard.d.ts +0 -26
  234. package/esm/update-guard.js +0 -122
  235. package/esm/update-guard.js.map +0 -1
  236. package/lib/collection-importer.js.map +0 -1
  237. package/lib/collection.js.map +0 -1
  238. package/lib/database.js.map +0 -1
  239. package/lib/fields/array-field.js.map +0 -1
  240. package/lib/fields/belongs-to-field.js.map +0 -1
  241. package/lib/fields/belongs-to-many-field.js.map +0 -1
  242. package/lib/fields/boolean-field.js.map +0 -1
  243. package/lib/fields/context-field.js.map +0 -1
  244. package/lib/fields/date-field.js.map +0 -1
  245. package/lib/fields/field.js.map +0 -1
  246. package/lib/fields/has-inverse-field.js.map +0 -1
  247. package/lib/fields/has-many-field.js.map +0 -1
  248. package/lib/fields/has-one-field.js.map +0 -1
  249. package/lib/fields/index.js.map +0 -1
  250. package/lib/fields/json-field.js.map +0 -1
  251. package/lib/fields/number-field.js.map +0 -1
  252. package/lib/fields/password-field.js.map +0 -1
  253. package/lib/fields/radio-field.js.map +0 -1
  254. package/lib/fields/relation-field.js.map +0 -1
  255. package/lib/fields/sort-field.js.map +0 -1
  256. package/lib/fields/string-field.js.map +0 -1
  257. package/lib/fields/text-field.js.map +0 -1
  258. package/lib/fields/time-field.js.map +0 -1
  259. package/lib/fields/uid-field.js.map +0 -1
  260. package/lib/fields/virtual-field.js.map +0 -1
  261. package/lib/filter-parser.js.map +0 -1
  262. package/lib/index.js.map +0 -1
  263. package/lib/magic-attribute-model.js.map +0 -1
  264. package/lib/mock-database.js.map +0 -1
  265. package/lib/model-hook.js.map +0 -1
  266. package/lib/model.js.map +0 -1
  267. package/lib/operators/array.js.map +0 -1
  268. package/lib/operators/association.js.map +0 -1
  269. package/lib/operators/date.js.map +0 -1
  270. package/lib/operators/empty.js.map +0 -1
  271. package/lib/operators/index.js.map +0 -1
  272. package/lib/operators/ne.js.map +0 -1
  273. package/lib/operators/string.js.map +0 -1
  274. package/lib/operators/utils.js.map +0 -1
  275. package/lib/options-parser.js.map +0 -1
  276. package/lib/playground.js.map +0 -1
  277. package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
  278. package/lib/relation-repository/belongs-to-repository.js.map +0 -1
  279. package/lib/relation-repository/hasmany-repository.js.map +0 -1
  280. package/lib/relation-repository/hasone-repository.js.map +0 -1
  281. package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
  282. package/lib/relation-repository/relation-repository.js.map +0 -1
  283. package/lib/relation-repository/single-relation-repository.js.map +0 -1
  284. package/lib/relation-repository/types.js.map +0 -1
  285. package/lib/repository.js.map +0 -1
  286. package/lib/transaction-decorator.js.map +0 -1
  287. package/lib/update-associations.js.map +0 -1
  288. package/lib/update-guard.js.map +0 -1
  289. package/tsconfig.build.json +0 -9
  290. package/tsconfig.json +0 -5
package/lib/model-hook.js CHANGED
@@ -1,68 +1,109 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
15
6
  exports.ModelHook = void 0;
16
- const lodash_1 = __importDefault(require("lodash"));
17
- const model_1 = require("./model");
18
- const { hooks } = require('sequelize/lib/hooks');
7
+
8
+ function _lodash() {
9
+ const data = _interopRequireDefault(require("lodash"));
10
+
11
+ _lodash = function _lodash() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
+
20
+ 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); } }
21
+
22
+ 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); }); }; }
23
+
24
+ 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; } } }; }
25
+
26
+ 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
+ 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
+ const _require = require('sequelize/lib/hooks'),
31
+ hooks = _require.hooks;
32
+
19
33
  class ModelHook {
20
- constructor(database) {
21
- this.boundEvent = new Set();
22
- this.database = database;
34
+ constructor(database) {
35
+ this.database = void 0;
36
+ this.boundEvent = new Set();
37
+ this.database = database;
38
+ }
39
+
40
+ isModelHook(eventName) {
41
+ if (_lodash().default.isString(eventName)) {
42
+ const hookType = eventName.split('.').pop();
43
+
44
+ if (hooks[hookType]) {
45
+ return hookType;
46
+ }
23
47
  }
24
- isModelHook(eventName) {
25
- if (lodash_1.default.isString(eventName)) {
26
- const hookType = eventName.split('.').pop();
27
- if (hooks[hookType]) {
28
- return hookType;
29
- }
48
+
49
+ return false;
50
+ }
51
+
52
+ findModelName(hookArgs) {
53
+ var _iterator = _createForOfIteratorHelper(hookArgs),
54
+ _step;
55
+
56
+ try {
57
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
58
+ const arg = _step.value;
59
+
60
+ if (arg === null || arg === void 0 ? void 0 : arg._previousDataValues) {
61
+ return arg.constructor.name;
30
62
  }
31
- return false;
32
- }
33
- findModelName(hookArgs) {
34
- for (const arg of hookArgs) {
35
- if (arg instanceof model_1.Model) {
36
- return arg.constructor.name;
37
- }
38
- if (lodash_1.default.isPlainObject(arg)) {
39
- if (arg['model']) {
40
- return arg['model'].name;
41
- }
42
- if (lodash_1.default.get(arg, 'name.plural')) {
43
- return lodash_1.default.get(arg, 'name.plural');
44
- }
45
- }
63
+
64
+ if (_lodash().default.isPlainObject(arg)) {
65
+ if (arg['model']) {
66
+ return arg['model'].name;
67
+ }
68
+
69
+ if (_lodash().default.get(arg, 'name.plural')) {
70
+ return _lodash().default.get(arg, 'name.plural');
71
+ }
46
72
  }
47
- return null;
48
- }
49
- bindEvent(eventName) {
50
- this.boundEvent.add(eventName);
51
- }
52
- hasBindEvent(eventName) {
53
- return this.boundEvent.has(eventName);
54
- }
55
- sequelizeHookBuilder(eventName) {
56
- return (...args) => __awaiter(this, void 0, void 0, function* () {
57
- const modelName = this.findModelName(args);
58
- if (modelName) {
59
- // emit model event
60
- yield this.database.emitAsync(`${modelName}.${eventName}`, ...args);
61
- }
62
- // emit sequelize global event
63
- yield this.database.emitAsync(eventName, ...args);
64
- });
73
+ }
74
+ } catch (err) {
75
+ _iterator.e(err);
76
+ } finally {
77
+ _iterator.f();
65
78
  }
79
+
80
+ return null;
81
+ }
82
+
83
+ bindEvent(eventName) {
84
+ this.boundEvent.add(eventName);
85
+ }
86
+
87
+ hasBindEvent(eventName) {
88
+ return this.boundEvent.has(eventName);
89
+ }
90
+
91
+ sequelizeHookBuilder(eventName) {
92
+ var _this = this;
93
+
94
+ return /*#__PURE__*/_asyncToGenerator(function* (...args) {
95
+ const modelName = _this.findModelName(args);
96
+
97
+ if (modelName) {
98
+ // emit model event
99
+ yield _this.database.emitAsync(`${modelName}.${eventName}`, ...args);
100
+ } // emit sequelize global event
101
+
102
+
103
+ yield _this.database.emitAsync(eventName, ...args);
104
+ });
105
+ }
106
+
66
107
  }
67
- exports.ModelHook = ModelHook;
68
- //# sourceMappingURL=model-hook.js.map
108
+
109
+ exports.ModelHook = ModelHook;
package/lib/model.js CHANGED
@@ -1,87 +1,122 @@
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.Model = void 0;
7
- const sequelize_1 = require("sequelize");
8
- const lodash_1 = __importDefault(require("lodash"));
9
- class Model extends sequelize_1.Model {
10
- toJSON() {
11
- const handleObj = (obj, options) => {
12
- const handles = [
13
- (data) => {
14
- if (data instanceof Model) {
15
- return data.toJSON();
16
- }
17
- return data;
18
- },
19
- this.hiddenObjKey,
20
- ];
21
- return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), obj);
22
- };
23
- const handleArray = (arrayOfObj, options) => {
24
- const handles = [this.sortAssociations];
25
- return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
26
- };
27
- const opts = {
28
- model: this.constructor,
29
- collection: this.constructor.collection,
30
- db: this.constructor.database,
31
- };
32
- const traverseJSON = (data, options) => {
33
- const { model, db, collection } = options;
34
- // handle Object
35
- data = handleObj(data, options);
36
- const result = {};
37
- for (const key of Object.keys(data)) {
38
- // @ts-ignore
39
- if (model.hasAlias(key)) {
40
- const association = model.associations[key];
41
- const opts = {
42
- model: association.target,
43
- collection: db.getCollection(association.target.name),
44
- db,
45
- key,
46
- field: collection.getField(key),
47
- };
48
- if (['HasMany', 'BelongsToMany'].includes(association.associationType)) {
49
- result[key] = handleArray(data[key], opts).map((item) => traverseJSON(item, opts));
50
- }
51
- else {
52
- result[key] = data[key] ? traverseJSON(data[key], opts) : null;
53
- }
54
- }
55
- else {
56
- result[key] = data[key];
57
- }
58
- }
59
- return result;
60
- };
61
- return traverseJSON(super.toJSON(), opts);
62
- }
63
- hiddenObjKey(obj, options) {
64
- const hiddenFields = Array.from(options.collection.fields.values())
65
- .filter((field) => field.options.hidden)
66
- .map((field) => field.options.name);
67
- return lodash_1.default.omit(obj, hiddenFields);
68
- }
69
- sortAssociations(data, { field }) {
70
- const sortBy = field.options.sortBy;
71
- return sortBy ? this.sortArray(data, sortBy) : data;
72
- }
73
- sortArray(data, sortBy) {
74
- if (!lodash_1.default.isArray(sortBy)) {
75
- sortBy = [sortBy];
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
+ function _lodash() {
19
+ const data = _interopRequireDefault(require("lodash"));
20
+
21
+ _lodash = function _lodash() {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
27
+
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
+
30
+ class Model extends _sequelize().Model {
31
+ toJSON() {
32
+ const handleObj = (obj, options) => {
33
+ const handles = [data => {
34
+ if (data instanceof Model) {
35
+ return data.toJSON();
36
+ }
37
+
38
+ return data;
39
+ }, this.hiddenObjKey];
40
+ return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), obj);
41
+ };
42
+
43
+ const handleArray = (arrayOfObj, options) => {
44
+ const handles = [this.sortAssociations];
45
+ return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
46
+ };
47
+
48
+ const opts = {
49
+ model: this.constructor,
50
+ collection: this.constructor.collection,
51
+ db: this.constructor.database
52
+ };
53
+
54
+ const traverseJSON = (data, options) => {
55
+ const model = options.model,
56
+ db = options.db,
57
+ collection = options.collection; // handle Object
58
+
59
+ data = handleObj(data, options);
60
+ const result = {};
61
+
62
+ for (var _i = 0, _Object$keys = Object.keys(data); _i < _Object$keys.length; _i++) {
63
+ const key = _Object$keys[_i];
64
+
65
+ // @ts-ignore
66
+ if (model.hasAlias(key)) {
67
+ const association = model.associations[key];
68
+ const opts = {
69
+ model: association.target,
70
+ collection: db.getCollection(association.target.name),
71
+ db,
72
+ key,
73
+ field: collection.getField(key)
74
+ };
75
+
76
+ if (['HasMany', 'BelongsToMany'].includes(association.associationType)) {
77
+ result[key] = handleArray(data[key], opts).map(item => traverseJSON(item, opts));
78
+ } else {
79
+ result[key] = data[key] ? traverseJSON(data[key], opts) : null;
80
+ }
81
+ } else {
82
+ result[key] = data[key];
76
83
  }
77
- const orderItems = [];
78
- const orderDirections = [];
79
- sortBy.forEach((sortItem) => {
80
- orderDirections.push(sortItem.startsWith('-') ? 'desc' : 'asc');
81
- orderItems.push(sortItem.replace('-', ''));
82
- });
83
- return lodash_1.default.orderBy(data, orderItems, orderDirections);
84
+ }
85
+
86
+ return result;
87
+ };
88
+
89
+ return traverseJSON(super.toJSON(), opts);
90
+ }
91
+
92
+ hiddenObjKey(obj, options) {
93
+ const hiddenFields = Array.from(options.collection.fields.values()).filter(field => field.options.hidden).map(field => field.options.name);
94
+ return _lodash().default.omit(obj, hiddenFields);
95
+ }
96
+
97
+ sortAssociations(data, {
98
+ field
99
+ }) {
100
+ const sortBy = field.options.sortBy;
101
+ return sortBy ? this.sortArray(data, sortBy) : data;
102
+ }
103
+
104
+ sortArray(data, sortBy) {
105
+ if (!_lodash().default.isArray(sortBy)) {
106
+ sortBy = [sortBy];
84
107
  }
108
+
109
+ const orderItems = [];
110
+ const orderDirections = [];
111
+ sortBy.forEach(sortItem => {
112
+ orderDirections.push(sortItem.startsWith('-') ? 'desc' : 'asc');
113
+ orderItems.push(sortItem.replace('-', ''));
114
+ });
115
+ return _lodash().default.orderBy(data, orderItems, orderDirections);
116
+ }
117
+
85
118
  }
119
+
86
120
  exports.Model = Model;
87
- //# sourceMappingURL=model.js.map
121
+ Model.database = void 0;
122
+ Model.collection = void 0;
@@ -1,107 +1,147 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const sequelize_1 = require("sequelize");
4
- const utils_1 = require("./utils");
5
- const getFieldName = (ctx) => {
6
- const fieldName = ctx.fieldName;
7
- return fieldName;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
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 _utils = require("./utils");
19
+
20
+ const getFieldName = ctx => {
21
+ const fieldName = ctx.fieldName;
22
+ return fieldName;
8
23
  };
24
+
9
25
  const escape = (value, ctx) => {
10
- const sequelize = ctx.db.sequelize;
11
- return sequelize.escape(value);
26
+ const sequelize = ctx.db.sequelize;
27
+ return sequelize.escape(value);
12
28
  };
29
+
13
30
  const sqliteExistQuery = (value, ctx) => {
14
- const fieldName = getFieldName(ctx);
15
- const sqlArray = `(${value.map((v) => `'${v}'`).join(', ')})`;
16
- const subQuery = `exists (select * from json_each(${fieldName}) where json_each.value in ${sqlArray})`;
17
- return subQuery;
31
+ const fieldName = getFieldName(ctx);
32
+ const sqlArray = `(${value.map(v => `'${v}'`).join(', ')})`;
33
+ const subQuery = `exists (select * from json_each(${fieldName}) where json_each.value in ${sqlArray})`;
34
+ return subQuery;
18
35
  };
36
+
19
37
  const emptyQuery = (ctx, operator) => {
38
+ const fieldName = getFieldName(ctx);
39
+ let funcName = 'json_array_length';
40
+ let ifNull = 'IFNULL';
41
+
42
+ if ((0, _utils.isPg)(ctx)) {
43
+ funcName = 'jsonb_array_length';
44
+ ifNull = 'coalesce';
45
+ }
46
+
47
+ if ((0, _utils.isMySQL)(ctx)) {
48
+ funcName = 'json_length';
49
+ }
50
+
51
+ return `(select ${ifNull}(${funcName}(${fieldName}), 0) ${operator} 0)`;
52
+ };
53
+
54
+ var _default = {
55
+ $match(value, ctx) {
20
56
  const fieldName = getFieldName(ctx);
21
- let funcName = 'json_array_length';
22
- let ifNull = 'IFNULL';
23
- if ((0, utils_1.isPg)(ctx)) {
24
- funcName = 'jsonb_array_length';
25
- ifNull = 'coalesce';
57
+
58
+ if ((0, _utils.isPg)(ctx)) {
59
+ return {
60
+ [_sequelize().Op.contained]: value,
61
+ [_sequelize().Op.contains]: value
62
+ };
26
63
  }
27
- if ((0, utils_1.isMySQL)(ctx)) {
28
- funcName = 'json_length';
64
+
65
+ value = escape(JSON.stringify(value.sort()), ctx);
66
+
67
+ if ((0, _utils.isMySQL)(ctx)) {
68
+ return _sequelize().Sequelize.literal(`JSON_CONTAINS(${fieldName}, ${value}) AND JSON_CONTAINS(${value}, ${fieldName})`);
29
69
  }
30
- return `(select ${ifNull}(${funcName}(${fieldName}), 0) ${operator} 0)`;
31
- };
32
- exports.default = {
33
- $match(value, ctx) {
34
- const fieldName = getFieldName(ctx);
35
- if ((0, utils_1.isPg)(ctx)) {
36
- return {
37
- [sequelize_1.Op.contained]: value,
38
- [sequelize_1.Op.contains]: value,
39
- };
40
- }
41
- value = escape(JSON.stringify(value.sort()), ctx);
42
- if ((0, utils_1.isMySQL)(ctx)) {
43
- return sequelize_1.Sequelize.literal(`JSON_CONTAINS(${fieldName}, ${value}) AND JSON_CONTAINS(${value}, ${fieldName})`);
44
- }
45
- return {
46
- [sequelize_1.Op.eq]: sequelize_1.Sequelize.literal(`json(${value})`),
47
- };
48
- },
49
- $notMatch(value, ctx) {
50
- const fieldName = getFieldName(ctx);
51
- value = escape(JSON.stringify(value), ctx);
52
- if ((0, utils_1.isPg)(ctx)) {
53
- return sequelize_1.Sequelize.literal(`not (${fieldName} <@ ${value}::JSONB and ${fieldName} @> ${value}::JSONB)`);
54
- }
55
- if ((0, utils_1.isMySQL)(ctx)) {
56
- return sequelize_1.Sequelize.literal(`not (JSON_CONTAINS(${fieldName}, ${value}) AND JSON_CONTAINS(${value}, ${fieldName}))`);
57
- }
58
- return {
59
- [sequelize_1.Op.ne]: sequelize_1.Sequelize.literal(`json(${value})`),
60
- };
61
- },
62
- $anyOf(value, ctx) {
63
- const fieldName = getFieldName(ctx);
64
- if ((0, utils_1.isPg)(ctx)) {
65
- return sequelize_1.Sequelize.literal(`${fieldName} ?| ${escape(value.map((i) => `${i}`), ctx)}`);
66
- }
67
- if ((0, utils_1.isMySQL)(ctx)) {
68
- value = escape(JSON.stringify(value), ctx);
69
- return sequelize_1.Sequelize.literal(`JSON_OVERLAPS(${fieldName}, ${value})`);
70
- }
71
- const subQuery = sqliteExistQuery(value, ctx);
72
- return sequelize_1.Sequelize.literal(subQuery);
73
- },
74
- $noneOf(value, ctx) {
75
- let where;
76
- if ((0, utils_1.isPg)(ctx)) {
77
- const fieldName = getFieldName(ctx);
78
- // pg single quote
79
- where = sequelize_1.Sequelize.literal(`not (${fieldName} ?| ${escape(value.map((i) => `${i}`), ctx)})`);
80
- }
81
- else if ((0, utils_1.isMySQL)(ctx)) {
82
- const fieldName = getFieldName(ctx);
83
- value = escape(JSON.stringify(value), ctx);
84
- where = sequelize_1.Sequelize.literal(`NOT JSON_OVERLAPS(${fieldName}, ${value})`);
85
- }
86
- else {
87
- const subQuery = sqliteExistQuery(value, ctx);
88
- where = sequelize_1.Sequelize.literal(`not ${subQuery}`);
89
- }
90
- return {
91
- [sequelize_1.Op.or]: [where, { [sequelize_1.Op.is]: null }],
92
- };
93
- },
94
- $arrayEmpty(value, ctx) {
95
- const subQuery = emptyQuery(ctx, '=');
96
- return {
97
- [sequelize_1.Op.and]: [sequelize_1.Sequelize.literal(`${subQuery}`)],
98
- };
99
- },
100
- $arrayNotEmpty(value, ctx) {
101
- const subQuery = emptyQuery(ctx, '>');
102
- return {
103
- [sequelize_1.Op.and]: [sequelize_1.Sequelize.literal(`${subQuery}`)],
104
- };
105
- },
70
+
71
+ return {
72
+ [_sequelize().Op.eq]: _sequelize().Sequelize.literal(`json(${value})`)
73
+ };
74
+ },
75
+
76
+ $notMatch(value, ctx) {
77
+ const fieldName = getFieldName(ctx);
78
+ value = escape(JSON.stringify(value), ctx);
79
+
80
+ if ((0, _utils.isPg)(ctx)) {
81
+ return _sequelize().Sequelize.literal(`not (${fieldName} <@ ${value}::JSONB and ${fieldName} @> ${value}::JSONB)`);
82
+ }
83
+
84
+ if ((0, _utils.isMySQL)(ctx)) {
85
+ return _sequelize().Sequelize.literal(`not (JSON_CONTAINS(${fieldName}, ${value}) AND JSON_CONTAINS(${value}, ${fieldName}))`);
86
+ }
87
+
88
+ return {
89
+ [_sequelize().Op.ne]: _sequelize().Sequelize.literal(`json(${value})`)
90
+ };
91
+ },
92
+
93
+ $anyOf(value, ctx) {
94
+ const fieldName = getFieldName(ctx);
95
+
96
+ if ((0, _utils.isPg)(ctx)) {
97
+ return _sequelize().Sequelize.literal(`${fieldName} ?| ${escape(value.map(i => `${i}`), ctx)}`);
98
+ }
99
+
100
+ if ((0, _utils.isMySQL)(ctx)) {
101
+ value = escape(JSON.stringify(value), ctx);
102
+ return _sequelize().Sequelize.literal(`JSON_OVERLAPS(${fieldName}, ${value})`);
103
+ }
104
+
105
+ const subQuery = sqliteExistQuery(value, ctx);
106
+ return _sequelize().Sequelize.literal(subQuery);
107
+ },
108
+
109
+ $noneOf(value, ctx) {
110
+ let where;
111
+
112
+ if ((0, _utils.isPg)(ctx)) {
113
+ const fieldName = getFieldName(ctx); // pg single quote
114
+
115
+ where = _sequelize().Sequelize.literal(`not (${fieldName} ?| ${escape(value.map(i => `${i}`), ctx)})`);
116
+ } else if ((0, _utils.isMySQL)(ctx)) {
117
+ const fieldName = getFieldName(ctx);
118
+ value = escape(JSON.stringify(value), ctx);
119
+ where = _sequelize().Sequelize.literal(`NOT JSON_OVERLAPS(${fieldName}, ${value})`);
120
+ } else {
121
+ const subQuery = sqliteExistQuery(value, ctx);
122
+ where = _sequelize().Sequelize.literal(`not ${subQuery}`);
123
+ }
124
+
125
+ return {
126
+ [_sequelize().Op.or]: [where, {
127
+ [_sequelize().Op.is]: null
128
+ }]
129
+ };
130
+ },
131
+
132
+ $arrayEmpty(value, ctx) {
133
+ const subQuery = emptyQuery(ctx, '=');
134
+ return {
135
+ [_sequelize().Op.and]: [_sequelize().Sequelize.literal(`${subQuery}`)]
136
+ };
137
+ },
138
+
139
+ $arrayNotEmpty(value, ctx) {
140
+ const subQuery = emptyQuery(ctx, '>');
141
+ return {
142
+ [_sequelize().Op.and]: [_sequelize().Sequelize.literal(`${subQuery}`)]
143
+ };
144
+ }
145
+
106
146
  };
107
- //# sourceMappingURL=array.js.map
147
+ exports.default = _default;