@nocobase/database 0.7.0-alpha.8 → 0.7.0-alpha.82

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 +120 -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/index.ts +3 -0
  73. package/src/fields/password-field.ts +2 -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,151 +1,228 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.updateMultipleAssociation = exports.updateSingleAssociation = exports.updateAssociation = exports.updateAssociations = exports.updateThroughTableValue = exports.updateModelByValues = exports.modelAssociationByKey = exports.belongsToManyAssociations = exports.modelAssociations = void 0;
13
- const model_1 = require("./model");
14
- const update_guard_1 = require("./update-guard");
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.belongsToManyAssociations = belongsToManyAssociations;
7
+ exports.modelAssociationByKey = modelAssociationByKey;
8
+ exports.modelAssociations = modelAssociations;
9
+ exports.updateAssociation = updateAssociation;
10
+ exports.updateAssociations = updateAssociations;
11
+ exports.updateModelByValues = updateModelByValues;
12
+ exports.updateMultipleAssociation = updateMultipleAssociation;
13
+ exports.updateSingleAssociation = updateSingleAssociation;
14
+ exports.updateThroughTableValue = updateThroughTableValue;
15
+
16
+ var _model = require("./model");
17
+
18
+ var _updateGuard = require("./update-guard");
19
+
20
+ 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; }
21
+
22
+ 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; }
23
+
24
+ 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; }
25
+
26
+ 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(_e2) { throw _e2; }, 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(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
27
+
28
+ 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); } }
29
+
30
+ 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); }); }; }
31
+
32
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
33
+
34
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
35
+
36
+ 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); }
37
+
38
+ 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; }
39
+
40
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
41
+
42
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
43
+
15
44
  function isUndefinedOrNull(value) {
16
- return typeof value === 'undefined' || value === null;
45
+ return typeof value === 'undefined' || value === null;
17
46
  }
47
+
18
48
  function isStringOrNumber(value) {
19
- return typeof value === 'string' || typeof value === 'number';
49
+ return typeof value === 'string' || typeof value === 'number';
20
50
  }
51
+
21
52
  function getKeysByPrefix(keys, prefix) {
22
- return keys.filter((key) => key.startsWith(`${prefix}.`)).map((key) => key.substring(prefix.length + 1));
53
+ return keys.filter(key => key.startsWith(`${prefix}.`)).map(key => key.substring(prefix.length + 1));
23
54
  }
55
+
24
56
  function modelAssociations(instance) {
25
- return instance.constructor.associations;
57
+ return instance.constructor.associations;
26
58
  }
27
- exports.modelAssociations = modelAssociations;
59
+
28
60
  function belongsToManyAssociations(instance) {
29
- const associations = modelAssociations(instance);
30
- return Object.entries(associations)
31
- .filter((entry) => {
32
- const [key, association] = entry;
33
- return association.associationType == 'BelongsToMany';
34
- })
35
- .map((association) => {
36
- return association[1];
37
- });
61
+ const associations = modelAssociations(instance);
62
+ return Object.entries(associations).filter(entry => {
63
+ const _entry = _slicedToArray(entry, 2),
64
+ key = _entry[0],
65
+ association = _entry[1];
66
+
67
+ return association.associationType == 'BelongsToMany';
68
+ }).map(association => {
69
+ return association[1];
70
+ });
38
71
  }
39
- exports.belongsToManyAssociations = belongsToManyAssociations;
72
+
40
73
  function modelAssociationByKey(instance, key) {
41
- return modelAssociations(instance)[key];
74
+ return modelAssociations(instance)[key];
42
75
  }
