@loopback/repository 2.6.0 → 2.10.0
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/CHANGELOG.md +63 -0
- package/README.md +1 -1
- package/dist/decorators/metadata.d.ts +1 -1
- package/dist/decorators/metadata.js +6 -6
- package/dist/decorators/metadata.js.map +1 -1
- package/dist/decorators/model.decorator.d.ts +1 -1
- package/dist/decorators/model.decorator.js +10 -10
- package/dist/decorators/model.decorator.js.map +1 -1
- package/dist/decorators/repository.decorator.js +4 -4
- package/dist/decorators/repository.decorator.js.map +1 -1
- package/dist/define-model-class.d.ts +2 -2
- package/dist/mixins/repository.mixin.d.ts +23 -12
- package/dist/mixins/repository.mixin.js +13 -7
- package/dist/mixins/repository.mixin.js.map +1 -1
- package/dist/model.d.ts +19 -2
- package/dist/model.js +46 -2
- package/dist/model.js.map +1 -1
- package/dist/query.d.ts +30 -0
- package/dist/query.js +50 -0
- package/dist/query.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.decorator.js +2 -2
- package/dist/relations/belongs-to/belongs-to.decorator.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.helpers.d.ts +1 -0
- package/dist/relations/belongs-to/belongs-to.helpers.js +13 -6
- package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.repository.d.ts +1 -1
- package/dist/relations/has-many/has-many-through-repository.factory.d.ts +11 -0
- package/dist/relations/has-many/has-many-through-repository.factory.js +31 -0
- package/dist/relations/has-many/has-many-through-repository.factory.js.map +1 -0
- package/dist/relations/has-many/has-many-through.helpers.d.ts +122 -14
- package/dist/relations/has-many/has-many-through.helpers.js +153 -20
- package/dist/relations/has-many/has-many-through.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository.d.ts +39 -2
- package/dist/relations/has-many/has-many-through.repository.js +87 -0
- package/dist/relations/has-many/has-many-through.repository.js.map +1 -1
- package/dist/relations/has-many/has-many.helpers.js +2 -1
- package/dist/relations/has-many/has-many.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many.repository.d.ts +1 -1
- package/dist/relations/has-many/index.d.ts +4 -2
- package/dist/relations/has-many/index.js +4 -2
- package/dist/relations/has-many/index.js.map +1 -1
- package/dist/relations/has-one/has-one.helpers.js +2 -1
- package/dist/relations/has-one/has-one.helpers.js.map +1 -1
- package/dist/relations/has-one/has-one.repository.d.ts +1 -1
- package/dist/relations/relation.decorator.js +6 -6
- package/dist/relations/relation.decorator.js.map +1 -1
- package/dist/relations/relation.types.d.ts +2 -2
- package/dist/relations/relation.types.js +2 -2
- package/dist/relations/relation.types.js.map +1 -1
- package/dist/repositories/constraint-utils.d.ts +10 -0
- package/dist/repositories/constraint-utils.js +16 -1
- package/dist/repositories/constraint-utils.js.map +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +33 -2
- package/dist/repositories/legacy-juggler-bridge.js +34 -0
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/package.json +15 -13
- package/src/decorators/metadata.ts +1 -1
- package/src/decorators/model.decorator.ts +1 -1
- package/src/decorators/repository.decorator.ts +1 -1
- package/src/define-model-class.ts +2 -2
- package/src/mixins/repository.mixin.ts +8 -2
- package/src/model.ts +64 -4
- package/src/query.ts +55 -0
- package/src/relations/belongs-to/belongs-to.decorator.ts +1 -1
- package/src/relations/belongs-to/belongs-to.helpers.ts +19 -8
- package/src/relations/belongs-to/belongs-to.repository.ts +1 -1
- package/src/relations/has-many/has-many-through-repository.factory.ts +104 -0
- package/src/relations/has-many/has-many-through.helpers.ts +182 -21
- package/src/relations/has-many/has-many-through.repository.ts +191 -2
- package/src/relations/has-many/has-many.helpers.ts +1 -2
- package/src/relations/has-many/has-many.repository.ts +1 -1
- package/src/relations/has-many/index.ts +4 -2
- package/src/relations/has-one/has-one.helpers.ts +1 -2
- package/src/relations/has-one/has-one.repository.ts +1 -1
- package/src/relations/relation.decorator.ts +1 -1
- package/src/relations/relation.types.ts +2 -2
- package/src/repositories/constraint-utils.ts +17 -0
- package/src/repositories/legacy-juggler-bridge.ts +59 -1
package/dist/query.js
CHANGED
|
@@ -213,6 +213,56 @@ class WhereBuilder {
|
|
|
213
213
|
}
|
|
214
214
|
return this;
|
|
215
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Add a `like` condition
|
|
218
|
+
* @param key - Property name
|
|
219
|
+
* @param val - Regexp condition
|
|
220
|
+
*/
|
|
221
|
+
like(key, val) {
|
|
222
|
+
const w = {};
|
|
223
|
+
w[key] = { like: val };
|
|
224
|
+
return this.add(w);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Add a `nlike` condition
|
|
228
|
+
* @param key - Property name
|
|
229
|
+
* @param val - Regexp condition
|
|
230
|
+
*/
|
|
231
|
+
nlike(key, val) {
|
|
232
|
+
const w = {};
|
|
233
|
+
w[key] = { nlike: val };
|
|
234
|
+
return this.add(w);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Add a `ilike` condition
|
|
238
|
+
* @param key - Property name
|
|
239
|
+
* @param val - Regexp condition
|
|
240
|
+
*/
|
|
241
|
+
ilike(key, val) {
|
|
242
|
+
const w = {};
|
|
243
|
+
w[key] = { ilike: val };
|
|
244
|
+
return this.add(w);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Add a `nilike` condition
|
|
248
|
+
* @param key - Property name
|
|
249
|
+
* @param val - Regexp condition
|
|
250
|
+
*/
|
|
251
|
+
nilike(key, val) {
|
|
252
|
+
const w = {};
|
|
253
|
+
w[key] = { nilike: val };
|
|
254
|
+
return this.add(w);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Add a `regexp` condition
|
|
258
|
+
* @param key - Property name
|
|
259
|
+
* @param val - Regexp condition
|
|
260
|
+
*/
|
|
261
|
+
regexp(key, val) {
|
|
262
|
+
const w = {};
|
|
263
|
+
w[key] = { regexp: val };
|
|
264
|
+
return this.add(w);
|
|
265
|
+
}
|
|
216
266
|
/**
|
|
217
267
|
* Get the where object
|
|
218
268
|
*/
|
package/dist/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,4DAA4B;AAG5B,uDAAuD;AAEvD,MAAM,cAAc,GAAG;IACrB,QAAQ;IACR,OAAO;IACP,OAAO;IACP,MAAM;IACN,QAAQ;IACR,SAAS;CACV,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,GAAG,cAAc,CAAC,CAAC;AAoNlD;;;GAGG;AACH,SAAgB,QAAQ,CACtB,SAAc;IAEd,IAAI,OAAO,SAAS,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAChD,KAAK,MAAM,GAAG,IAAI,SAAS,EAAE;QAC3B,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YAC/B,OAAO,KAAK,CAAC;SACd;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAVD,4BAUC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAa,YAAY;IAGvB,YAAY,CAAa;QACvB,IAAI,CAAC,KAAK,GAAG,CAAC,aAAD,CAAC,cAAD,CAAC,GAAI,EAAE,CAAC;IACvB,CAAC;IAEO,GAAG,CAAC,CAAY;QACtB,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YAC9B,IAAI,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE;gBACnB,wEAAwE;gBACxE,8BAA8B;gBAC9B,MAAM,KAAK,GAAG,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,EAAC,CAAC;gBACrC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;gBACnB,OAAO,IAAI,CAAC;aACb;SACF;QACD,wB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ACD,MAAM,KAAK,GAAG,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;YACxC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC/B,IAAI;YACF,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;SAC3B;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,MAAM,CAAC,CAAC;SAC5C;IACH,CAAC,CAAC;AACJ,CAAC;AAtBD,wCAsBC"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.belongsTo = void 0;
|
|
8
|
-
const
|
|
8
|
+
const core_1 = require("@loopback/core");
|
|
9
9
|
const model_decorator_1 = require("../../decorators/model.decorator");
|
|
10
10
|
const relation_decorator_1 = require("../relation.decorator");
|
|
11
11
|
const relation_types_1 = require("../relation.types");
|
|
@@ -24,7 +24,7 @@ function belongsTo(targetResolver, definition, propertyDefinition) {
|
|
|
24
24
|
propertyDefinition,
|
|
25
25
|
// properties enforced by the decorator
|
|
26
26
|
{
|
|
27
|
-
type:
|
|
27
|
+
type: core_1.MetadataInspector.getDesignTypeForProperty(decoratedTarget, decoratedKey),
|
|
28
28
|
});
|
|
29
29
|
model_decorator_1.property(propMeta)(decoratedTarget, decoratedKey);
|
|
30
30
|
// @belongsTo() is typically decorating the foreign key property,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"belongs-to.decorator.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to.decorator.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE
|
|
1
|
+
{"version":3,"file":"belongs-to.decorator.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to.decorator.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,yCAAiD;AACjD,sEAA0D;AAE1D,8DAA+C;AAC/C,sDAAoE;AAEpE;;;;;;;GAOG;AACH,SAAgB,SAAS,CACvB,cAAiC,EACjC,UAAyC,EACzC,kBAAgD;IAEhD,OAAO,UAAU,eAAuB,EAAE,YAAoB;QAC5D,MAAM,QAAQ,GAAuB,MAAM,CAAC,MAAM,CAChD,EAAE;QACF,oCAAoC;QACpC,kBAAkB;QAClB,uCAAuC;QACvC;YACE,IAAI,EAAE,wBAAiB,CAAC,wBAAwB,CAC9C,eAAe,EACf,YAAY,CACb;SAIF,CACF,CAAC;QACF,0BAAQ,CAAC,QAAQ,CAAC,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;QAElD,iEAAiE;QACjE,4EAA4E;QAC5E,MAAM,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAErD,MAAM,IAAI,GAAwB,MAAM,CAAC,MAAM;QAC7C,kDAAkD;QAClD;YACE,OAAO,EAAE,YAAY;YACrB,IAAI,EAAE,YAAY;SACnB;QACD,oCAAoC;QACpC,UAAU;QACV,uCAAuC;QACvC;YACE,IAAI,EAAE,6BAAY,CAAC,SAAS;YAC5B,WAAW,EAAE,KAAK;YAClB,MAAM,EAAE,eAAe,CAAC,WAAW;YACnC,MAAM,EAAE,cAAc;SACvB,CACF,CAAC;QACF,6BAAQ,CAAC,IAAI,CAAC,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;IAChD,CAAC,CAAC;AACJ,CAAC;AA7CD,8BA6CC"}
|
|
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.resolveBelongsToMetadata = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
10
11
|
const errors_1 = require("../../errors");
|
|
11
12
|
const type_resolver_1 = require("../../type-resolver");
|
|
12
13
|
const relation_types_1 = require("../relation.types");
|
|
@@ -27,10 +28,6 @@ function resolveBelongsToMetadata(relationMeta) {
|
|
|
27
28
|
const reason = 'target must be a type resolver';
|
|
28
29
|
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
29
30
|
}
|
|
30
|
-
if (!relationMeta.keyFrom) {
|
|
31
|
-
const reason = 'keyFrom is required';
|
|
32
|
-
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
33
|
-
}
|
|
34
31
|
const sourceModel = relationMeta.source;
|
|
35
32
|
if (!sourceModel || !sourceModel.modelName) {
|
|
36
33
|
const reason = 'source model must be defined';
|
|
@@ -39,18 +36,28 @@ function resolveBelongsToMetadata(relationMeta) {
|
|
|
39
36
|
const targetModel = relationMeta.target();
|
|
40
37
|
const targetName = targetModel.modelName;
|
|
41
38
|
debug('Resolved model %s from given metadata: %o', targetName, targetModel);
|
|
39
|
+
let keyFrom;
|
|
40
|
+
if (relationMeta.keyFrom &&
|
|
41
|
+
relationMeta.source.definition.properties[relationMeta.keyFrom]) {
|
|
42
|
+
keyFrom = relationMeta.keyFrom;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
keyFrom = lodash_1.camelCase(targetName + '_id');
|
|
46
|
+
}
|
|
42
47
|
const targetProperties = targetModel.definition.properties;
|
|
43
48
|
debug('relation metadata from %o: %o', targetName, targetProperties);
|
|
44
49
|
if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
|
|
45
50
|
// The explicit cast is needed because of a limitation of type inference
|
|
46
|
-
return relationMeta
|
|
51
|
+
return Object.assign(relationMeta, {
|
|
52
|
+
keyFrom,
|
|
53
|
+
});
|
|
47
54
|
}
|
|
48
55
|
const targetPrimaryKey = targetModel.definition.idProperties()[0];
|
|
49
56
|
if (!targetPrimaryKey) {
|
|
50
57
|
const reason = `${targetName} does not have any primary key (id property)`;
|
|
51
58
|
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
52
59
|
}
|
|
53
|
-
return Object.assign(relationMeta, { keyTo: targetPrimaryKey });
|
|
60
|
+
return Object.assign(relationMeta, { keyFrom, keyTo: targetPrimaryKey });
|
|
54
61
|
}
|
|
55
62
|
exports.resolveBelongsToMetadata = resolveBelongsToMetadata;
|
|
56
63
|
//# sourceMappingURL=belongs-to.helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"belongs-to.helpers.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAoE;AAEpE,MAAM,KAAK,GAAG,eAAY,CAAC,wCAAwC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"belongs-to.helpers.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,mCAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAoE;AAEpE,MAAM,KAAK,GAAG,eAAY,CAAC,wCAAwC,CAAC,CAAC;AAWrE;;;;;;GAMG;AACH,SAAgB,wBAAwB,CAAC,YAAiC;IACxE,IAAK,YAAY,CAAC,IAAqB,KAAK,6BAAY,CAAC,SAAS,EAAE;QAClE,MAAM,MAAM,GAAG,iCAAiC,CAAC;QACjD,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,IAAI,C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,MAAM,GAAG,GAAG,UAAU,8CAA8C,CAAC;QAC3E,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,EAAC,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAC,CAAC,CAAC;AACzE,CAAC;AAhDD,4DAgDC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Entity, EntityCrudRepository, Getter, HasManyDefinition } from '../..';
|
|
2
|
+
import { HasManyThroughRepository } from './has-many-through.repository';
|
|
3
|
+
/**
|
|
4
|
+
* a factory to generate hasManyThrough repository class.
|
|
5
|
+
*
|
|
6
|
+
* Warning: The hasManyThrough interface is experimental and is subject to change.
|
|
7
|
+
* If backwards-incompatible changes are made, a new major version may not be
|
|
8
|
+
* released.
|
|
9
|
+
*/
|
|
10
|
+
export declare type HasManyThroughRepositoryFactory<TargetEntity extends Entity, TargetID, ThroughEntity extends Entity, SourceID> = (fkValue: SourceID) => HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity>;
|
|
11
|
+
export declare function createHasManyThroughRepositoryFactory<Target extends Entity, TargetID, Through extends Entity, ThroughID, SourceID>(relationMetadata: HasManyDefinition, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>, throughRepositoryGetter: Getter<EntityCrudRepository<Through, ThroughID>>): HasManyThroughRepositoryFactory<Target, TargetID, Through, SourceID>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHasManyThroughRepositoryFactory = void 0;
|
|
4
|
+
const has_many_through_helpers_1 = require("./has-many-through.helpers");
|
|
5
|
+
const has_many_through_repository_1 = require("./has-many-through.repository");
|
|
6
|
+
function createHasManyThroughRepositoryFactory(relationMetadata, targetRepositoryGetter, throughRepositoryGetter) {
|
|
7
|
+
const meta = has_many_through_helpers_1.resolveHasManyThroughMetadata(relationMetadata);
|
|
8
|
+
const result = function (fkValue) {
|
|
9
|
+
function getTargetConstraintFromThroughModels(throughInstances) {
|
|
10
|
+
return has_many_through_helpers_1.createTargetConstraintFromThrough(meta, throughInstances);
|
|
11
|
+
}
|
|
12
|
+
function getTargetKeys(throughInstances) {
|
|
13
|
+
return has_many_through_helpers_1.getTargetKeysFromThroughModels(meta, throughInstances);
|
|
14
|
+
}
|
|
15
|
+
function getThroughConstraintFromSource() {
|
|
16
|
+
const constraint = has_many_through_helpers_1.createThroughConstraintFromSource(meta, fkValue);
|
|
17
|
+
return constraint;
|
|
18
|
+
}
|
|
19
|
+
function getTargetIds(targetInstances) {
|
|
20
|
+
return has_many_through_helpers_1.getTargetIdsFromTargetModels(meta, targetInstances);
|
|
21
|
+
}
|
|
22
|
+
function getThroughConstraintFromTarget(fkValues) {
|
|
23
|
+
const constraint = has_many_through_helpers_1.createThroughConstraintFromTarget(meta, fkValues);
|
|
24
|
+
return constraint;
|
|
25
|
+
}
|
|
26
|
+
return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getTargetIds, getThroughConstraintFromTarget);
|
|
27
|
+
};
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
exports.createHasManyThroughRepositoryFactory = createHasManyThroughRepositoryFactory;
|
|
31
|
+
//# sourceMappingURL=has-many-through-repository.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"has-many-through-repository.factory.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through-repository.factory.ts"],"names":[],"mappings":";;;AAWA,yEAOoC;AACpC,+EAGuC;AAmBvC,SAAgB,qCAAqC,CAOnD,gBAAmC,EACnC,sBAAsE,EACtE,uBAAyE;IAEzE,MAAM,IAAI,GAAG,wDAA6B,CAAC,gBAAgB,CAAC,CAAC;IAC7D,MAAM,MAAM,GAAG,UAAU,OAAiB;QACxC,SAAS,oCAAoC,CAC3C,gBAA2B;YAE3B,OAAO,4DAAiC,CACtC,IAAI,EACJ,gBAAgB,CACjB,CAAC;QACJ,CAAC;QACD,SAAS,aAAa,CAAC,gBAA2B;YAChD,OAAO,yDAA8B,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;QAChE,CAAC;QACD,SAAS,8BAA8B;YACrC,MAAM,UAAU,GAAwB,4DAAiC,CAGvE,IAAI,EAAE,OAAO,CAAC,CAAC;YACjB,OAAO,UAAU,CAAC;QACpB,CAAC;QACD,SAAS,YAAY,CAAC,eAAyB;YAC7C,OAAO,uDAA4B,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;QAC7D,CAAC;QACD,SAAS,8BAA8B,CACrC,QAAoB;YAEpB,MAAM,UAAU,GAAwB,4DAAiC,CAGvE,IAAI,EAAE,QAAQ,CAAC,CAAC;YAClB,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,OAAO,IAAI,6DAA+B,CAQxC,sBAAsB,EACtB,uBAAuB,EACvB,oCAAoC,EACpC,aAAa,EACb,8BAA8B,EAC9B,YAAY,EACZ,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AA9DD,sFA8DC"}
|
|
@@ -8,10 +8,9 @@ export declare type HasManyThroughResolvedDefinition = HasManyDefinition & {
|
|
|
8
8
|
};
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
|
-
* Creates constraint
|
|
12
|
-
* @param relationMeta - hasManyThrough metadata
|
|
13
|
-
* @param throughInstances -
|
|
14
|
-
* @internal
|
|
11
|
+
* Creates target constraint based on through models
|
|
12
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
13
|
+
* @param throughInstances - an array of through instances
|
|
15
14
|
*
|
|
16
15
|
* @example
|
|
17
16
|
* ```ts
|
|
@@ -25,27 +24,71 @@ export declare type HasManyThroughResolvedDefinition = HasManyDefinition & {
|
|
|
25
24
|
* keyTo: 'productId',
|
|
26
25
|
* },
|
|
27
26
|
* };
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
* createTargetConstraintFromThrough(resolvedMetadata,[{
|
|
28
|
+
id: 2,
|
|
29
|
+
categoryId: 2,
|
|
30
|
+
productId: 8,
|
|
31
|
+
}]);
|
|
32
|
+
* >>> {id: 8}
|
|
33
|
+
* createTargetConstraintFromThrough(resolvedMetadata, [
|
|
30
34
|
{
|
|
31
35
|
id: 2,
|
|
32
36
|
categoryId: 2,
|
|
33
37
|
productId: 8,
|
|
34
38
|
}, {
|
|
35
|
-
id:
|
|
39
|
+
id: 1,
|
|
36
40
|
categoryId: 2,
|
|
37
41
|
productId: 9,
|
|
38
42
|
}
|
|
39
43
|
]);
|
|
44
|
+
|
|
45
|
+
>>> {id: {inq: [9, 8]}}
|
|
40
46
|
* ```
|
|
41
47
|
*/
|
|
42
|
-
export declare function
|
|
48
|
+
export declare function createTargetConstraintFromThrough<Target extends Entity, Through extends Entity>(relationMeta: HasManyThroughResolvedDefinition, throughInstances: Through[]): DataObject<Target>;
|
|
43
49
|
/**
|
|
44
|
-
*
|
|
50
|
+
* Returns an array of target fks of the given throughInstances.
|
|
45
51
|
*
|
|
46
|
-
* @param relationMeta - hasManyThrough metadata
|
|
47
|
-
* @param
|
|
48
|
-
*
|
|
52
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
53
|
+
* @param throughInstances - an array of through instances
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const resolvedMetadata = {
|
|
58
|
+
* // .. other props
|
|
59
|
+
* keyFrom: 'id',
|
|
60
|
+
* keyTo: 'id',
|
|
61
|
+
* through: {
|
|
62
|
+
* model: () => CategoryProductLink,
|
|
63
|
+
* keyFrom: 'categoryId',
|
|
64
|
+
* keyTo: 'productId',
|
|
65
|
+
* },
|
|
66
|
+
* };
|
|
67
|
+
* getTargetKeysFromThroughModels(resolvedMetadata,[{
|
|
68
|
+
id: 2,
|
|
69
|
+
categoryId: 2,
|
|
70
|
+
productId: 8,
|
|
71
|
+
}]);
|
|
72
|
+
* >>> [8]
|
|
73
|
+
* getTargetKeysFromThroughModels(resolvedMetadata, [
|
|
74
|
+
{
|
|
75
|
+
id: 2,
|
|
76
|
+
categoryId: 2,
|
|
77
|
+
productId: 8,
|
|
78
|
+
}, {
|
|
79
|
+
id: 1,
|
|
80
|
+
categoryId: 2,
|
|
81
|
+
productId: 9,
|
|
82
|
+
}
|
|
83
|
+
]);
|
|
84
|
+
>>> [8, 9]
|
|
85
|
+
*/
|
|
86
|
+
export declare function getTargetKeysFromThroughModels<Through extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, throughInstances: Through[]): TargetID[];
|
|
87
|
+
/**
|
|
88
|
+
* Creates through constraint based on the source key
|
|
89
|
+
*
|
|
90
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
91
|
+
* @param fkValue - foreign key of the source instance
|
|
49
92
|
* @internal
|
|
50
93
|
*
|
|
51
94
|
* @example
|
|
@@ -60,10 +103,75 @@ export declare function createTargetConstraint<Target extends Entity, Through ex
|
|
|
60
103
|
* keyTo: 'productId',
|
|
61
104
|
* },
|
|
62
105
|
* };
|
|
63
|
-
*
|
|
106
|
+
* createThroughConstraintFromSource(resolvedMetadata, 1);
|
|
107
|
+
*
|
|
108
|
+
* >>> {categoryId: 1}
|
|
64
109
|
* ```
|
|
65
110
|
*/
|
|
66
|
-
export declare function
|
|
111
|
+
export declare function createThroughConstraintFromSource<Through extends Entity, SourceID>(relationMeta: HasManyThroughResolvedDefinition, fkValue: SourceID): DataObject<Through>;
|
|
112
|
+
/**
|
|
113
|
+
* Returns an array of target ids of the given target instances.
|
|
114
|
+
*
|
|
115
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
116
|
+
* @param targetInstances - an array of target instances
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const resolvedMetadata = {
|
|
121
|
+
* // .. other props
|
|
122
|
+
* keyFrom: 'id',
|
|
123
|
+
* keyTo: 'id',
|
|
124
|
+
* through: {
|
|
125
|
+
* model: () => CategoryProductLink,
|
|
126
|
+
* keyFrom: 'categoryId',
|
|
127
|
+
* keyTo: 'productId',
|
|
128
|
+
* },
|
|
129
|
+
* };
|
|
130
|
+
* getTargetKeysFromTargetModels(resolvedMetadata,[{
|
|
131
|
+
id: 2,
|
|
132
|
+
des: 'a target',
|
|
133
|
+
}]);
|
|
134
|
+
* >>> [2]
|
|
135
|
+
* getTargetKeysFromTargetModels(resolvedMetadata, [
|
|
136
|
+
{
|
|
137
|
+
id: 2,
|
|
138
|
+
des: 'a target',
|
|
139
|
+
}, {
|
|
140
|
+
id: 1,
|
|
141
|
+
des: 'a target',
|
|
142
|
+
}
|
|
143
|
+
]);
|
|
144
|
+
>>> [2, 1]
|
|
145
|
+
*/
|
|
146
|
+
export declare function getTargetIdsFromTargetModels<Target extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, targetInstances: Target[]): TargetID[];
|
|
147
|
+
/**
|
|
148
|
+
* Creates through constraint based on the target foreign key
|
|
149
|
+
*
|
|
150
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
151
|
+
* @param fkValue an array of the target instance foreign keys
|
|
152
|
+
* @internal
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const resolvedMetadata = {
|
|
157
|
+
* // .. other props
|
|
158
|
+
* keyFrom: 'id',
|
|
159
|
+
* keyTo: 'id',
|
|
160
|
+
* through: {
|
|
161
|
+
* model: () => CategoryProductLink,
|
|
162
|
+
* keyFrom: 'categoryId',
|
|
163
|
+
* keyTo: 'productId',
|
|
164
|
+
* },
|
|
165
|
+
* };
|
|
166
|
+
* createThroughConstraintFromTarget(resolvedMetadata, [3]);
|
|
167
|
+
*
|
|
168
|
+
* >>> {productId: 3}
|
|
169
|
+
*
|
|
170
|
+
* createThroughConstraintFromTarget(resolvedMetadata, [3,4]);
|
|
171
|
+
*
|
|
172
|
+
* >>> {productId: {inq:[3,4]}}
|
|
173
|
+
*/
|
|
174
|
+
export declare function createThroughConstraintFromTarget<Through extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, fkValues: TargetID[]): DataObject<Through>;
|
|
67
175
|
/**
|
|
68
176
|
* Resolves given hasMany metadata if target is specified to be a resolver.
|
|
69
177
|
* Mainly used to infer what the `keyTo` property should be from the target's
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// This file is licensed under the MIT License.
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.resolveHasManyThroughMetadata = exports.
|
|
7
|
+
exports.resolveHasManyThroughMetadata = exports.createThroughConstraintFromTarget = exports.getTargetIdsFromTargetModels = exports.createThroughConstraintFromSource = exports.getTargetKeysFromThroughModels = exports.createTargetConstraintFromThrough = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
10
10
|
const lodash_1 = require("lodash");
|
|
@@ -12,10 +12,9 @@ const __1 = require("../..");
|
|
|
12
12
|
const has_many_helpers_1 = require("./has-many.helpers");
|
|
13
13
|
const debug = debug_1.default('loopback:repository:has-many-through-helpers');
|
|
14
14
|
/**
|
|
15
|
-
* Creates constraint
|
|
16
|
-
* @param relationMeta - hasManyThrough metadata
|
|
17
|
-
* @param throughInstances -
|
|
18
|
-
* @internal
|
|
15
|
+
* Creates target constraint based on through models
|
|
16
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
17
|
+
* @param throughInstances - an array of through instances
|
|
19
18
|
*
|
|
20
19
|
* @example
|
|
21
20
|
* ```ts
|
|
@@ -29,37 +28,87 @@ const debug = debug_1.default('loopback:repository:has-many-through-helpers');
|
|
|
29
28
|
* keyTo: 'productId',
|
|
30
29
|
* },
|
|
31
30
|
* };
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
* createTargetConstraintFromThrough(resolvedMetadata,[{
|
|
32
|
+
id: 2,
|
|
33
|
+
categoryId: 2,
|
|
34
|
+
productId: 8,
|
|
35
|
+
}]);
|
|
36
|
+
* >>> {id: 8}
|
|
37
|
+
* createTargetConstraintFromThrough(resolvedMetadata, [
|
|
34
38
|
{
|
|
35
39
|
id: 2,
|
|
36
40
|
categoryId: 2,
|
|
37
41
|
productId: 8,
|
|
38
42
|
}, {
|
|
39
|
-
id:
|
|
43
|
+
id: 1,
|
|
40
44
|
categoryId: 2,
|
|
41
45
|
productId: 9,
|
|
42
46
|
}
|
|
43
47
|
]);
|
|
48
|
+
|
|
49
|
+
>>> {id: {inq: [9, 8]}}
|
|
44
50
|
* ```
|
|
45
51
|
*/
|
|
46
|
-
function
|
|
52
|
+
function createTargetConstraintFromThrough(relationMeta, throughInstances) {
|
|
53
|
+
const fkValues = getTargetKeysFromThroughModels(relationMeta, throughInstances);
|
|
47
54
|
const targetPrimaryKey = relationMeta.keyTo;
|
|
48
|
-
const targetFkName = relationMeta.through.keyTo;
|
|
49
|
-
const fkValues = throughInstances.map((throughInstance) => throughInstance[targetFkName]);
|
|
50
55
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
56
|
const constraint = {
|
|
52
57
|
[targetPrimaryKey]: fkValues.length === 1 ? fkValues[0] : { inq: fkValues },
|
|
53
58
|
};
|
|
54
59
|
return constraint;
|
|
55
60
|
}
|
|
56
|
-
exports.
|
|
61
|
+
exports.createTargetConstraintFromThrough = createTargetConstraintFromThrough;
|
|
57
62
|
/**
|
|
58
|
-
*
|
|
63
|
+
* Returns an array of target fks of the given throughInstances.
|
|
59
64
|
*
|
|
60
|
-
* @param relationMeta - hasManyThrough metadata
|
|
61
|
-
* @param
|
|
62
|
-
*
|
|
65
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
66
|
+
* @param throughInstances - an array of through instances
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const resolvedMetadata = {
|
|
71
|
+
* // .. other props
|
|
72
|
+
* keyFrom: 'id',
|
|
73
|
+
* keyTo: 'id',
|
|
74
|
+
* through: {
|
|
75
|
+
* model: () => CategoryProductLink,
|
|
76
|
+
* keyFrom: 'categoryId',
|
|
77
|
+
* keyTo: 'productId',
|
|
78
|
+
* },
|
|
79
|
+
* };
|
|
80
|
+
* getTargetKeysFromThroughModels(resolvedMetadata,[{
|
|
81
|
+
id: 2,
|
|
82
|
+
categoryId: 2,
|
|
83
|
+
productId: 8,
|
|
84
|
+
}]);
|
|
85
|
+
* >>> [8]
|
|
86
|
+
* getTargetKeysFromThroughModels(resolvedMetadata, [
|
|
87
|
+
{
|
|
88
|
+
id: 2,
|
|
89
|
+
categoryId: 2,
|
|
90
|
+
productId: 8,
|
|
91
|
+
}, {
|
|
92
|
+
id: 1,
|
|
93
|
+
categoryId: 2,
|
|
94
|
+
productId: 9,
|
|
95
|
+
}
|
|
96
|
+
]);
|
|
97
|
+
>>> [8, 9]
|
|
98
|
+
*/
|
|
99
|
+
function getTargetKeysFromThroughModels(relationMeta, throughInstances) {
|
|
100
|
+
const targetFkName = relationMeta.through.keyTo;
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
|
+
let fkValues = throughInstances.map((throughInstance) => throughInstance[targetFkName]);
|
|
103
|
+
fkValues = __1.deduplicate(fkValues);
|
|
104
|
+
return fkValues;
|
|
105
|
+
}
|
|
106
|
+
exports.getTargetKeysFromThroughModels = getTargetKeysFromThroughModels;
|
|
107
|
+
/**
|
|
108
|
+
* Creates through constraint based on the source key
|
|
109
|
+
*
|
|
110
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
111
|
+
* @param fkValue - foreign key of the source instance
|
|
63
112
|
* @internal
|
|
64
113
|
*
|
|
65
114
|
* @example
|
|
@@ -74,16 +123,100 @@ exports.createTargetConstraint = createTargetConstraint;
|
|
|
74
123
|
* keyTo: 'productId',
|
|
75
124
|
* },
|
|
76
125
|
* };
|
|
77
|
-
*
|
|
126
|
+
* createThroughConstraintFromSource(resolvedMetadata, 1);
|
|
127
|
+
*
|
|
128
|
+
* >>> {categoryId: 1}
|
|
78
129
|
* ```
|
|
79
130
|
*/
|
|
80
|
-
function
|
|
131
|
+
function createThroughConstraintFromSource(relationMeta, fkValue) {
|
|
81
132
|
const sourceFkName = relationMeta.through.keyFrom;
|
|
82
133
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
134
|
const constraint = { [sourceFkName]: fkValue };
|
|
84
135
|
return constraint;
|
|
85
136
|
}
|
|
86
|
-
exports.
|
|
137
|
+
exports.createThroughConstraintFromSource = createThroughConstraintFromSource;
|
|
138
|
+
/**
|
|
139
|
+
* Returns an array of target ids of the given target instances.
|
|
140
|
+
*
|
|
141
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
142
|
+
* @param targetInstances - an array of target instances
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const resolvedMetadata = {
|
|
147
|
+
* // .. other props
|
|
148
|
+
* keyFrom: 'id',
|
|
149
|
+
* keyTo: 'id',
|
|
150
|
+
* through: {
|
|
151
|
+
* model: () => CategoryProductLink,
|
|
152
|
+
* keyFrom: 'categoryId',
|
|
153
|
+
* keyTo: 'productId',
|
|
154
|
+
* },
|
|
155
|
+
* };
|
|
156
|
+
* getTargetKeysFromTargetModels(resolvedMetadata,[{
|
|
157
|
+
id: 2,
|
|
158
|
+
des: 'a target',
|
|
159
|
+
}]);
|
|
160
|
+
* >>> [2]
|
|
161
|
+
* getTargetKeysFromTargetModels(resolvedMetadata, [
|
|
162
|
+
{
|
|
163
|
+
id: 2,
|
|
164
|
+
des: 'a target',
|
|
165
|
+
}, {
|
|
166
|
+
id: 1,
|
|
167
|
+
des: 'a target',
|
|
168
|
+
}
|
|
169
|
+
]);
|
|
170
|
+
>>> [2, 1]
|
|
171
|
+
*/
|
|
172
|
+
function getTargetIdsFromTargetModels(relationMeta, targetInstances) {
|
|
173
|
+
const targetId = relationMeta.keyTo;
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
175
|
+
let ids = [];
|
|
176
|
+
ids = targetInstances.map((targetInstance) => targetInstance[targetId]);
|
|
177
|
+
ids = __1.deduplicate(ids);
|
|
178
|
+
return ids;
|
|
179
|
+
}
|
|
180
|
+
exports.getTargetIdsFromTargetModels = getTargetIdsFromTargetModels;
|
|
181
|
+
/**
|
|
182
|
+
* Creates through constraint based on the target foreign key
|
|
183
|
+
*
|
|
184
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
185
|
+
* @param fkValue an array of the target instance foreign keys
|
|
186
|
+
* @internal
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* const resolvedMetadata = {
|
|
191
|
+
* // .. other props
|
|
192
|
+
* keyFrom: 'id',
|
|
193
|
+
* keyTo: 'id',
|
|
194
|
+
* through: {
|
|
195
|
+
* model: () => CategoryProductLink,
|
|
196
|
+
* keyFrom: 'categoryId',
|
|
197
|
+
* keyTo: 'productId',
|
|
198
|
+
* },
|
|
199
|
+
* };
|
|
200
|
+
* createThroughConstraintFromTarget(resolvedMetadata, [3]);
|
|
201
|
+
*
|
|
202
|
+
* >>> {productId: 3}
|
|
203
|
+
*
|
|
204
|
+
* createThroughConstraintFromTarget(resolvedMetadata, [3,4]);
|
|
205
|
+
*
|
|
206
|
+
* >>> {productId: {inq:[3,4]}}
|
|
207
|
+
*/
|
|
208
|
+
function createThroughConstraintFromTarget(relationMeta, fkValues) {
|
|
209
|
+
if (fkValues === undefined || fkValues.length === 0) {
|
|
210
|
+
throw new Error('"fkValue" must be provided');
|
|
211
|
+
}
|
|
212
|
+
const targetFkName = relationMeta.through.keyTo;
|
|
213
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
214
|
+
const constraint = fkValues.length === 1
|
|
215
|
+
? { [targetFkName]: fkValues[0] }
|
|
216
|
+
: { [targetFkName]: { inq: fkValues } };
|
|
217
|
+
return constraint;
|
|
218
|
+
}
|
|
219
|
+
exports.createThroughConstraintFromTarget = createThroughConstraintFromTarget;
|
|
87
220
|
/**
|
|
88
221
|
* Resolves given hasMany metadata if target is specified to be a resolver.
|
|
89
222
|
* Mainly used to infer what the `keyTo` property should be from the target's
|
|
@@ -114,7 +247,7 @@ function resolveHasManyThroughMetadata(relationMeta) {
|
|
|
114
247
|
throughModelProperties[relationMeta.through.keyFrom] &&
|
|
115
248
|
relationMeta.keyTo &&
|
|
116
249
|
targetModelProperties[relationMeta.keyTo]) {
|
|
117
|
-
// The
|
|
250
|
+
// The explicit cast is needed because of a limitation of type inference
|
|
118
251
|
return relationMeta;
|
|
119
252
|
}
|
|
120
253
|
const sourceModel = relationMeta.source;
|