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