@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.
- package/lib/collection-importer.js +101 -67
- package/lib/collection.js +339 -210
- package/lib/database.d.ts +4 -1
- package/lib/database.js +489 -275
- package/lib/fields/array-field.js +45 -25
- package/lib/fields/belongs-to-field.js +101 -54
- package/lib/fields/belongs-to-many-field.js +98 -53
- package/lib/fields/boolean-field.js +24 -9
- package/lib/fields/context-field.js +77 -42
- package/lib/fields/date-field.js +24 -9
- package/lib/fields/field.js +114 -75
- package/lib/fields/has-inverse-field.js +4 -2
- package/lib/fields/has-many-field.js +105 -56
- package/lib/fields/has-one-field.js +105 -54
- package/lib/fields/index.d.ts +3 -1
- package/lib/fields/index.js +277 -32
- package/lib/fields/json-field.js +36 -16
- package/lib/fields/number-field.js +53 -26
- package/lib/fields/password-field.js +120 -73
- package/lib/fields/radio-field.js +75 -47
- package/lib/fields/relation-field.js +41 -28
- package/lib/fields/sort-field.js +165 -89
- package/lib/fields/string-field.js +24 -9
- package/lib/fields/text-field.js +24 -9
- package/lib/fields/time-field.js +24 -9
- package/lib/fields/uid-field.js +57 -28
- package/lib/fields/uuid-field.d.ts +9 -0
- package/lib/fields/uuid-field.js +39 -0
- package/lib/fields/virtual-field.js +24 -9
- package/lib/filter-parser.js +288 -179
- package/lib/index.js +210 -29
- package/lib/magic-attribute-model.js +123 -71
- package/lib/mock-database.js +68 -34
- package/lib/model-hook.js +101 -60
- package/lib/model.js +116 -81
- package/lib/operators/array.js +136 -96
- package/lib/operators/association.js +30 -14
- package/lib/operators/boolean.d.ts +13 -0
- package/lib/operators/boolean.js +35 -0
- package/lib/operators/date.js +78 -34
- package/lib/operators/empty.js +113 -75
- package/lib/operators/index.js +15 -3
- package/lib/operators/ne.js +27 -12
- package/{esm/operators/ne.d.ts → lib/operators/notIn.d.ts} +2 -2
- package/lib/operators/notIn.js +29 -0
- package/lib/operators/string.js +56 -35
- package/lib/operators/utils.js +18 -10
- package/lib/options-parser.js +323 -215
- package/lib/playground.js +66 -53
- package/lib/relation-repository/belongs-to-many-repository.js +281 -198
- package/lib/relation-repository/belongs-to-repository.js +10 -6
- package/lib/relation-repository/hasmany-repository.js +168 -121
- package/lib/relation-repository/hasone-repository.js +10 -6
- package/lib/relation-repository/multiple-relation-repository.d.ts +3 -3
- package/lib/relation-repository/multiple-relation-repository.js +263 -148
- package/lib/relation-repository/relation-repository.d.ts +1 -1
- package/lib/relation-repository/relation-repository.js +155 -95
- package/lib/relation-repository/single-relation-repository.d.ts +6 -6
- package/lib/relation-repository/single-relation-repository.js +138 -99
- package/lib/relation-repository/types.js +4 -2
- package/lib/repository.d.ts +4 -7
- package/lib/repository.js +465 -291
- package/lib/transaction-decorator.js +80 -67
- package/lib/update-associations.d.ts +1 -2
- package/lib/update-associations.js +525 -321
- package/lib/update-guard.js +160 -117
- package/package.json +3 -8
- package/src/__tests__/fields/uuid-field.test.ts +30 -0
- package/src/__tests__/operator/notIn.test.ts +33 -0
- package/src/__tests__/option-parser.test.ts +20 -0
- package/src/database.ts +51 -4
- package/src/fields/index.ts +3 -0
- package/src/fields/password-field.ts +2 -0
- package/src/fields/uuid-field.ts +21 -0
- package/src/mock-database.ts +1 -1
- package/src/model-hook.ts +1 -1
- package/src/operators/boolean.ts +18 -0
- package/src/operators/index.ts +2 -0
- package/src/operators/notIn.ts +12 -0
- package/src/options-parser.ts +1 -1
- package/src/relation-repository/multiple-relation-repository.ts +8 -6
- package/src/relation-repository/relation-repository.ts +11 -6
- package/src/relation-repository/single-relation-repository.ts +6 -6
- package/src/repository.ts +4 -6
- package/src/update-associations.ts +1 -2
- package/esm/collection-importer.d.ts +0 -7
- package/esm/collection-importer.js +0 -49
- package/esm/collection-importer.js.map +0 -1
- package/esm/collection.d.ts +0 -73
- package/esm/collection.js +0 -224
- package/esm/collection.js.map +0 -1
- package/esm/database.d.ts +0 -101
- package/esm/database.js +0 -275
- package/esm/database.js.map +0 -1
- package/esm/fields/array-field.d.ts +0 -11
- package/esm/fields/array-field.js +0 -26
- package/esm/fields/array-field.js.map +0 -1
- package/esm/fields/belongs-to-field.d.ts +0 -12
- package/esm/fields/belongs-to-field.js +0 -57
- package/esm/fields/belongs-to-field.js.map +0 -1
- package/esm/fields/belongs-to-many-field.d.ts +0 -11
- package/esm/fields/belongs-to-many-field.js +0 -55
- package/esm/fields/belongs-to-many-field.js.map +0 -1
- package/esm/fields/boolean-field.d.ts +0 -8
- package/esm/fields/boolean-field.js +0 -8
- package/esm/fields/boolean-field.js.map +0 -1
- package/esm/fields/context-field.d.ts +0 -13
- package/esm/fields/context-field.js +0 -43
- package/esm/fields/context-field.js.map +0 -1
- package/esm/fields/date-field.d.ts +0 -8
- package/esm/fields/date-field.js +0 -8
- package/esm/fields/date-field.js.map +0 -1
- package/esm/fields/field.d.ts +0 -37
- package/esm/fields/field.js +0 -74
- package/esm/fields/field.js.map +0 -1
- package/esm/fields/has-inverse-field.d.ts +0 -4
- package/esm/fields/has-inverse-field.js +0 -2
- package/esm/fields/has-inverse-field.js.map +0 -1
- package/esm/fields/has-many-field.d.ts +0 -64
- package/esm/fields/has-many-field.js +0 -58
- package/esm/fields/has-many-field.js.map +0 -1
- package/esm/fields/has-one-field.d.ts +0 -64
- package/esm/fields/has-one-field.js +0 -57
- package/esm/fields/has-one-field.js.map +0 -1
- package/esm/fields/index.d.ts +0 -40
- package/esm/fields/index.js +0 -21
- package/esm/fields/index.js.map +0 -1
- package/esm/fields/json-field.d.ts +0 -14
- package/esm/fields/json-field.js +0 -17
- package/esm/fields/json-field.js.map +0 -1
- package/esm/fields/number-field.d.ts +0 -32
- package/esm/fields/number-field.js +0 -28
- package/esm/fields/number-field.js.map +0 -1
- package/esm/fields/password-field.d.ts +0 -21
- package/esm/fields/password-field.js +0 -71
- package/esm/fields/password-field.js.map +0 -1
- package/esm/fields/radio-field.d.ts +0 -14
- package/esm/fields/radio-field.js +0 -49
- package/esm/fields/radio-field.js.map +0 -1
- package/esm/fields/relation-field.d.ts +0 -20
- package/esm/fields/relation-field.js +0 -27
- package/esm/fields/relation-field.js.map +0 -1
- package/esm/fields/sort-field.d.ts +0 -16
- package/esm/fields/sort-field.js +0 -90
- package/esm/fields/sort-field.js.map +0 -1
- package/esm/fields/string-field.d.ts +0 -8
- package/esm/fields/string-field.js +0 -8
- package/esm/fields/string-field.js.map +0 -1
- package/esm/fields/text-field.d.ts +0 -8
- package/esm/fields/text-field.js +0 -8
- package/esm/fields/text-field.js.map +0 -1
- package/esm/fields/time-field.d.ts +0 -8
- package/esm/fields/time-field.js +0 -8
- package/esm/fields/time-field.js.map +0 -1
- package/esm/fields/uid-field.d.ts +0 -10
- package/esm/fields/uid-field.js +0 -27
- package/esm/fields/uid-field.js.map +0 -1
- package/esm/fields/virtual-field.d.ts +0 -8
- package/esm/fields/virtual-field.js +0 -8
- package/esm/fields/virtual-field.js.map +0 -1
- package/esm/filter-parser.d.ts +0 -27
- package/esm/filter-parser.js +0 -185
- package/esm/filter-parser.js.map +0 -1
- package/esm/index.d.ts +0 -15
- package/esm/index.js +0 -16
- package/esm/index.js.map +0 -1
- package/esm/magic-attribute-model.d.ts +0 -7
- package/esm/magic-attribute-model.js +0 -70
- package/esm/magic-attribute-model.js.map +0 -1
- package/esm/mock-database.d.ts +0 -22
- package/esm/mock-database.js +0 -34
- package/esm/mock-database.js.map +0 -1
- package/esm/model-hook.d.ts +0 -12
- package/esm/model-hook.js +0 -61
- package/esm/model-hook.js.map +0 -1
- package/esm/model.d.ts +0 -15
- package/esm/model.js +0 -80
- package/esm/model.js.map +0 -1
- package/esm/operators/array.d.ts +0 -26
- package/esm/operators/array.js +0 -105
- package/esm/operators/array.js.map +0 -1
- package/esm/operators/association.d.ts +0 -10
- package/esm/operators/association.js +0 -14
- package/esm/operators/association.js.map +0 -1
- package/esm/operators/date.d.ts +0 -34
- package/esm/operators/date.js +0 -35
- package/esm/operators/date.js.map +0 -1
- package/esm/operators/empty.d.ts +0 -28
- package/esm/operators/empty.js +0 -58
- package/esm/operators/empty.js.map +0 -1
- package/esm/operators/index.d.ts +0 -2
- package/esm/operators/index.js +0 -2
- package/esm/operators/index.js.map +0 -1
- package/esm/operators/ne.js +0 -12
- package/esm/operators/ne.js.map +0 -1
- package/esm/operators/string.d.ts +0 -21
- package/esm/operators/string.js +0 -35
- package/esm/operators/string.js.map +0 -1
- package/esm/operators/utils.d.ts +0 -4
- package/esm/operators/utils.js +0 -11
- package/esm/operators/utils.js.map +0 -1
- package/esm/options-parser.d.ts +0 -31
- package/esm/options-parser.js +0 -225
- package/esm/options-parser.js.map +0 -1
- package/esm/playground.d.ts +0 -1
- package/esm/playground.js +0 -53
- package/esm/playground.js.map +0 -1
- package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
- package/esm/relation-repository/belongs-to-many-repository.js +0 -199
- package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
- package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
- package/esm/relation-repository/belongs-to-repository.js +0 -4
- package/esm/relation-repository/belongs-to-repository.js.map +0 -1
- package/esm/relation-repository/hasmany-repository.d.ts +0 -23
- package/esm/relation-repository/hasmany-repository.js +0 -125
- package/esm/relation-repository/hasmany-repository.js.map +0 -1
- package/esm/relation-repository/hasone-repository.d.ts +0 -17
- package/esm/relation-repository/hasone-repository.js +0 -4
- package/esm/relation-repository/hasone-repository.js.map +0 -1
- package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
- package/esm/relation-repository/multiple-relation-repository.js +0 -149
- package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
- package/esm/relation-repository/relation-repository.d.ts +0 -32
- package/esm/relation-repository/relation-repository.js +0 -93
- package/esm/relation-repository/relation-repository.js.map +0 -1
- package/esm/relation-repository/single-relation-repository.d.ts +0 -23
- package/esm/relation-repository/single-relation-repository.js +0 -96
- package/esm/relation-repository/single-relation-repository.js.map +0 -1
- package/esm/relation-repository/types.d.ts +0 -7
- package/esm/relation-repository/types.js +0 -2
- package/esm/relation-repository/types.js.map +0 -1
- package/esm/repository.d.ts +0 -165
- package/esm/repository.js +0 -276
- package/esm/repository.js.map +0 -1
- package/esm/transaction-decorator.d.ts +0 -1
- package/esm/transaction-decorator.js +0 -63
- package/esm/transaction-decorator.js.map +0 -1
- package/esm/update-associations.d.ts +0 -60
- package/esm/update-associations.js +0 -362
- package/esm/update-associations.js.map +0 -1
- package/esm/update-guard.d.ts +0 -26
- package/esm/update-guard.js +0 -122
- package/esm/update-guard.js.map +0 -1
- package/lib/collection-importer.js.map +0 -1
- package/lib/collection.js.map +0 -1
- package/lib/database.js.map +0 -1
- package/lib/fields/array-field.js.map +0 -1
- package/lib/fields/belongs-to-field.js.map +0 -1
- package/lib/fields/belongs-to-many-field.js.map +0 -1
- package/lib/fields/boolean-field.js.map +0 -1
- package/lib/fields/context-field.js.map +0 -1
- package/lib/fields/date-field.js.map +0 -1
- package/lib/fields/field.js.map +0 -1
- package/lib/fields/has-inverse-field.js.map +0 -1
- package/lib/fields/has-many-field.js.map +0 -1
- package/lib/fields/has-one-field.js.map +0 -1
- package/lib/fields/index.js.map +0 -1
- package/lib/fields/json-field.js.map +0 -1
- package/lib/fields/number-field.js.map +0 -1
- package/lib/fields/password-field.js.map +0 -1
- package/lib/fields/radio-field.js.map +0 -1
- package/lib/fields/relation-field.js.map +0 -1
- package/lib/fields/sort-field.js.map +0 -1
- package/lib/fields/string-field.js.map +0 -1
- package/lib/fields/text-field.js.map +0 -1
- package/lib/fields/time-field.js.map +0 -1
- package/lib/fields/uid-field.js.map +0 -1
- package/lib/fields/virtual-field.js.map +0 -1
- package/lib/filter-parser.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/magic-attribute-model.js.map +0 -1
- package/lib/mock-database.js.map +0 -1
- package/lib/model-hook.js.map +0 -1
- package/lib/model.js.map +0 -1
- package/lib/operators/array.js.map +0 -1
- package/lib/operators/association.js.map +0 -1
- package/lib/operators/date.js.map +0 -1
- package/lib/operators/empty.js.map +0 -1
- package/lib/operators/index.js.map +0 -1
- package/lib/operators/ne.js.map +0 -1
- package/lib/operators/string.js.map +0 -1
- package/lib/operators/utils.js.map +0 -1
- package/lib/options-parser.js.map +0 -1
- package/lib/playground.js.map +0 -1
- package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
- package/lib/relation-repository/belongs-to-repository.js.map +0 -1
- package/lib/relation-repository/hasmany-repository.js.map +0 -1
- package/lib/relation-repository/hasone-repository.js.map +0 -1
- package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
- package/lib/relation-repository/relation-repository.js.map +0 -1
- package/lib/relation-repository/single-relation-repository.js.map +0 -1
- package/lib/relation-repository/types.js.map +0 -1
- package/lib/repository.js.map +0 -1
- package/lib/transaction-decorator.js.map +0 -1
- package/lib/update-associations.js.map +0 -1
- package/lib/update-guard.js.map +0 -1
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
|
@@ -1,151 +1,228 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
exports.updateMultipleAssociation =
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
45
|
+
return typeof value === 'undefined' || value === null;
|
|
17
46
|
}
|
|
47
|
+
|
|
18
48
|
function isStringOrNumber(value) {
|
|
19
|
-
|
|
49
|
+
return typeof value === 'string' || typeof value === 'number';
|
|
20
50
|
}
|
|
51
|
+
|
|
21
52
|
function getKeysByPrefix(keys, prefix) {
|
|
22
|
-
|
|
53
|
+
return keys.filter(key => key.startsWith(`${prefix}.`)).map(key => key.substring(prefix.length + 1));
|
|
23
54
|
}
|
|
55
|
+
|
|
24
56
|
function modelAssociations(instance) {
|
|
25
|
-
|
|
57
|
+
return instance.constructor.associations;
|
|
26
58
|
}
|
|
27
|
-
|
|
59
|
+
|
|
28
60
|
function belongsToManyAssociations(instance) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
72
|
+
|
|
40
73
|
function modelAssociationByKey(instance, key) {
|
|
41
|
-
|
|
74
|
+
return modelAssociations(instance)[key];
|
|
42
75
|
}
|
|
43
|
-
|
|
44
|
-
function updateModelByValues(
|
|
45
|
-
|
|
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
|
-
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
128
|
-
function
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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
|
-
|
|
374
|
-
|
|
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
|
+
}
|