@nocobase/database 0.9.3-alpha.1 → 0.9.4-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.d.ts +9 -9
- package/lib/collection.js +100 -96
- package/lib/database.d.ts +4 -4
- package/lib/database.js +25 -53
- package/lib/eager-loading/eager-loading-tree.d.ts +23 -0
- package/lib/eager-loading/eager-loading-tree.js +338 -0
- package/lib/filter-parser.d.ts +1 -7
- package/lib/filter-parser.js +27 -7
- package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +5 -0
- package/lib/listeners/append-child-collection-name-after-repository-find.js +40 -0
- package/lib/listeners/index.js +2 -0
- package/lib/mock-database.js +3 -1
- package/lib/operators/string.js +1 -1
- package/lib/options-parser.js +4 -0
- package/lib/query-interface/postgres-query-interface.js +2 -2
- package/lib/relation-repository/belongs-to-many-repository.d.ts +2 -1
- package/lib/relation-repository/belongs-to-many-repository.js +58 -37
- package/lib/relation-repository/hasmany-repository.d.ts +2 -1
- package/lib/relation-repository/hasmany-repository.js +31 -16
- package/lib/relation-repository/multiple-relation-repository.js +8 -26
- package/lib/relation-repository/relation-repository.d.ts +1 -7
- package/lib/relation-repository/single-relation-repository.d.ts +1 -1
- package/lib/relation-repository/single-relation-repository.js +10 -16
- package/lib/repository.d.ts +11 -8
- package/lib/repository.js +104 -89
- package/lib/sql-parser/postgres.js +41 -0
- package/lib/update-guard.d.ts +1 -1
- package/lib/update-guard.js +16 -13
- package/lib/utils.d.ts +0 -7
- package/lib/utils.js +0 -76
- package/package.json +4 -4
- package/src/__tests__/eager-loading/eager-loading-tree.test.ts +393 -0
- package/src/__tests__/migrator.test.ts +4 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +1 -0
- package/src/__tests__/repository/aggregation.test.ts +297 -0
- package/src/__tests__/repository/count.test.ts +1 -1
- package/src/__tests__/repository/find.test.ts +10 -1
- package/src/__tests__/repository.test.ts +30 -0
- package/src/__tests__/update-guard.test.ts +13 -0
- package/src/collection.ts +74 -66
- package/src/database.ts +26 -42
- package/src/eager-loading/eager-loading-tree.ts +304 -0
- package/src/filter-parser.ts +16 -2
- package/src/listeners/adjacency-list.ts +1 -3
- package/src/listeners/append-child-collection-name-after-repository-find.ts +31 -0
- package/src/listeners/index.ts +2 -0
- package/src/mock-database.ts +3 -1
- package/src/operators/notIn.ts +1 -0
- package/src/operators/string.ts +1 -1
- package/src/options-parser.ts +5 -0
- package/src/query-interface/postgres-query-interface.ts +1 -1
- package/src/relation-repository/belongs-to-many-repository.ts +33 -1
- package/src/relation-repository/hasmany-repository.ts +17 -0
- package/src/relation-repository/multiple-relation-repository.ts +14 -19
- package/src/relation-repository/single-relation-repository.ts +13 -15
- package/src/repository.ts +79 -36
- package/src/sql-parser/postgres.js +25505 -0
- package/src/update-guard.ts +21 -16
- package/src/utils.ts +0 -61
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.EagerLoadingTree = void 0;
|
|
7
|
+
function _lodash() {
|
|
8
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
9
|
+
_lodash = function _lodash() {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
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; }
|
|
16
|
+
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; }
|
|
17
|
+
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; }
|
|
18
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
19
|
+
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); }
|
|
20
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
21
|
+
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); }); }; }
|
|
22
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
23
|
+
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); }
|
|
24
|
+
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; }
|
|
25
|
+
class EagerLoadingTree {
|
|
26
|
+
constructor(root) {
|
|
27
|
+
this.root = void 0;
|
|
28
|
+
this.root = root;
|
|
29
|
+
}
|
|
30
|
+
static buildFromSequelizeOptions(options) {
|
|
31
|
+
const model = options.model,
|
|
32
|
+
rootAttributes = options.rootAttributes,
|
|
33
|
+
includeOption = options.includeOption;
|
|
34
|
+
const root = {
|
|
35
|
+
model,
|
|
36
|
+
association: null,
|
|
37
|
+
rawAttributes: _lodash().default.cloneDeep(rootAttributes),
|
|
38
|
+
attributes: _lodash().default.cloneDeep(rootAttributes),
|
|
39
|
+
order: options.rootOrder,
|
|
40
|
+
children: []
|
|
41
|
+
};
|
|
42
|
+
const pushAttribute = (node, attribute) => {
|
|
43
|
+
if (_lodash().default.isArray(node.attributes) && !node.attributes.includes(attribute)) {
|
|
44
|
+
node.attributes.push(attribute);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const traverseIncludeOption = (includeOption, eagerLoadingTreeParent) => {
|
|
48
|
+
const includeOptions = _lodash().default.castArray(includeOption);
|
|
49
|
+
if (includeOption.length > 0) {
|
|
50
|
+
const modelPrimaryKey = eagerLoadingTreeParent.model.primaryKeyAttribute;
|
|
51
|
+
pushAttribute(eagerLoadingTreeParent, modelPrimaryKey);
|
|
52
|
+
}
|
|
53
|
+
var _iterator = _createForOfIteratorHelper(includeOptions),
|
|
54
|
+
_step;
|
|
55
|
+
try {
|
|
56
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
57
|
+
const include = _step.value;
|
|
58
|
+
// skip fromFilter include option
|
|
59
|
+
if (include.fromFilter) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const association = eagerLoadingTreeParent.model.associations[include.association];
|
|
63
|
+
const associationType = association.associationType;
|
|
64
|
+
const child = {
|
|
65
|
+
model: association.target,
|
|
66
|
+
association,
|
|
67
|
+
rawAttributes: _lodash().default.cloneDeep(include.attributes),
|
|
68
|
+
attributes: _lodash().default.cloneDeep(include.attributes),
|
|
69
|
+
parent: eagerLoadingTreeParent,
|
|
70
|
+
children: []
|
|
71
|
+
};
|
|
72
|
+
if (associationType == 'HasOne' || associationType == 'HasMany') {
|
|
73
|
+
const sourceKey = association.sourceKey,
|
|
74
|
+
foreignKey = association.foreignKey;
|
|
75
|
+
pushAttribute(eagerLoadingTreeParent, sourceKey);
|
|
76
|
+
pushAttribute(child, foreignKey);
|
|
77
|
+
}
|
|
78
|
+
if (associationType == 'BelongsTo') {
|
|
79
|
+
const targetKey = association.targetKey,
|
|
80
|
+
foreignKey = association.foreignKey;
|
|
81
|
+
pushAttribute(eagerLoadingTreeParent, foreignKey);
|
|
82
|
+
pushAttribute(child, targetKey);
|
|
83
|
+
}
|
|
84
|
+
if (associationType == 'BelongsToMany') {
|
|
85
|
+
const sourceKey = association.sourceKey;
|
|
86
|
+
pushAttribute(eagerLoadingTreeParent, sourceKey);
|
|
87
|
+
}
|
|
88
|
+
eagerLoadingTreeParent.children.push(child);
|
|
89
|
+
if (include.include) {
|
|
90
|
+
traverseIncludeOption(include.include, child);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
_iterator.e(err);
|
|
95
|
+
} finally {
|
|
96
|
+
_iterator.f();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
traverseIncludeOption(includeOption, root);
|
|
100
|
+
return new EagerLoadingTree(root);
|
|
101
|
+
}
|
|
102
|
+
load(pks, transaction) {
|
|
103
|
+
var _this = this;
|
|
104
|
+
return _asyncToGenerator(function* () {
|
|
105
|
+
const result = {};
|
|
106
|
+
const loadRecursive = /*#__PURE__*/function () {
|
|
107
|
+
var _ref = _asyncToGenerator(function* (node, ids) {
|
|
108
|
+
const modelPrimaryKey = node.model.primaryKeyAttribute;
|
|
109
|
+
let instances = [];
|
|
110
|
+
// load instances from database
|
|
111
|
+
if (!node.parent) {
|
|
112
|
+
const findOptions = {
|
|
113
|
+
where: {
|
|
114
|
+
[modelPrimaryKey]: ids
|
|
115
|
+
},
|
|
116
|
+
attributes: node.attributes
|
|
117
|
+
};
|
|
118
|
+
if (node.order) {
|
|
119
|
+
findOptions['order'] = node.order;
|
|
120
|
+
}
|
|
121
|
+
instances = yield node.model.findAll(_objectSpread(_objectSpread({}, findOptions), {}, {
|
|
122
|
+
transaction
|
|
123
|
+
}));
|
|
124
|
+
} else if (ids.length > 0) {
|
|
125
|
+
const association = node.association;
|
|
126
|
+
const associationType = association.associationType;
|
|
127
|
+
if (associationType == 'HasOne' || associationType == 'HasMany') {
|
|
128
|
+
const foreignKey = association.foreignKey;
|
|
129
|
+
const foreignKeyValues = node.parent.instances.map(instance => instance.get(association.sourceKey));
|
|
130
|
+
const findOptions = {
|
|
131
|
+
where: {
|
|
132
|
+
[foreignKey]: foreignKeyValues
|
|
133
|
+
},
|
|
134
|
+
attributes: node.attributes,
|
|
135
|
+
transaction
|
|
136
|
+
};
|
|
137
|
+
instances = yield node.model.findAll(findOptions);
|
|
138
|
+
}
|
|
139
|
+
if (associationType == 'BelongsTo') {
|
|
140
|
+
const foreignKey = association.foreignKey;
|
|
141
|
+
const parentInstancesForeignKeyValues = node.parent.instances.map(instance => instance.get(foreignKey));
|
|
142
|
+
instances = yield node.model.findAll({
|
|
143
|
+
transaction,
|
|
144
|
+
where: {
|
|
145
|
+
[association.targetKey]: parentInstancesForeignKeyValues
|
|
146
|
+
},
|
|
147
|
+
attributes: node.attributes
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (associationType == 'BelongsToMany') {
|
|
151
|
+
const foreignKeyValues = node.parent.instances.map(instance => instance.get(association.sourceKey));
|
|
152
|
+
instances = yield node.model.findAll({
|
|
153
|
+
transaction,
|
|
154
|
+
attributes: node.attributes,
|
|
155
|
+
include: [{
|
|
156
|
+
association: association.oneFromTarget,
|
|
157
|
+
where: {
|
|
158
|
+
[association.foreignKey]: foreignKeyValues
|
|
159
|
+
}
|
|
160
|
+
}]
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
node.instances = instances;
|
|
165
|
+
var _iterator2 = _createForOfIteratorHelper(node.children),
|
|
166
|
+
_step2;
|
|
167
|
+
try {
|
|
168
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
169
|
+
const child = _step2.value;
|
|
170
|
+
const nodeIds = instances.map(instance => instance.get(modelPrimaryKey));
|
|
171
|
+
yield loadRecursive(child, nodeIds);
|
|
172
|
+
}
|
|
173
|
+
// merge instances to parent
|
|
174
|
+
} catch (err) {
|
|
175
|
+
_iterator2.e(err);
|
|
176
|
+
} finally {
|
|
177
|
+
_iterator2.f();
|
|
178
|
+
}
|
|
179
|
+
if (!node.parent) {
|
|
180
|
+
return;
|
|
181
|
+
} else {
|
|
182
|
+
const association = node.association;
|
|
183
|
+
const associationType = association.associationType;
|
|
184
|
+
const setParentAccessor = parentInstance => {
|
|
185
|
+
const key = association.as;
|
|
186
|
+
const children = parentInstance.getDataValue(association.as);
|
|
187
|
+
if (association.isSingleAssociation) {
|
|
188
|
+
const isEmpty = !children;
|
|
189
|
+
parentInstance[key] = parentInstance.dataValues[key] = isEmpty ? null : children;
|
|
190
|
+
} else {
|
|
191
|
+
const isEmpty = !children || children.length == 0;
|
|
192
|
+
parentInstance[key] = parentInstance.dataValues[key] = isEmpty ? [] : children;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
if (associationType == 'HasMany' || associationType == 'HasOne') {
|
|
196
|
+
const foreignKey = association.foreignKey;
|
|
197
|
+
const sourceKey = association.sourceKey;
|
|
198
|
+
var _iterator3 = _createForOfIteratorHelper(node.instances),
|
|
199
|
+
_step3;
|
|
200
|
+
try {
|
|
201
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
202
|
+
const instance = _step3.value;
|
|
203
|
+
const parentInstance = node.parent.instances.find(parentInstance => parentInstance.get(sourceKey) == instance.get(foreignKey));
|
|
204
|
+
if (parentInstance) {
|
|
205
|
+
if (associationType == 'HasMany') {
|
|
206
|
+
const children = parentInstance.getDataValue(association.as);
|
|
207
|
+
if (!children) {
|
|
208
|
+
parentInstance.setDataValue(association.as, [instance]);
|
|
209
|
+
} else {
|
|
210
|
+
children.push(instance);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (associationType == 'HasOne') {
|
|
214
|
+
parentInstance.setDataValue(association.as, instance);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
} catch (err) {
|
|
219
|
+
_iterator3.e(err);
|
|
220
|
+
} finally {
|
|
221
|
+
_iterator3.f();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (associationType == 'BelongsTo') {
|
|
225
|
+
const foreignKey = association.foreignKey;
|
|
226
|
+
const targetKey = association.targetKey;
|
|
227
|
+
var _iterator4 = _createForOfIteratorHelper(node.instances),
|
|
228
|
+
_step4;
|
|
229
|
+
try {
|
|
230
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
231
|
+
const instance = _step4.value;
|
|
232
|
+
const parentInstances = node.parent.instances.filter(parentInstance => parentInstance.get(foreignKey) == instance.get(targetKey));
|
|
233
|
+
var _iterator5 = _createForOfIteratorHelper(parentInstances),
|
|
234
|
+
_step5;
|
|
235
|
+
try {
|
|
236
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
237
|
+
const parentInstance = _step5.value;
|
|
238
|
+
parentInstance.setDataValue(association.as, instance);
|
|
239
|
+
}
|
|
240
|
+
} catch (err) {
|
|
241
|
+
_iterator5.e(err);
|
|
242
|
+
} finally {
|
|
243
|
+
_iterator5.f();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} catch (err) {
|
|
247
|
+
_iterator4.e(err);
|
|
248
|
+
} finally {
|
|
249
|
+
_iterator4.f();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (associationType == 'BelongsToMany') {
|
|
253
|
+
const sourceKey = association.sourceKey;
|
|
254
|
+
const foreignKey = association.foreignKey;
|
|
255
|
+
const oneFromTarget = association.oneFromTarget;
|
|
256
|
+
var _iterator6 = _createForOfIteratorHelper(node.instances),
|
|
257
|
+
_step6;
|
|
258
|
+
try {
|
|
259
|
+
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
260
|
+
const instance = _step6.value;
|
|
261
|
+
const parentInstance = node.parent.instances.find(parentInstance => parentInstance.get(sourceKey) == instance[oneFromTarget.as].get(foreignKey));
|
|
262
|
+
if (parentInstance) {
|
|
263
|
+
const children = parentInstance.getDataValue(association.as);
|
|
264
|
+
if (!children) {
|
|
265
|
+
parentInstance.setDataValue(association.as, [instance]);
|
|
266
|
+
} else {
|
|
267
|
+
children.push(instance);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
} catch (err) {
|
|
272
|
+
_iterator6.e(err);
|
|
273
|
+
} finally {
|
|
274
|
+
_iterator6.f();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
var _iterator7 = _createForOfIteratorHelper(node.parent.instances),
|
|
278
|
+
_step7;
|
|
279
|
+
try {
|
|
280
|
+
for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
|
|
281
|
+
const parent = _step7.value;
|
|
282
|
+
setParentAccessor(parent);
|
|
283
|
+
}
|
|
284
|
+
} catch (err) {
|
|
285
|
+
_iterator7.e(err);
|
|
286
|
+
} finally {
|
|
287
|
+
_iterator7.f();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
return function loadRecursive(_x, _x2) {
|
|
292
|
+
return _ref.apply(this, arguments);
|
|
293
|
+
};
|
|
294
|
+
}();
|
|
295
|
+
yield loadRecursive(_this.root, pks);
|
|
296
|
+
const setInstanceAttributes = node => {
|
|
297
|
+
const nodeRawAttributes = node.rawAttributes;
|
|
298
|
+
if (!_lodash().default.isArray(nodeRawAttributes)) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const nodeChildrenAs = node.children.map(child => child.association.as);
|
|
302
|
+
const includeAttributes = [...nodeRawAttributes, ...nodeChildrenAs];
|
|
303
|
+
var _iterator8 = _createForOfIteratorHelper(node.instances),
|
|
304
|
+
_step8;
|
|
305
|
+
try {
|
|
306
|
+
for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
|
|
307
|
+
const instance = _step8.value;
|
|
308
|
+
const attributes = _lodash().default.pick(instance.dataValues, includeAttributes);
|
|
309
|
+
instance.dataValues = attributes;
|
|
310
|
+
}
|
|
311
|
+
} catch (err) {
|
|
312
|
+
_iterator8.e(err);
|
|
313
|
+
} finally {
|
|
314
|
+
_iterator8.f();
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
// traverse tree and set instance attributes
|
|
318
|
+
const traverse = node => {
|
|
319
|
+
setInstanceAttributes(node);
|
|
320
|
+
var _iterator9 = _createForOfIteratorHelper(node.children),
|
|
321
|
+
_step9;
|
|
322
|
+
try {
|
|
323
|
+
for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
|
|
324
|
+
const child = _step9.value;
|
|
325
|
+
traverse(child);
|
|
326
|
+
}
|
|
327
|
+
} catch (err) {
|
|
328
|
+
_iterator9.e(err);
|
|
329
|
+
} finally {
|
|
330
|
+
_iterator9.f();
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
traverse(_this.root);
|
|
334
|
+
return result;
|
|
335
|
+
})();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
exports.EagerLoadingTree = EagerLoadingTree;
|
package/lib/filter-parser.d.ts
CHANGED
|
@@ -15,13 +15,7 @@ export default class FilterParser {
|
|
|
15
15
|
context: FilterParserContext;
|
|
16
16
|
constructor(filter: FilterType, context: FilterParserContext);
|
|
17
17
|
prepareFilter(filter: FilterType): any;
|
|
18
|
-
toSequelizeParams():
|
|
19
|
-
where?: undefined;
|
|
20
|
-
include?: undefined;
|
|
21
|
-
} | {
|
|
22
|
-
where: {};
|
|
23
|
-
include: any[];
|
|
24
|
-
};
|
|
18
|
+
toSequelizeParams(): any;
|
|
25
19
|
private getFieldNameFromQueryPath;
|
|
26
20
|
}
|
|
27
21
|
export {};
|
package/lib/filter-parser.js
CHANGED
|
@@ -215,20 +215,40 @@ class FilterParser {
|
|
|
215
215
|
});
|
|
216
216
|
};
|
|
217
217
|
debug('where %o, include %o', where, include);
|
|
218
|
-
|
|
218
|
+
const results = {
|
|
219
219
|
where,
|
|
220
220
|
include: toInclude(include)
|
|
221
221
|
};
|
|
222
|
+
//traverse filter include, set fromFiler to true
|
|
223
|
+
const traverseInclude = include => {
|
|
224
|
+
var _iterator = _createForOfIteratorHelper(include),
|
|
225
|
+
_step;
|
|
226
|
+
try {
|
|
227
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
228
|
+
const item = _step.value;
|
|
229
|
+
if (item.include) {
|
|
230
|
+
traverseInclude(item.include);
|
|
231
|
+
}
|
|
232
|
+
item.fromFilter = true;
|
|
233
|
+
}
|
|
234
|
+
} catch (err) {
|
|
235
|
+
_iterator.e(err);
|
|
236
|
+
} finally {
|
|
237
|
+
_iterator.f();
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
traverseInclude(results.include);
|
|
241
|
+
return results;
|
|
222
242
|
}
|
|
223
243
|
getFieldNameFromQueryPath(queryPath) {
|
|
224
244
|
const paths = queryPath.split('.');
|
|
225
245
|
let fieldName;
|
|
226
246
|
const fullPaths = [];
|
|
227
|
-
var
|
|
228
|
-
|
|
247
|
+
var _iterator2 = _createForOfIteratorHelper(paths),
|
|
248
|
+
_step2;
|
|
229
249
|
try {
|
|
230
|
-
for (
|
|
231
|
-
const path =
|
|
250
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
251
|
+
const path = _step2.value;
|
|
232
252
|
if (path.startsWith('$') || !_lodash().default.isNaN(parseInt(path))) {
|
|
233
253
|
continue;
|
|
234
254
|
}
|
|
@@ -236,9 +256,9 @@ class FilterParser {
|
|
|
236
256
|
fieldName = path;
|
|
237
257
|
}
|
|
238
258
|
} catch (err) {
|
|
239
|
-
|
|
259
|
+
_iterator2.e(err);
|
|
240
260
|
} finally {
|
|
241
|
-
|
|
261
|
+
_iterator2.f();
|
|
242
262
|
}
|
|
243
263
|
return [fieldName, fullPaths.join('.')];
|
|
244
264
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.appendChildCollectionNameAfterRepositoryFind = void 0;
|
|
7
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
8
|
+
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); }
|
|
9
|
+
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; }
|
|
10
|
+
const appendChildCollectionNameAfterRepositoryFind = db => {
|
|
11
|
+
return ({
|
|
12
|
+
findOptions,
|
|
13
|
+
dataCollection,
|
|
14
|
+
data
|
|
15
|
+
}) => {
|
|
16
|
+
if (dataCollection.isParent()) {
|
|
17
|
+
var _iterator = _createForOfIteratorHelper(data),
|
|
18
|
+
_step;
|
|
19
|
+
try {
|
|
20
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
21
|
+
const row = _step.value;
|
|
22
|
+
const rowCollection = db.tableNameCollectionMap.get(findOptions.raw ? `${row['__schemaName']}.${row['__tableName']}` : `${row.get('__schemaName')}.${row.get('__tableName')}`);
|
|
23
|
+
if (!rowCollection) {
|
|
24
|
+
db.logger.warn(`Can not find collection by table name ${JSON.stringify(row)}, current collections: ${Array.from(db.tableNameCollectionMap.keys()).join(', ')}`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const rowCollectionName = rowCollection.name;
|
|
28
|
+
findOptions.raw ? row['__collection'] = rowCollectionName : row.set('__collection', rowCollectionName, {
|
|
29
|
+
raw: true
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
} catch (err) {
|
|
33
|
+
_iterator.e(err);
|
|
34
|
+
} finally {
|
|
35
|
+
_iterator.f();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.appendChildCollectionNameAfterRepositoryFind = appendChildCollectionNameAfterRepositoryFind;
|
package/lib/listeners/index.js
CHANGED
|
@@ -5,7 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.registerBuiltInListeners = void 0;
|
|
7
7
|
var _adjacencyList = require("./adjacency-list");
|
|
8
|
+
var _appendChildCollectionNameAfterRepositoryFind = require("./append-child-collection-name-after-repository-find");
|
|
8
9
|
const registerBuiltInListeners = db => {
|
|
9
10
|
db.on('beforeDefineCollection', _adjacencyList.beforeDefineAdjacencyListCollection);
|
|
11
|
+
db.on('afterRepositoryFind', (0, _appendChildCollectionNameAfterRepositoryFind.appendChildCollectionNameAfterRepositoryFind)(db));
|
|
10
12
|
};
|
|
11
13
|
exports.registerBuiltInListeners = registerBuiltInListeners;
|
package/lib/mock-database.js
CHANGED
|
@@ -62,7 +62,9 @@ function getConfigByEnv() {
|
|
|
62
62
|
}
|
|
63
63
|
function customLogger(queryString, queryObject) {
|
|
64
64
|
console.log(queryString); // outputs a string
|
|
65
|
-
|
|
65
|
+
if (queryObject.bind) {
|
|
66
|
+
console.log(queryObject.bind); // outputs an array
|
|
67
|
+
}
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
function mockDatabase(options = {}) {
|
package/lib/operators/string.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
var _utils = require("./utils");
|
|
8
7
|
function _sequelize() {
|
|
9
8
|
const data = require("sequelize");
|
|
10
9
|
_sequelize = function _sequelize() {
|
|
@@ -12,6 +11,7 @@ function _sequelize() {
|
|
|
12
11
|
};
|
|
13
12
|
return data;
|
|
14
13
|
}
|
|
14
|
+
var _utils = require("./utils");
|
|
15
15
|
var _default = {
|
|
16
16
|
$includes(value, ctx) {
|
|
17
17
|
return {
|
package/lib/options-parser.js
CHANGED
|
@@ -266,6 +266,10 @@ class OptionsParser {
|
|
|
266
266
|
queryParams['include'] = [];
|
|
267
267
|
}
|
|
268
268
|
let existIncludeIndex = queryParams['include'].findIndex(include => include['association'] == appendAssociation);
|
|
269
|
+
// if include from filter, remove fromFilter attribute
|
|
270
|
+
if (existIncludeIndex != -1) {
|
|
271
|
+
delete queryParams['include'][existIncludeIndex]['fromFilter'];
|
|
272
|
+
}
|
|
269
273
|
// if association not exist, create it
|
|
270
274
|
if (existIncludeIndex == -1) {
|
|
271
275
|
// association not exists
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var _queryInterface = _interopRequireDefault(require("./query-interface"));
|
|
8
|
-
var
|
|
8
|
+
var _postgres = _interopRequireDefault(require("../sql-parser/postgres"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
10
|
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; }
|
|
11
11
|
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; }
|
|
@@ -71,7 +71,7 @@ class PostgresQueryInterface extends _queryInterface.default {
|
|
|
71
71
|
});
|
|
72
72
|
const def = viewDefQuery[0]['definition'];
|
|
73
73
|
try {
|
|
74
|
-
const _sqlParser$parse =
|
|
74
|
+
const _sqlParser$parse = _postgres.default.parse(def),
|
|
75
75
|
ast = _sqlParser$parse.ast;
|
|
76
76
|
const columns = ast[0].columns;
|
|
77
77
|
const usages = columns.map(column => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Transaction } from 'sequelize';
|
|
2
2
|
import { Model } from '../model';
|
|
3
|
-
import { CreateOptions, DestroyOptions, FindOneOptions, FindOptions, TargetKey, UpdateOptions } from '../repository';
|
|
3
|
+
import { AggregateOptions, CreateOptions, DestroyOptions, FindOneOptions, FindOptions, TargetKey, UpdateOptions } from '../repository';
|
|
4
4
|
import { FindAndCountOptions, MultipleRelationRepository } from './multiple-relation-repository';
|
|
5
5
|
import { AssociatedOptions, PrimaryKeyWithThroughValues } from './types';
|
|
6
6
|
declare type CreateBelongsToManyOptions = CreateOptions;
|
|
@@ -20,6 +20,7 @@ interface IBelongsToManyRepository<M extends Model> {
|
|
|
20
20
|
}): Promise<void>;
|
|
21
21
|
}
|
|
22
22
|
export declare class BelongsToManyRepository extends MultipleRelationRepository implements IBelongsToManyRepository<any> {
|
|
23
|
+
aggregate(options: AggregateOptions): Promise<any>;
|
|
23
24
|
create(options?: CreateBelongsToManyOptions): Promise<any>;
|
|
24
25
|
destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<boolean>;
|
|
25
26
|
protected setTargets(call: 'add' | 'set', options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|