43
- exports.modelAssociationByKey = modelAssociationByKey;
44
- function updateModelByValues(instance, values, options) {
45
- return __awaiter(this, void 0, void 0, function* () {
46
- if (!(options === null || options === void 0 ? void 0 : options.sanitized)) {
47
- const guard = new update_guard_1.UpdateGuard();
48
- //@ts-ignore
49
- guard.setModel(instance.constructor);
50
- guard.setBlackList(options.blacklist);
51
- guard.setWhiteList(options.whitelist);
52
- guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
53
- values = guard.sanitize(values);
54
- }
55
- yield instance.update(values, options);
56
- yield updateAssociations(instance, values, options);
57
- });
76
+
77
+ function updateModelByValues(_x, _x2, _x3) {
78
+ return _updateModelByValues.apply(this, arguments);
58
79
  }
59
- exports.updateModelByValues = updateModelByValues;
60
- function updateThroughTableValue(instance, throughName, throughValues, source, transaction = null) {
61
- return __awaiter(this, void 0, void 0, function* () {
62
- // update through table values
63
- for (const belongsToMany of belongsToManyAssociations(instance)) {
64
- // @ts-ignore
65
- const throughModel = belongsToMany.through.model;
66
- const throughModelName = throughModel.name;
67
- if (throughModelName === throughModelName) {
68
- const where = {
69
- [belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
70
- [belongsToMany.otherKey]: source.get(belongsToMany.targetKey),
71
- };
72
- return yield throughModel.update(throughValues, {
73
- where,
74
- transaction,
75
- });
76
- }
77
- }
78
- });
80
+
81
+ function _updateModelByValues() {
82
+ _updateModelByValues = _asyncToGenerator(function* (instance, values, options) {
83
+ if (!(options === null || options === void 0 ? void 0 : options.sanitized)) {
84
+ const guard = new _updateGuard.UpdateGuard(); //@ts-ignore
85
+
86
+ guard.setModel(instance.constructor);
87
+ guard.setBlackList(options.blacklist);
88
+ guard.setWhiteList(options.whitelist);
89
+ guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
90
+ values = guard.sanitize(values);
91
+ }
92
+
93
+ yield instance.update(values, options);
94
+ yield updateAssociations(instance, values, options);
95
+ });
96
+ return _updateModelByValues.apply(this, arguments);
97
+ }
98
+
99
+ function updateThroughTableValue(_x4, _x5, _x6, _x7) {
100
+ return _updateThroughTableValue.apply(this, arguments);
79
101
  }
80
- exports.updateThroughTableValue = updateThroughTableValue;
81
102
  /**
82
103
  * update association of instance by values
83
104
  * @param instance
84
105
  * @param values
85
106
  * @param options
86
107
  */
87
- function updateAssociations(instance, values, options = {}) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- // if no values set, return
90
- if (!values) {
91
- return;
92
- }
93
- let newTransaction = false;
94
- let transaction = options.transaction;
95
- if (!transaction) {
96
- newTransaction = true;
97
- transaction = yield instance.sequelize.transaction();
98
- }
99
- const keys = Object.keys(values);
100
- for (const key of Object.keys(modelAssociations(instance))) {
101
- if (keys.includes(key)) {
102
- yield updateAssociation(instance, key, values[key], Object.assign(Object.assign({}, options), { transaction }));
103
- }
104
- }
105
- // update through table values
106
- for (const belongsToMany of belongsToManyAssociations(instance)) {
107
- // @ts-ignore
108
- const throughModel = belongsToMany.through.model;
109
- const throughModelName = throughModel.name;
110
- if (values[throughModelName] && options.sourceModel) {
111
- const where = {
112
- [belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
113
- [belongsToMany.otherKey]: options.sourceModel.get(belongsToMany.targetKey),
114
- };
115
- yield throughModel.update(values[throughModel.name], {
116
- where,
117
- context: options.context,
118
- transaction,
119
- });
120
- }
121
- }
122
- if (newTransaction) {
123
- yield transaction.commit();
108
+
109
+
110
+ function _updateThroughTableValue() {
111
+ _updateThroughTableValue = _asyncToGenerator(function* (instance, throughName, throughValues, source, transaction = null) {
112
+ // update through table values
113
+ var _iterator = _createForOfIteratorHelper(belongsToManyAssociations(instance)),
114
+ _step;
115
+
116
+ try {
117
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
118
+ const belongsToMany = _step.value;
119
+ // @ts-ignore
120
+ const throughModel = belongsToMany.through.model;
121
+ const throughModelName = throughModel.name;
122
+
123
+ if (throughModelName === throughModelName) {
124
+ const where = {
125
+ [belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
126
+ [belongsToMany.otherKey]: source.get(belongsToMany.targetKey)
127
+ };
128
+ return yield throughModel.update(throughValues, {
129
+ where,
130
+ transaction
131
+ });
124
132
  }
125
- });
133
+ }
134
+ } catch (err) {
135
+ _iterator.e(err);
136
+ } finally {
137
+ _iterator.f();
138
+ }
139
+ });
140
+ return _updateThroughTableValue.apply(this, arguments);
126
141
  }
127
- exports.updateAssociations = updateAssociations;
128
- function isReverseAssociationPair(a, b) {
129
- const typeSet = new Set();
130
- typeSet.add(a.associationType);
131
- typeSet.add(b.associationType);
132
- if (typeSet.size == 1 && typeSet.has('BelongsToMany')) {
133
- return (a.through.tableName === b.through.tableName &&
134
- a.target.name === b.source.name &&
135
- b.target.name === a.source.name &&
136
- a.foreignKey === b.otherKey &&
137
- a.sourceKey === b.targetKey &&
138
- a.otherKey === b.foreignKey &&
139
- a.targetKey === b.sourceKey);
142
+
143
+ function updateAssociations(_x8, _x9) {
144
+ return _updateAssociations.apply(this, arguments);
145
+ }
146
+
147
+ function _updateAssociations() {
148
+ _updateAssociations = _asyncToGenerator(function* (instance, values, options = {}) {
149
+ // if no values set, return
150
+ if (!values) {
151
+ return;
152
+ }
153
+
154
+ let newTransaction = false;
155
+ let transaction = options.transaction;
156
+
157
+ if (!transaction) {
158
+ newTransaction = true;
159
+ transaction = yield instance.sequelize.transaction();
160
+ }
161
+
162
+ const keys = Object.keys(values);
163
+
164
+ for (var _i2 = 0, _Object$keys = Object.keys(modelAssociations(instance)); _i2 < _Object$keys.length; _i2++) {
165
+ const key = _Object$keys[_i2];
166
+
167
+ if (keys.includes(key)) {
168
+ yield updateAssociation(instance, key, values[key], _objectSpread(_objectSpread({}, options), {}, {
169
+ transaction
170
+ }));
171
+ }
172
+ } // update through table values
173
+
174
+
175
+ var _iterator2 = _createForOfIteratorHelper(belongsToManyAssociations(instance)),
176
+ _step2;
177
+
178
+ try {
179
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
180
+ const belongsToMany = _step2.value;
181
+ // @ts-ignore
182
+ const throughModel = belongsToMany.through.model;
183
+ const throughModelName = throughModel.name;
184
+
185
+ if (values[throughModelName] && options.sourceModel) {
186
+ const where = {
187
+ [belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
188
+ [belongsToMany.otherKey]: options.sourceModel.get(belongsToMany.targetKey)
189
+ };
190
+ yield throughModel.update(values[throughModel.name], {
191
+ where,
192
+ context: options.context,
193
+ transaction
194
+ });
195
+ }
196
+ }
197
+ } catch (err) {
198
+ _iterator2.e(err);
199
+ } finally {
200
+ _iterator2.f();
140
201
  }
141
- if ((typeSet.has('HasOne') && typeSet.has('BelongsTo')) || (typeSet.has('HasMany') && typeSet.has('BelongsTo'))) {
142
- const sourceAssoc = a.associationType == 'BelongsTo' ? b : a;
143
- const targetAssoc = sourceAssoc == a ? b : a;
144
- return (sourceAssoc.source.name === targetAssoc.target.name &&
145
- sourceAssoc.foreignKey === targetAssoc.foreignKey &&
146
- sourceAssoc.sourceKey === targetAssoc.targetKey);
202
+
203
+ if (newTransaction) {
204
+ yield transaction.commit();
147
205
  }
148
- return false;
206
+ });
207
+ return _updateAssociations.apply(this, arguments);
208
+ }
209
+
210
+ function isReverseAssociationPair(a, b) {
211
+ const typeSet = new Set();
212
+ typeSet.add(a.associationType);
213
+ typeSet.add(b.associationType);
214
+
215
+ if (typeSet.size == 1 && typeSet.has('BelongsToMany')) {
216
+ return a.through.tableName === b.through.tableName && a.target.name === b.source.name && b.target.name === a.source.name && a.foreignKey === b.otherKey && a.sourceKey === b.targetKey && a.otherKey === b.foreignKey && a.targetKey === b.sourceKey;
217
+ }
218
+
219
+ if (typeSet.has('HasOne') && typeSet.has('BelongsTo') || typeSet.has('HasMany') && typeSet.has('BelongsTo')) {
220
+ const sourceAssoc = a.associationType == 'BelongsTo' ? b : a;
221
+ const targetAssoc = sourceAssoc == a ? b : a;
222
+ return sourceAssoc.source.name === targetAssoc.target.name && sourceAssoc.foreignKey === targetAssoc.foreignKey && sourceAssoc.sourceKey === targetAssoc.targetKey;
223
+ }
224
+
225
+ return false;
149
226
  }
150
227
  /**
151
228
  * update model association by key
@@ -154,26 +231,11 @@ function isReverseAssociationPair(a, b) {
154
231
  * @param value
155
232
  * @param options
156
233
  */
157
- function updateAssociation(instance, key, value, options = {}) {
158
- return __awaiter(this, void 0, void 0, function* () {
159
- const association = modelAssociationByKey(instance, key);
160
- if (!association) {
161
- return false;
162
- }
163
- if (options.associationContext && isReverseAssociationPair(association, options.associationContext)) {
164
- return false;
165
- }
166
- switch (association.associationType) {
167
- case 'HasOne':
168
- case 'BelongsTo':
169
- return updateSingleAssociation(instance, key, value, options);
170
- case 'HasMany':
171
- case 'BelongsToMany':
172
- return updateMultipleAssociation(instance, key, value, options);
173
- }
174
- });
234
+
235
+
236
+ function updateAssociation(_x10, _x11, _x12) {
237
+ return _updateAssociation.apply(this, arguments);
175
238
  }
176
- exports.updateAssociation = updateAssociation;
177
239
  /**
178
240
  * update belongsTo and HasOne
179
241
  * @param model
@@ -181,98 +243,36 @@ exports.updateAssociation = updateAssociation;
181
243
  * @param value
182
244
  * @param options
183
245
  */
184
- function updateSingleAssociation(model, key, value, options = {}) {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- const association = modelAssociationByKey(model, key);
187
- if (!association) {
188
- return false;
189
- }
190
- if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
191
- return false;
192
- }
193
- const { context, updateAssociationValues = [], transaction = yield model.sequelize.transaction() } = options;
194
- const keys = getKeysByPrefix(updateAssociationValues, key);
195
- try {
196
- // set method of association
197
- const setAccessor = association.accessors.set;
198
- const removeAssociation = () => __awaiter(this, void 0, void 0, function* () {
199
- yield model[setAccessor](null, { transaction });
200
- model.setDataValue(key, null);
201
- if (!options.transaction) {
202
- yield transaction.commit();
203
- }
204
- return true;
205
- });
206
- if (isUndefinedOrNull(value)) {
207
- return yield removeAssociation();
208
- }
209
- if (isStringOrNumber(value)) {
210
- yield model[setAccessor](value, { context, transaction });
211
- if (!options.transaction) {
212
- yield transaction.commit();
213
- }
214
- return true;
215
- }
216
- if (value instanceof model_1.Model) {
217
- yield model[setAccessor](value, { context, transaction });
218
- model.setDataValue(key, value);
219
- if (!options.transaction) {
220
- yield transaction.commit();
221
- }
222
- return true;
223
- }
224
- const createAccessor = association.accessors.create;
225
- let dataKey;
226
- let M;
227
- if (association.associationType === 'BelongsTo') {
228
- M = association.target;
229
- // @ts-ignore
230
- dataKey = association.targetKey;
231
- }
232
- else {
233
- M = association.source;
234
- dataKey = M.primaryKeyAttribute;
235
- }
236
- if (isStringOrNumber(value[dataKey])) {
237
- let instance = yield M.findOne({
238
- where: {
239
- [dataKey]: value[dataKey],
240
- },
241
- transaction,
242
- });
243
- if (instance) {
244
- yield model[setAccessor](instance, { context, transaction });
245
- if (updateAssociationValues.includes(key)) {
246
- yield instance.update(value, Object.assign(Object.assign({}, options), { transaction }));
247
- }
248
- yield updateAssociations(instance, value, Object.assign(Object.assign({}, options), { transaction, associationContext: association, updateAssociationValues: keys }));
249
- model.setDataValue(key, instance);
250
- if (!options.transaction) {
251
- yield transaction.commit();
252
- }
253
- return true;
254
- }
255
- }
256
- const instance = yield model[createAccessor](value, { context, transaction });
257
- yield updateAssociations(instance, value, Object.assign(Object.assign({}, options), { transaction, associationContext: association, updateAssociationValues: keys }));
258
- model.setDataValue(key, instance);
259
- // @ts-ignore
260
- if (association.targetKey) {
261
- model.setDataValue(association.foreignKey, instance[dataKey]);
262
- }
263
- if (!options.transaction) {
264
- yield transaction.commit();
265
- }
266
- }
267
- catch (error) {
268
- if (!options.transaction) {
269
- yield transaction.rollback();
270
- }
271
- throw error;
272
- }
273
- });
246
+
247
+
248
+ function _updateAssociation() {
249
+ _updateAssociation = _asyncToGenerator(function* (instance, key, value, options = {}) {
250
+ const association = modelAssociationByKey(instance, key);
251
+
252
+ if (!association) {
253
+ return false;
254
+ }
255
+
256
+ if (options.associationContext && isReverseAssociationPair(association, options.associationContext)) {
257
+ return false;
258
+ }
259
+
260
+ switch (association.associationType) {
261
+ case 'HasOne':
262
+ case 'BelongsTo':
263
+ return updateSingleAssociation(instance, key, value, options);
264
+
265
+ case 'HasMany':
266
+ case 'BelongsToMany':
267
+ return updateMultipleAssociation(instance, key, value, options);
268
+ }
269
+ });
270
+ return _updateAssociation.apply(this, arguments);
271
+ }
272
+
273
+ function updateSingleAssociation(_x13, _x14, _x15) {
274
+ return _updateSingleAssociation.apply(this, arguments);
274
275
  }
275
- exports.updateSingleAssociation = updateSingleAssociation;
276
276
  /**
277
277
  * update multiple association of model by value
278
278
  * @param model
@@ -280,95 +280,299 @@ exports.updateSingleAssociation = updateSingleAssociation;
280
280
  * @param value
281
281
  * @param options
282
282
  */
283
- function updateMultipleAssociation(model, key, value, options = {}) {
284
- return __awaiter(this, void 0, void 0, function* () {
285
- const association = modelAssociationByKey(model, key);
286
- if (!association) {
287
- return false;
288
- }
289
- if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
290
- return false;
283
+
284
+
285
+ function _updateSingleAssociation() {
286
+ _updateSingleAssociation = _asyncToGenerator(function* (model, key, value, options = {}) {
287
+ const association = modelAssociationByKey(model, key);
288
+
289
+ if (!association) {
290
+ return false;
291
+ }
292
+
293
+ if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
294
+ return false;
295
+ }
296
+
297
+ const context = options.context,
298
+ _options$updateAssoci = options.updateAssociationValues,
299
+ updateAssociationValues = _options$updateAssoci === void 0 ? [] : _options$updateAssoci,
300
+ _options$transaction = options.transaction,
301
+ transaction = _options$transaction === void 0 ? yield model.sequelize.transaction() : _options$transaction;
302
+ const keys = getKeysByPrefix(updateAssociationValues, key);
303
+
304
+ try {
305
+ // set method of association
306
+ const setAccessor = association.accessors.set;
307
+
308
+ const removeAssociation = /*#__PURE__*/function () {
309
+ var _ref = _asyncToGenerator(function* () {
310
+ yield model[setAccessor](null, {
311
+ transaction
312
+ });
313
+ model.setDataValue(key, null);
314
+
315
+ if (!options.transaction) {
316
+ yield transaction.commit();
317
+ }
318
+
319
+ return true;
320
+ });
321
+
322
+ return function removeAssociation() {
323
+ return _ref.apply(this, arguments);
324
+ };
325
+ }();
326
+
327
+ if (isUndefinedOrNull(value)) {
328
+ return yield removeAssociation();
329
+ }
330
+
331
+ if (isStringOrNumber(value)) {
332
+ yield model[setAccessor](value, {
333
+ context,
334
+ transaction
335
+ });
336
+
337
+ if (!options.transaction) {
338
+ yield transaction.commit();
291
339
  }
292
- const { context, updateAssociationValues = [], transaction = yield model.sequelize.transaction() } = options;
293
- const keys = getKeysByPrefix(updateAssociationValues, key);
294
- try {
295
- const setAccessor = association.accessors.set;
296
- const createAccessor = association.accessors.create;
297
- if (isUndefinedOrNull(value)) {
298
- yield model[setAccessor](null, { transaction, context });
299
- model.setDataValue(key, null);
300
- return;
301
- }
302
- if (isStringOrNumber(value)) {
303
- yield model[setAccessor](value, { transaction, context });
304
- return;
305
- }
306
- if (!Array.isArray(value)) {
307
- value = [value];
308
- }
309
- const list1 = []; // to be setted
310
- const list2 = []; // to be added
311
- for (const item of value) {
312
- if (isUndefinedOrNull(item)) {
313
- continue;
314
- }
315
- if (isStringOrNumber(item)) {
316
- list1.push(item);
317
- }
318
- else if (item instanceof model_1.Model) {
319
- list1.push(item);
320
- }
321
- else if (item.sequelize) {
322
- list1.push(item);
323
- }
324
- else if (typeof item === 'object') {
325
- list2.push(item);
326
- }
327
- }
328
- // associate targets in lists1
329
- yield model[setAccessor](list1, { transaction, context });
330
- const list3 = [];
331
- for (const item of list2) {
332
- const pk = association.target.primaryKeyAttribute;
333
- const through = association.through ? association.through.model.name : null;
334
- const accessorOptions = {
335
- context,
336
- transaction,
337
- };
338
- const throughValue = item[through];
339
- if (throughValue) {
340
- accessorOptions['through'] = throughValue;
341
- }
342
- if (isUndefinedOrNull(item[pk])) {
343
- // create new record
344
- const instance = yield model[createAccessor](item, accessorOptions);
345
- yield updateAssociations(instance, item, Object.assign(Object.assign({}, options), { transaction, associationContext: association, updateAssociationValues: keys }));
346
- list3.push(instance);
347
- }
348
- else {
349
- // set & update record
350
- const instance = yield association.target.findByPk(item[pk], {
351
- transaction,
352
- });
353
- const addAccessor = association.accessors.add;
354
- yield model[addAccessor](item[pk], accessorOptions);
355
- if (updateAssociationValues.includes(key)) {
356
- yield instance.update(item, Object.assign(Object.assign({}, options), { transaction }));
357
- }
358
- yield updateAssociations(instance, item, Object.assign(Object.assign({}, options), { transaction, associationContext: association, updateAssociationValues: keys }));
359
- list3.push(instance);
360
- }
361
- }
362
- model.setDataValue(key, list1.concat(list3));
363
- if (!options.transaction) {
364
- yield transaction.commit();
365
- }
340
+
341
+ return true;
342
+ }
343
+
344
+ if (value instanceof _model.Model) {
345
+ yield model[setAccessor](value, {
346
+ context,
347
+ transaction
348
+ });
349
+ model.setDataValue(key, value);
350
+
351
+ if (!options.transaction) {
352
+ yield transaction.commit();
366
353
  }
367
- catch (error) {
368
- yield transaction.rollback();
369
- throw error;
354
+
355
+ return true;
356
+ }
357
+
358
+ const createAccessor = association.accessors.create;
359
+ let dataKey;
360
+ let M;
361
+
362
+ if (association.associationType === 'BelongsTo') {
363
+ M = association.target; // @ts-ignore
364
+
365
+ dataKey = association.targetKey;
366
+ } else {
367
+ M = association.source;
368
+ dataKey = M.primaryKeyAttribute;
369
+ }
370
+
371
+ if (isStringOrNumber(value[dataKey])) {
372
+ let instance = yield M.findOne({
373
+ where: {
374
+ [dataKey]: value[dataKey]
375
+ },
376
+ transaction
377
+ });
378
+
379
+ if (instance) {
380
+ yield model[setAccessor](instance, {
381
+ context,
382
+ transaction
383
+ });
384
+
385
+ if (updateAssociationValues.includes(key)) {
386
+ yield instance.update(value, _objectSpread(_objectSpread({}, options), {}, {
387
+ transaction
388
+ }));
389
+ }
390
+
391
+ yield updateAssociations(instance, value, _objectSpread(_objectSpread({}, options), {}, {
392
+ transaction,
393
+ associationContext: association,
394
+ updateAssociationValues: keys
395
+ }));
396
+ model.setDataValue(key, instance);
397
+
398
+ if (!options.transaction) {
399
+ yield transaction.commit();
400
+ }
401
+
402
+ return true;
370
403
  }
371
- });
404
+ }
405
+
406
+ const instance = yield model[createAccessor](value, {
407
+ context,
408
+ transaction
409
+ });
410
+ yield updateAssociations(instance, value, _objectSpread(_objectSpread({}, options), {}, {
411
+ transaction,
412
+ associationContext: association,
413
+ updateAssociationValues: keys
414
+ }));
415
+ model.setDataValue(key, instance); // @ts-ignore
416
+
417
+ if (association.targetKey) {
418
+ model.setDataValue(association.foreignKey, instance[dataKey]);
419
+ }
420
+
421
+ if (!options.transaction) {
422
+ yield transaction.commit();
423
+ }
424
+ } catch (error) {
425
+ if (!options.transaction) {
426
+ yield transaction.rollback();
427
+ }
428
+
429
+ throw error;
430
+ }
431
+ });
432
+ return _updateSingleAssociation.apply(this, arguments);
372
433
  }
373
- exports.updateMultipleAssociation = updateMultipleAssociation;
374
- //# sourceMappingURL=update-associations.js.map
434
+
435
+ function updateMultipleAssociation(_x16, _x17, _x18) {
436
+ return _updateMultipleAssociation.apply(this, arguments);
437
+ }
438
+
439
+ function _updateMultipleAssociation() {
440
+ _updateMultipleAssociation = _asyncToGenerator(function* (model, key, value, options = {}) {
441
+ const association = modelAssociationByKey(model, key);
442
+
443
+ if (!association) {
444
+ return false;
445
+ }
446
+
447
+ if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
448
+ return false;
449
+ }
450
+
451
+ const context = options.context,
452
+ _options$updateAssoci2 = options.updateAssociationValues,
453
+ updateAssociationValues = _options$updateAssoci2 === void 0 ? [] : _options$updateAssoci2,
454
+ _options$transaction2 = options.transaction,
455
+ transaction = _options$transaction2 === void 0 ? yield model.sequelize.transaction() : _options$transaction2;
456
+ const keys = getKeysByPrefix(updateAssociationValues, key);
457
+
458
+ try {
459
+ const setAccessor = association.accessors.set;
460
+ const createAccessor = association.accessors.create;
461
+
462
+ if (isUndefinedOrNull(value)) {
463
+ yield model[setAccessor](null, {
464
+ transaction,
465
+ context
466
+ });
467
+ model.setDataValue(key, null);
468
+ return;
469
+ }
470
+
471
+ if (isStringOrNumber(value)) {
472
+ yield model[setAccessor](value, {
473
+ transaction,
474
+ context
475
+ });
476
+ return;
477
+ }
478
+
479
+ if (!Array.isArray(value)) {
480
+ value = [value];
481
+ }
482
+
483
+ const list1 = []; // to be setted
484
+
485
+ const list2 = []; // to be added
486
+
487
+ var _iterator3 = _createForOfIteratorHelper(value),
488
+ _step3;
489
+
490
+ try {
491
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
492
+ const item = _step3.value;
493
+
494
+ if (isUndefinedOrNull(item)) {
495
+ continue;
496
+ }
497
+
498
+ if (isStringOrNumber(item)) {
499
+ list1.push(item);
500
+ } else if (item instanceof _model.Model) {
501
+ list1.push(item);
502
+ } else if (item.sequelize) {
503
+ list1.push(item);
504
+ } else if (typeof item === 'object') {
505
+ list2.push(item);
506
+ }
507
+ } // associate targets in lists1
508
+
509
+ } catch (err) {
510
+ _iterator3.e(err);
511
+ } finally {
512
+ _iterator3.f();
513
+ }
514
+
515
+ yield model[setAccessor](list1, {
516
+ transaction,
517
+ context
518
+ });
519
+ const list3 = [];
520
+
521
+ for (var _i3 = 0, _list = list2; _i3 < _list.length; _i3++) {
522
+ const item = _list[_i3];
523
+ const pk = association.target.primaryKeyAttribute;
524
+ const through = association.through ? association.through.model.name : null;
525
+ const accessorOptions = {
526
+ context,
527
+ transaction
528
+ };
529
+ const throughValue = item[through];
530
+
531
+ if (throughValue) {
532
+ accessorOptions['through'] = throughValue;
533
+ }
534
+
535
+ if (isUndefinedOrNull(item[pk])) {
536
+ // create new record
537
+ const instance = yield model[createAccessor](item, accessorOptions);
538
+ yield updateAssociations(instance, item, _objectSpread(_objectSpread({}, options), {}, {
539
+ transaction,
540
+ associationContext: association,
541
+ updateAssociationValues: keys
542
+ }));
543
+ list3.push(instance);
544
+ } else {
545
+ // set & update record
546
+ const instance = yield association.target.findByPk(item[pk], {
547
+ transaction
548
+ });
549
+ const addAccessor = association.accessors.add;
550
+ yield model[addAccessor](item[pk], accessorOptions);
551
+
552
+ if (updateAssociationValues.includes(key)) {
553
+ yield instance.update(item, _objectSpread(_objectSpread({}, options), {}, {
554
+ transaction
555
+ }));
556
+ }
557
+
558
+ yield updateAssociations(instance, item, _objectSpread(_objectSpread({}, options), {}, {
559
+ transaction,
560
+ associationContext: association,
561
+ updateAssociationValues: keys
562
+ }));
563
+ list3.push(instance);
564
+ }
565
+ }
566
+
567
+ model.setDataValue(key, list1.concat(list3));
568
+
569
+ if (!options.transaction) {
570
+ yield transaction.commit();
571
+ }
572
+ } catch (error) {
573
+ yield transaction.rollback();
574
+ throw error;
575
+ }
576
+ });
577
+ return _updateMultipleAssociation.apply(this, arguments);
578
+ }