@loopback/repository 2.9.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 +17 -0
- package/README.md +1 -1
- package/dist/model.d.ts +19 -2
- package/dist/model.js +43 -0
- package/dist/model.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/has-many/has-many-through-repository.factory.js +4 -1
- package/dist/relations/has-many/has-many-through-repository.factory.js.map +1 -1
- package/dist/relations/has-many/has-many-through.helpers.d.ts +35 -0
- package/dist/relations/has-many/has-many-through.helpers.js +44 -1
- package/dist/relations/has-many/has-many-through.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository.d.ts +2 -1
- package/dist/relations/has-many/has-many-through.repository.js +21 -5
- package/dist/relations/has-many/has-many-through.repository.js.map +1 -1
- package/dist/relations/relation.types.d.ts +1 -1
- package/package.json +8 -8
- package/src/model.ts +62 -2
- package/src/relations/belongs-to/belongs-to.helpers.ts +19 -8
- package/src/relations/has-many/has-many-through-repository.factory.ts +5 -1
- package/src/relations/has-many/has-many-through.helpers.ts +49 -0
- package/src/relations/has-many/has-many-through.repository.ts +26 -9
- package/src/relations/relation.types.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,23 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.10.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.9.0...@loopback/repository@2.10.0) (2020-07-20)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **repository:** hasManyThrough can delete correct target n through based on filter ([c1ba91f](https://github.com/strongloop/loopback-next/commit/c1ba91f2dcbc33dd1cee905d25f9719a71bc7919))
|
|
12
|
+
* ensure delete only applies to optional properties ([89cd43f](https://github.com/strongloop/loopback-next/commit/89cd43f1a455983f120d9bb9c869eac36adc7ad7))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* **repository:** add helpers to create relations ([42d17aa](https://github.com/strongloop/loopback-next/commit/42d17aa37fa88bb4c4152b26450ddb8b72da1f49))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
6
23
|
# [2.9.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.8.0...@loopback/repository@2.9.0) (2020-06-30)
|
|
7
24
|
|
|
8
25
|
|
package/README.md
CHANGED
|
@@ -97,7 +97,7 @@ or properties.
|
|
|
97
97
|
import {repository} from '@loopback/repository';
|
|
98
98
|
import {NoteRepository} from '../repositories';
|
|
99
99
|
import {Note} from '../models';
|
|
100
|
-
import {post, requestBody, get, param} from '@loopback/
|
|
100
|
+
import {post, requestBody, get, param} from '@loopback/rest';
|
|
101
101
|
|
|
102
102
|
export class NoteController {
|
|
103
103
|
constructor(
|
package/dist/model.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AnyObject, DataObject, Options, PrototypeOf } from './common-types';
|
|
2
|
-
import { JsonSchema } from './index';
|
|
3
|
-
import { RelationMetadata } from './relations';
|
|
2
|
+
import { BelongsToDefinition, HasManyDefinition, HasOneDefinition, JsonSchema, RelationMetadata } from './index';
|
|
4
3
|
import { TypeResolver } from './type-resolver';
|
|
5
4
|
import { Type } from './types';
|
|
6
5
|
/**
|
|
@@ -85,6 +84,24 @@ export declare class ModelDefinition {
|
|
|
85
84
|
* @param definition - The definition of the new relation.
|
|
86
85
|
*/
|
|
87
86
|
addRelation(definition: RelationMetadata): this;
|
|
87
|
+
/**
|
|
88
|
+
* Define a new belongsTo relation.
|
|
89
|
+
* @param name - The name of the belongsTo relation.
|
|
90
|
+
* @param definition - The definition of the belongsTo relation.
|
|
91
|
+
*/
|
|
92
|
+
belongsTo(name: string, definition: Omit<BelongsToDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
|
93
|
+
/**
|
|
94
|
+
* Define a new hasOne relation.
|
|
95
|
+
* @param name - The name of the hasOne relation.
|
|
96
|
+
* @param definition - The definition of the hasOne relation.
|
|
97
|
+
*/
|
|
98
|
+
hasOne(name: string, definition: Omit<HasOneDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
|
99
|
+
/**
|
|
100
|
+
* Define a new hasMany relation.
|
|
101
|
+
* @param name - The name of the hasMany relation.
|
|
102
|
+
* @param definition - The definition of the hasMany relation.
|
|
103
|
+
*/
|
|
104
|
+
hasMany(name: string, definition: Omit<HasManyDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
|
88
105
|
/**
|
|
89
106
|
* Get an array of names of ID properties, which are specified in
|
|
90
107
|
* the model settings or properties with `id` attribute.
|
package/dist/model.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.rejectNavigationalPropertiesInData = exports.Event = exports.Entity = exports.ValueObject = exports.Model = exports.ModelDefinition = void 0;
|
|
8
|
+
const index_1 = require("./index");
|
|
8
9
|
/**
|
|
9
10
|
* Definition for a model
|
|
10
11
|
*/
|
|
@@ -59,6 +60,48 @@ class ModelDefinition {
|
|
|
59
60
|
this.relations[definition.name] = definition;
|
|
60
61
|
return this;
|
|
61
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Define a new belongsTo relation.
|
|
65
|
+
* @param name - The name of the belongsTo relation.
|
|
66
|
+
* @param definition - The definition of the belongsTo relation.
|
|
67
|
+
*/
|
|
68
|
+
belongsTo(name, definition) {
|
|
69
|
+
const meta = {
|
|
70
|
+
...definition,
|
|
71
|
+
name,
|
|
72
|
+
type: index_1.RelationType.belongsTo,
|
|
73
|
+
targetsMany: false,
|
|
74
|
+
};
|
|
75
|
+
return this.addRelation(meta);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Define a new hasOne relation.
|
|
79
|
+
* @param name - The name of the hasOne relation.
|
|
80
|
+
* @param definition - The definition of the hasOne relation.
|
|
81
|
+
*/
|
|
82
|
+
hasOne(name, definition) {
|
|
83
|
+
const meta = {
|
|
84
|
+
...definition,
|
|
85
|
+
name,
|
|
86
|
+
type: index_1.RelationType.hasOne,
|
|
87
|
+
targetsMany: false,
|
|
88
|
+
};
|
|
89
|
+
return this.addRelation(meta);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Define a new hasMany relation.
|
|
93
|
+
* @param name - The name of the hasMany relation.
|
|
94
|
+
* @param definition - The definition of the hasMany relation.
|
|
95
|
+
*/
|
|
96
|
+
hasMany(name, definition) {
|
|
97
|
+
const meta = {
|
|
98
|
+
...definition,
|
|
99
|
+
name,
|
|
100
|
+
type: index_1.RelationType.hasMany,
|
|
101
|
+
targetsMany: true,
|
|
102
|
+
};
|
|
103
|
+
return this.addRelation(meta);
|
|
104
|
+
}
|
|
62
105
|
/**
|
|
63
106
|
* Get an array of names of ID properties, which are specified in
|
|
64
107
|
* the model settings or properties with `id` attribute.
|
package/dist/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAGhE,mCAOiB;AAiEjB;;GAEG;AACH,MAAa,eAAe;IAQ1B,YAAY,SAAyC;QACnD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE;YACjC,SAAS,GAAG,EA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,IAAI,OAAO,EAAE;gBAC5B,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC;aAC5B;SACF;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAxED,wBAwEC;AAED;;GAEG;AACH,MAAa,KAAK;CAGjB;AAHD,sBAGC;AAMD;;;;;;;GAOG;AACH,SAAgB,kCAAkC,CAChD,UAAa,EACb,IAAgC;IAEhC,MAAM,GAAG,GAAG,UAAU,CAAC,UAAU,CAAC;IAClC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC;IAE7B,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,SAAS,EAAE;QAC7B,MAAM,OAAO,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACtC,IAAI,CAAC,CAAC,OAAO,IAAI,IAAI,CAAC;YAAE,SAAS;QAEjC,IAAI,GAAG,GACL,wDAAwD;YACxD,WAAW,UAAU,CAAC,SAAS,eAAe,OAAO,MAAM;YAC3D,mBAAmB,CAAC;QAEtB,IAAI,OAAO,IAAI,KAAK,EAAE;YACpB,GAAG;gBACD,iEAAiE;oBACjE,+DAA+D,CAAC;SACnE;QAED,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;KACtB;AACH,CAAC;AAxBD,gFAwBC"}
|
|
@@ -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,CAAC,8BAAc,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE;QACxC,MAAM,MAAM,GAAG,gCAAgC,CAAC;QAChD,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,WAAW,GAAG,YAAY,CAAC,MAAM,CAAC;IACxC,IAAI,CAAC,WAAW,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE;QAC1C,MAAM,MAAM,GAAG,8BAA8B,CAAC;QAC9C,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,WAAW,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC;IAC1C,MAAM,UAAU,GAAG,WAAW,CAAC,SAAS,CAAC;IACzC,KAAK,CAAC,2CAA2C,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC;IAE5E,IAAI,OAAO,CAAC;IACZ,IACE,YAAY,CAAC,OAAO;QACpB,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,EAC/D;QACA,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC;KAChC;SAAM;QACL,OAAO,GAAG,kBAAS,CAAC,UAAU,GAAG,KAAK,CAAC,CAAC;KACzC;IAED,MAAM,gBAAgB,GAAG,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC;IAC3D,KAAK,CAAC,+BAA+B,EAAE,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAErE,IAAI,YAAY,CAAC,KAAK,IAAI,gBAAgB,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAC9D,wEAAwE;QACxE,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;YACjC,OAAO;SACR,CAAgC,CAAC;KACnC;IAED,MAAM,gBAAgB,GAAG,WAAW,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IAClE,IAAI,CAAC,gBAAgB,EAAE;QACrB,MAAM,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"}
|
|
@@ -16,11 +16,14 @@ function createHasManyThroughRepositoryFactory(relationMetadata, targetRepositor
|
|
|
16
16
|
const constraint = has_many_through_helpers_1.createThroughConstraintFromSource(meta, fkValue);
|
|
17
17
|
return constraint;
|
|
18
18
|
}
|
|
19
|
+
function getTargetIds(targetInstances) {
|
|
20
|
+
return has_many_through_helpers_1.getTargetIdsFromTargetModels(meta, targetInstances);
|
|
21
|
+
}
|
|
19
22
|
function getThroughConstraintFromTarget(fkValues) {
|
|
20
23
|
const constraint = has_many_through_helpers_1.createThroughConstraintFromTarget(meta, fkValues);
|
|
21
24
|
return constraint;
|
|
22
25
|
}
|
|
23
|
-
return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getThroughConstraintFromTarget);
|
|
26
|
+
return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getTargetIds, getThroughConstraintFromTarget);
|
|
24
27
|
};
|
|
25
28
|
return result;
|
|
26
29
|
}
|
|
@@ -1 +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,
|
|
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"}
|
|
@@ -109,6 +109,41 @@ export declare function getTargetKeysFromThroughModels<Through extends Entity, T
|
|
|
109
109
|
* ```
|
|
110
110
|
*/
|
|
111
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[];
|
|
112
147
|
/**
|
|
113
148
|
* Creates through constraint based on the target foreign key
|
|
114
149
|
*
|
|
@@ -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.createThroughConstraintFromTarget = exports.createThroughConstraintFromSource = exports.getTargetKeysFromThroughModels = exports.createTargetConstraintFromThrough = void 0;
|
|
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");
|
|
@@ -135,6 +135,49 @@ function createThroughConstraintFromSource(relationMeta, fkValue) {
|
|
|
135
135
|
return constraint;
|
|
136
136
|
}
|
|
137
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;
|
|
138
181
|
/**
|
|
139
182
|
* Creates through constraint based on the target foreign key
|
|
140
183
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-many-through.helpers.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CA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,CAAC,YAAY,CAAC,EAAE,OAAO,EAAC,CAAC;IAClD,OAAO,UAAU,CAAC;AACpB,CAAC;AAXD,8EAWC;
|
|
1
|
+
{"version":3,"file":"has-many-through.helpers.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,mCAAiC;AACjC,6BAOe;AACf,yDAA4D;AAE5D,MAAM,KAAK,GAAG,eAAY,CAAC,8CAA8C,CAAC,CAAC;AAW3E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,SAAgB,iCAAiC,CAI/C,YAA8C,EAC9C,gBAA2B;IAE3B,MAAM,QAAQ,GAAG,8BAA8B,CAC7C,YAAY,EACZ,gBAAgB,CACjB,CAAC;IACF,MAAM,gBAAgB,GAAG,YAAY,CAAC,KAAK,CAAC;IAE5C,8DAA8D;IAC9D,MAAM,UAAU,GAAQ;QACtB,CAAC,gBAAgB,CAAC,EAAE,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAC,GAAG,EAAE,QAAQ,EAAC;KAC1E,CAAC;IACF,OAAO,UAAU,CAAC;AACpB,CAAC;AAlBD,8EAkBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,SAAgB,8BAA8B,CAI5C,YAA8C,EAC9C,gBAA2B;IAE3B,MAAM,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,CAAC;IAChD,8DAA8D;IAC9D,IAAI,QAAQ,GAAQ,gBAAgB,CAAC,GAAG,CACtC,CAAC,eAAwB,EAAE,EAAE,CAC3B,eAAe,CAAC,YAA6B,CAAC,CACjD,CAAC;IACF,QAAQ,GAAG,eAAW,CAAC,QAAQ,CAAC,CAAC;IACjC,OAAO,QA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AC;AA9ED,sEA8EC"}
|
|
@@ -77,8 +77,9 @@ export declare class DefaultHasManyThroughRepository<TargetEntity extends Entity
|
|
|
77
77
|
getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>;
|
|
78
78
|
getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[];
|
|
79
79
|
getThroughConstraintFromSource: () => DataObject<ThroughEntity>;
|
|
80
|
+
getTargetIds: (targetInstances: TargetEntity[]) => TargetID[];
|
|
80
81
|
getThroughConstraintFromTarget: (targetID: TargetID[]) => DataObject<ThroughEntity>;
|
|
81
|
-
constructor(getTargetRepository: Getter<TargetRepository>, getThroughRepository: Getter<ThroughRepository>, getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>, getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[], getThroughConstraintFromSource: () => DataObject<ThroughEntity>, getThroughConstraintFromTarget: (targetID: TargetID[]) => DataObject<ThroughEntity>);
|
|
82
|
+
constructor(getTargetRepository: Getter<TargetRepository>, getThroughRepository: Getter<ThroughRepository>, getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>, getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[], getThroughConstraintFromSource: () => DataObject<ThroughEntity>, getTargetIds: (targetInstances: TargetEntity[]) => TargetID[], getThroughConstraintFromTarget: (targetID: TargetID[]) => DataObject<ThroughEntity>);
|
|
82
83
|
create(targetModelData: DataObject<TargetEntity>, options?: Options & {
|
|
83
84
|
throughData?: DataObject<ThroughEntity>;
|
|
84
85
|
throughOptions?: Options;
|
|
@@ -14,12 +14,13 @@ const __1 = require("../..");
|
|
|
14
14
|
* released.
|
|
15
15
|
*/
|
|
16
16
|
class DefaultHasManyThroughRepository {
|
|
17
|
-
constructor(getTargetRepository, getThroughRepository, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getThroughConstraintFromTarget) {
|
|
17
|
+
constructor(getTargetRepository, getThroughRepository, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getTargetIds, getThroughConstraintFromTarget) {
|
|
18
18
|
this.getTargetRepository = getTargetRepository;
|
|
19
19
|
this.getThroughRepository = getThroughRepository;
|
|
20
20
|
this.getTargetConstraintFromThroughModels = getTargetConstraintFromThroughModels;
|
|
21
21
|
this.getTargetKeys = getTargetKeys;
|
|
22
22
|
this.getThroughConstraintFromSource = getThroughConstraintFromSource;
|
|
23
|
+
this.getTargetIds = getTargetIds;
|
|
23
24
|
this.getThroughConstraintFromTarget = getThroughConstraintFromTarget;
|
|
24
25
|
}
|
|
25
26
|
async create(targetModelData, options) {
|
|
@@ -41,10 +42,25 @@ class DefaultHasManyThroughRepository {
|
|
|
41
42
|
const throughRepository = await this.getThroughRepository();
|
|
42
43
|
const sourceConstraint = this.getThroughConstraintFromSource();
|
|
43
44
|
const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, sourceConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (where) {
|
|
46
|
+
// only delete related through models
|
|
47
|
+
// TODO(Agnes): this performance can be improved by only fetching related data
|
|
48
|
+
// TODO: add target ids to the `where` constraint
|
|
49
|
+
const targets = await targetRepository.find({ where });
|
|
50
|
+
const targetIds = this.getTargetIds(targets);
|
|
51
|
+
if (targetIds.length > 0) {
|
|
52
|
+
const targetConstraint = this.getThroughConstraintFromTarget(targetIds);
|
|
53
|
+
const constraints = { ...targetConstraint, ...sourceConstraint };
|
|
54
|
+
await throughRepository.deleteAll(__1.constrainDataObject({}, constraints), options === null || options === void 0 ? void 0 : options.throughOptions);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// otherwise, delete through models that relate to the sourceId
|
|
59
|
+
const targetFkValues = this.getTargetKeys(throughInstances);
|
|
60
|
+
// delete through instances that have the targets that are going to be deleted
|
|
61
|
+
const throughFkConstraint = this.getThroughConstraintFromTarget(targetFkValues);
|
|
62
|
+
await throughRepository.deleteAll(__1.constrainWhereOr({}, [sourceConstraint, throughFkConstraint]));
|
|
63
|
+
}
|
|
48
64
|
// delete target(s)
|
|
49
65
|
const targetConstraint = this.getTargetConstraintFromThroughModels(throughInstances);
|
|
50
66
|
return targetRepository.deleteAll(__1.constrainWhere(where, targetConstraint), options);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-many-through.repository.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through.repository.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,6BAae;AAgGf;;;;;;GAMG;AACH,MAAa,+BAA+B;IAQ1C,YACS,mBAA6C,EAC7C,oBAA+C,EAC/C,oCAEsB,EACtB,aAAgE,EAChE,8BAA+D,EAC/D,8BAEuB;
|
|
1
|
+
{"version":3,"file":"has-many-through.repository.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through.repository.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,6BAae;AAgGf;;;;;;GAMG;AACH,MAAa,+BAA+B;IAQ1C,YACS,mBAA6C,EAC7C,oBAA+C,EAC/C,oCAEsB,EACtB,aAAgE,EAChE,8BAA+D,EAC/D,YAA6D,EAC7D,8BAEuB;QAVvB,wBAAmB,GAAnB,mBAAmB,CAA0B;QAC7C,yBAAoB,GAApB,oBAAoB,CAA2B;QAC/C,yCAAoC,GAApC,oCAAoC,CAEd;QACtB,kBAAa,GAAb,aAAa,CAAmD;QAChE,mCAA8B,GAA9B,8BAA8B,CAAiC;QAC/D,iBAAY,GAAZ,YAAY,CAAiD;QAC7D,mCAA8B,GAA9B,8BAA8B,CAEP;IAC7B,CAAC;IAEJ,KAAK,CAAC,MAAM,CACV,eAAyC,EACzC,OAGC;QAED,MAAM,gBAAg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,gBAAgB,GAAG,IAAI,CAAC,8BAA8B,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;QACzE,MAAM,WAAW,GAAG,EAAC,GAAG,gBAAgB,EAAE,GAAG,gBAAgB,EAAC,CAAC;QAC/D,MAAM,iBAAiB,CAAC,SAAS,CAC/B,uBAAmB,CAAC,EAAE,EAAE,WAAwC,CAAC,EACjE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,cAAc,CACxB,CAAC;IACJ,CAAC;CACF;AAvKD,0EAuKC"}
|
|
@@ -96,7 +96,7 @@ export interface HasManyDefinition extends RelationDefinitionBase {
|
|
|
96
96
|
export interface BelongsToDefinition extends RelationDefinitionBase {
|
|
97
97
|
type: RelationType.belongsTo;
|
|
98
98
|
targetsMany: false;
|
|
99
|
-
keyFrom
|
|
99
|
+
keyFrom?: string;
|
|
100
100
|
keyTo?: string;
|
|
101
101
|
}
|
|
102
102
|
export interface HasOneDefinition extends RelationDefinitionBase {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loopback/repository",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"description": "Define and implement a common set of interfaces for interacting with databases",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,20 +22,20 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@loopback/build": "^6.1.
|
|
26
|
-
"@loopback/eslint-config": "^8.0.
|
|
27
|
-
"@loopback/testlab": "^3.2.
|
|
25
|
+
"@loopback/build": "^6.1.1",
|
|
26
|
+
"@loopback/eslint-config": "^8.0.4",
|
|
27
|
+
"@loopback/testlab": "^3.2.1",
|
|
28
28
|
"@types/bson": "^4.0.2",
|
|
29
29
|
"@types/json-schema": "^7.0.5",
|
|
30
30
|
"@types/lodash": "^4.14.157",
|
|
31
|
-
"@types/node": "^10.17.
|
|
31
|
+
"@types/node": "^10.17.27",
|
|
32
32
|
"bson": "4.0.4"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@loopback/core": "^2.9.
|
|
35
|
+
"@loopback/core": "^2.9.2",
|
|
36
36
|
"@types/debug": "^4.1.5",
|
|
37
37
|
"debug": "^4.1.1",
|
|
38
|
-
"lodash": "^4.17.
|
|
38
|
+
"lodash": "^4.17.19",
|
|
39
39
|
"loopback-datasource-juggler": "^4.21.2",
|
|
40
40
|
"tslib": "^2.0.0"
|
|
41
41
|
},
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"url": "https://github.com/strongloop/loopback-next.git",
|
|
51
51
|
"directory": "packages/repository"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "5538896411bb56467ae52670a29d1aec1690be74"
|
|
54
54
|
}
|
package/src/model.ts
CHANGED
|
@@ -4,8 +4,14 @@
|
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import {AnyObject, DataObject, Options, PrototypeOf} from './common-types';
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
BelongsToDefinition,
|
|
9
|
+
HasManyDefinition,
|
|
10
|
+
HasOneDefinition,
|
|
11
|
+
JsonSchema,
|
|
12
|
+
RelationMetadata,
|
|
13
|
+
RelationType,
|
|
14
|
+
} from './index';
|
|
9
15
|
import {TypeResolver} from './type-resolver';
|
|
10
16
|
import {Type} from './types';
|
|
11
17
|
|
|
@@ -145,6 +151,60 @@ export class ModelDefinition {
|
|
|
145
151
|
return this;
|
|
146
152
|
}
|
|
147
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Define a new belongsTo relation.
|
|
156
|
+
* @param name - The name of the belongsTo relation.
|
|
157
|
+
* @param definition - The definition of the belongsTo relation.
|
|
158
|
+
*/
|
|
159
|
+
belongsTo(
|
|
160
|
+
name: string,
|
|
161
|
+
definition: Omit<BelongsToDefinition, 'name' | 'type' | 'targetsMany'>,
|
|
162
|
+
): this {
|
|
163
|
+
const meta: BelongsToDefinition = {
|
|
164
|
+
...definition,
|
|
165
|
+
name,
|
|
166
|
+
type: RelationType.belongsTo,
|
|
167
|
+
targetsMany: false,
|
|
168
|
+
};
|
|
169
|
+
return this.addRelation(meta);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Define a new hasOne relation.
|
|
174
|
+
* @param name - The name of the hasOne relation.
|
|
175
|
+
* @param definition - The definition of the hasOne relation.
|
|
176
|
+
*/
|
|
177
|
+
hasOne(
|
|
178
|
+
name: string,
|
|
179
|
+
definition: Omit<HasOneDefinition, 'name' | 'type' | 'targetsMany'>,
|
|
180
|
+
): this {
|
|
181
|
+
const meta: HasOneDefinition = {
|
|
182
|
+
...definition,
|
|
183
|
+
name,
|
|
184
|
+
type: RelationType.hasOne,
|
|
185
|
+
targetsMany: false,
|
|
186
|
+
};
|
|
187
|
+
return this.addRelation(meta);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Define a new hasMany relation.
|
|
192
|
+
* @param name - The name of the hasMany relation.
|
|
193
|
+
* @param definition - The definition of the hasMany relation.
|
|
194
|
+
*/
|
|
195
|
+
hasMany(
|
|
196
|
+
name: string,
|
|
197
|
+
definition: Omit<HasManyDefinition, 'name' | 'type' | 'targetsMany'>,
|
|
198
|
+
): this {
|
|
199
|
+
const meta: HasManyDefinition = {
|
|
200
|
+
...definition,
|
|
201
|
+
name,
|
|
202
|
+
type: RelationType.hasMany,
|
|
203
|
+
targetsMany: true,
|
|
204
|
+
};
|
|
205
|
+
return this.addRelation(meta);
|
|
206
|
+
}
|
|
207
|
+
|
|
148
208
|
/**
|
|
149
209
|
* Get an array of names of ID properties, which are specified in
|
|
150
210
|
* the model settings or properties with `id` attribute.
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import debugFactory from 'debug';
|
|
7
|
+
import {camelCase} from 'lodash';
|
|
7
8
|
import {InvalidRelationError} from '../../errors';
|
|
8
9
|
import {isTypeResolver} from '../../type-resolver';
|
|
9
10
|
import {BelongsToDefinition, RelationType} from '../relation.types';
|
|
@@ -14,7 +15,10 @@ const debug = debugFactory('loopback:repository:belongs-to-helpers');
|
|
|
14
15
|
* Relation definition with optional metadata (e.g. `keyTo`) filled in.
|
|
15
16
|
* @internal
|
|
16
17
|
*/
|
|
17
|
-
export type BelongsToResolvedDefinition = BelongsToDefinition & {
|
|
18
|
+
export type BelongsToResolvedDefinition = BelongsToDefinition & {
|
|
19
|
+
keyFrom: string;
|
|
20
|
+
keyTo: string;
|
|
21
|
+
};
|
|
18
22
|
|
|
19
23
|
/**
|
|
20
24
|
* Resolves given belongsTo metadata if target is specified to be a resolver.
|
|
@@ -34,11 +38,6 @@ export function resolveBelongsToMetadata(relationMeta: BelongsToDefinition) {
|
|
|
34
38
|
throw new InvalidRelationError(reason, relationMeta);
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
if (!relationMeta.keyFrom) {
|
|
38
|
-
const reason = 'keyFrom is required';
|
|
39
|
-
throw new InvalidRelationError(reason, relationMeta);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
41
|
const sourceModel = relationMeta.source;
|
|
43
42
|
if (!sourceModel || !sourceModel.modelName) {
|
|
44
43
|
const reason = 'source model must be defined';
|
|
@@ -49,12 +48,24 @@ export function resolveBelongsToMetadata(relationMeta: BelongsToDefinition) {
|
|
|
49
48
|
const targetName = targetModel.modelName;
|
|
50
49
|
debug('Resolved model %s from given metadata: %o', targetName, targetModel);
|
|
51
50
|
|
|
51
|
+
let keyFrom;
|
|
52
|
+
if (
|
|
53
|
+
relationMeta.keyFrom &&
|
|
54
|
+
relationMeta.source.definition.properties[relationMeta.keyFrom]
|
|
55
|
+
) {
|
|
56
|
+
keyFrom = relationMeta.keyFrom;
|
|
57
|
+
} else {
|
|
58
|
+
keyFrom = camelCase(targetName + '_id');
|
|
59
|
+
}
|
|
60
|
+
|
|
52
61
|
const targetProperties = targetModel.definition.properties;
|
|
53
62
|
debug('relation metadata from %o: %o', targetName, targetProperties);
|
|
54
63
|
|
|
55
64
|
if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
|
|
56
65
|
// The explicit cast is needed because of a limitation of type inference
|
|
57
|
-
return relationMeta
|
|
66
|
+
return Object.assign(relationMeta, {
|
|
67
|
+
keyFrom,
|
|
68
|
+
}) as BelongsToResolvedDefinition;
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
const targetPrimaryKey = targetModel.definition.idProperties()[0];
|
|
@@ -63,5 +74,5 @@ export function resolveBelongsToMetadata(relationMeta: BelongsToDefinition) {
|
|
|
63
74
|
throw new InvalidRelationError(reason, relationMeta);
|
|
64
75
|
}
|
|
65
76
|
|
|
66
|
-
return Object.assign(relationMeta, {keyTo: targetPrimaryKey});
|
|
77
|
+
return Object.assign(relationMeta, {keyFrom, keyTo: targetPrimaryKey});
|
|
67
78
|
}
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
createTargetConstraintFromThrough,
|
|
14
14
|
createThroughConstraintFromSource,
|
|
15
15
|
createThroughConstraintFromTarget,
|
|
16
|
+
getTargetIdsFromTargetModels,
|
|
16
17
|
getTargetKeysFromThroughModels,
|
|
17
18
|
resolveHasManyThroughMetadata,
|
|
18
19
|
} from './has-many-through.helpers';
|
|
@@ -69,7 +70,9 @@ export function createHasManyThroughRepositoryFactory<
|
|
|
69
70
|
>(meta, fkValue);
|
|
70
71
|
return constraint;
|
|
71
72
|
}
|
|
72
|
-
|
|
73
|
+
function getTargetIds(targetInstances: Target[]): TargetID[] {
|
|
74
|
+
return getTargetIdsFromTargetModels(meta, targetInstances);
|
|
75
|
+
}
|
|
73
76
|
function getThroughConstraintFromTarget(
|
|
74
77
|
fkValues: TargetID[],
|
|
75
78
|
): DataObject<Through> {
|
|
@@ -93,6 +96,7 @@ export function createHasManyThroughRepositoryFactory<
|
|
|
93
96
|
getTargetConstraintFromThroughModels,
|
|
94
97
|
getTargetKeys,
|
|
95
98
|
getThroughConstraintFromSource,
|
|
99
|
+
getTargetIds,
|
|
96
100
|
getThroughConstraintFromTarget,
|
|
97
101
|
);
|
|
98
102
|
};
|
|
@@ -174,6 +174,55 @@ export function createThroughConstraintFromSource<
|
|
|
174
174
|
const constraint: any = {[sourceFkName]: fkValue};
|
|
175
175
|
return constraint;
|
|
176
176
|
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Returns an array of target ids of the given target instances.
|
|
180
|
+
*
|
|
181
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
182
|
+
* @param targetInstances - an array of target instances
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* const resolvedMetadata = {
|
|
187
|
+
* // .. other props
|
|
188
|
+
* keyFrom: 'id',
|
|
189
|
+
* keyTo: 'id',
|
|
190
|
+
* through: {
|
|
191
|
+
* model: () => CategoryProductLink,
|
|
192
|
+
* keyFrom: 'categoryId',
|
|
193
|
+
* keyTo: 'productId',
|
|
194
|
+
* },
|
|
195
|
+
* };
|
|
196
|
+
* getTargetKeysFromTargetModels(resolvedMetadata,[{
|
|
197
|
+
id: 2,
|
|
198
|
+
des: 'a target',
|
|
199
|
+
}]);
|
|
200
|
+
* >>> [2]
|
|
201
|
+
* getTargetKeysFromTargetModels(resolvedMetadata, [
|
|
202
|
+
{
|
|
203
|
+
id: 2,
|
|
204
|
+
des: 'a target',
|
|
205
|
+
}, {
|
|
206
|
+
id: 1,
|
|
207
|
+
des: 'a target',
|
|
208
|
+
}
|
|
209
|
+
]);
|
|
210
|
+
>>> [2, 1]
|
|
211
|
+
*/
|
|
212
|
+
export function getTargetIdsFromTargetModels<Target extends Entity, TargetID>(
|
|
213
|
+
relationMeta: HasManyThroughResolvedDefinition,
|
|
214
|
+
targetInstances: Target[],
|
|
215
|
+
): TargetID[] {
|
|
216
|
+
const targetId = relationMeta.keyTo;
|
|
217
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
218
|
+
let ids = [] as any;
|
|
219
|
+
ids = targetInstances.map(
|
|
220
|
+
(targetInstance: Target) => targetInstance[targetId as keyof Target],
|
|
221
|
+
);
|
|
222
|
+
ids = deduplicate(ids);
|
|
223
|
+
return ids as TargetID[];
|
|
224
|
+
}
|
|
225
|
+
|
|
177
226
|
/**
|
|
178
227
|
* Creates through constraint based on the target foreign key
|
|
179
228
|
*
|
|
@@ -135,6 +135,7 @@ export class DefaultHasManyThroughRepository<
|
|
|
135
135
|
) => DataObject<TargetEntity>,
|
|
136
136
|
public getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[],
|
|
137
137
|
public getThroughConstraintFromSource: () => DataObject<ThroughEntity>,
|
|
138
|
+
public getTargetIds: (targetInstances: TargetEntity[]) => TargetID[],
|
|
138
139
|
public getThroughConstraintFromTarget: (
|
|
139
140
|
targetID: TargetID[],
|
|
140
141
|
) => DataObject<ThroughEntity>,
|
|
@@ -191,15 +192,31 @@ export class DefaultHasManyThroughRepository<
|
|
|
191
192
|
constrainFilter(undefined, sourceConstraint),
|
|
192
193
|
options?.throughOptions,
|
|
193
194
|
);
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
195
|
+
if (where) {
|
|
196
|
+
// only delete related through models
|
|
197
|
+
// TODO(Agnes): this performance can be improved by only fetching related data
|
|
198
|
+
// TODO: add target ids to the `where` constraint
|
|
199
|
+
const targets = await targetRepository.find({where});
|
|
200
|
+
const targetIds = this.getTargetIds(targets);
|
|
201
|
+
if (targetIds.length > 0) {
|
|
202
|
+
const targetConstraint = this.getThroughConstraintFromTarget(targetIds);
|
|
203
|
+
const constraints = {...targetConstraint, ...sourceConstraint};
|
|
204
|
+
await throughRepository.deleteAll(
|
|
205
|
+
constrainDataObject({}, constraints as DataObject<ThroughEntity>),
|
|
206
|
+
options?.throughOptions,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
// otherwise, delete through models that relate to the sourceId
|
|
211
|
+
const targetFkValues = this.getTargetKeys(throughInstances);
|
|
212
|
+
// delete through instances that have the targets that are going to be deleted
|
|
213
|
+
const throughFkConstraint = this.getThroughConstraintFromTarget(
|
|
214
|
+
targetFkValues,
|
|
215
|
+
);
|
|
216
|
+
await throughRepository.deleteAll(
|
|
217
|
+
constrainWhereOr({}, [sourceConstraint, throughFkConstraint]),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
203
220
|
// delete target(s)
|
|
204
221
|
const targetConstraint = this.getTargetConstraintFromThroughModels(
|
|
205
222
|
throughInstances,
|
|
@@ -117,7 +117,7 @@ export interface BelongsToDefinition extends RelationDefinitionBase {
|
|
|
117
117
|
/*
|
|
118
118
|
* The foreign key in the source model, e.g. Order#customerId.
|
|
119
119
|
*/
|
|
120
|
-
keyFrom
|
|
120
|
+
keyFrom?: string;
|
|
121
121
|
|
|
122
122
|
/*
|
|
123
123
|
* The primary key of the target model, e.g Customer#id.
|