@loopback/repository 2.8.0 → 2.9.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 CHANGED
@@ -3,6 +3,18 @@
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.9.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.8.0...@loopback/repository@2.9.0) (2020-06-30)
7
+
8
+
9
+ ### Features
10
+
11
+ * add HasManyThroughFactory to Juggler bridge ([c2d4352](https://github.com/strongloop/loopback-next/commit/c2d43524be4fd1345970b29e722e57890f2ea766))
12
+ * **repository:** add link and unlink methods ([31dc4e9](https://github.com/strongloop/loopback-next/commit/31dc4e989ae981732fa43f7f48dfb8f19d5ac796))
13
+
14
+
15
+
16
+
17
+
6
18
  # [2.8.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.7.0...@loopback/repository@2.8.0) (2020-06-23)
7
19
 
8
20
 
@@ -7,5 +7,5 @@ import { HasManyThroughRepository } from './has-many-through.repository';
7
7
  * If backwards-incompatible changes are made, a new major version may not be
8
8
  * released.
9
9
  */
10
- export declare type HasManyThroughRepositoryFactory<TargetEntity extends Entity, TargetID, ThroughEntity extends Entity, ForeignKeyType> = (fkValue: ForeignKeyType) => HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity>;
11
- export declare function createHasManyThroughRepositoryFactory<Target extends Entity, TargetID, Through extends Entity, ThroughID, ForeignKeyType>(relationMetadata: HasManyDefinition, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>, throughRepositoryGetter: Getter<EntityCrudRepository<Through, ThroughID>>): HasManyThroughRepositoryFactory<Target, TargetID, Through, ForeignKeyType>;
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>;
@@ -1,8 +1,4 @@
1
1
  "use strict";
2
- // Copyright IBM Corp. 2020. All Rights Reserved.
3
- // Node module: @loopback/repository
4
- // This file is licensed under the MIT License.
5
- // License text available at https://opensource.org/licenses/MIT
6
2
  Object.defineProperty(exports, "__esModule", { value: true });
7
3
  exports.createHasManyThroughRepositoryFactory = void 0;
8
4
  const has_many_through_helpers_1 = require("./has-many-through.helpers");
@@ -10,18 +6,21 @@ const has_many_through_repository_1 = require("./has-many-through.repository");
10
6
  function createHasManyThroughRepositoryFactory(relationMetadata, targetRepositoryGetter, throughRepositoryGetter) {
11
7
  const meta = has_many_through_helpers_1.resolveHasManyThroughMetadata(relationMetadata);
12
8
  const result = function (fkValue) {
13
- function getTargetContraint(throughInstances) {
14
- return has_many_through_helpers_1.createTargetConstraint(meta, throughInstances);
9
+ function getTargetConstraintFromThroughModels(throughInstances) {
10
+ return has_many_through_helpers_1.createTargetConstraintFromThrough(meta, throughInstances);
15
11
  }
16
- function getThroughConstraint() {
17
- const constriant = has_many_through_helpers_1.createThroughConstraint(meta, fkValue);
18
- return constriant;
12
+ function getTargetKeys(throughInstances) {
13
+ return has_many_through_helpers_1.getTargetKeysFromThroughModels(meta, throughInstances);
19
14
  }
20
- function getThroughFkConstraint(targetInstance) {
21
- const constriant = has_many_through_helpers_1.createThroughFkConstraint(meta, targetInstance);
22
- return constriant;
15
+ function getThroughConstraintFromSource() {
16
+ const constraint = has_many_through_helpers_1.createThroughConstraintFromSource(meta, fkValue);
17
+ return constraint;
23
18
  }
24
- return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetContraint, getThroughConstraint, getThroughFkConstraint);
19
+ function getThroughConstraintFromTarget(fkValues) {
20
+ const constraint = has_many_through_helpers_1.createThroughConstraintFromTarget(meta, fkValues);
21
+ return constraint;
22
+ }
23
+ return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getThroughConstraintFromTarget);
25
24
  };
26
25
  return result;
27
26
  }
@@ -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":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAShE,yEAKoC;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,OAAuB;QAC9C,SAAS,kBAAkB,CACzB,gBAAqC;YAErC,OAAO,iDAAsB,CAAkB,IAAI,EAAE,gBAAgB,CAAC,CAAC;QACzE,CAAC;QACD,SAAS,oBAAoB;YAC3B,MAAM,UAAU,GAAwB,kDAAuB,CAG7D,IAAI,EAAE,OAAO,CAAC,CAAC;YACjB,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,SAAS,sBAAsB,CAC7B,cAAsB;YAEtB,MAAM,UAAU,GAAwB,oDAAyB,CAG/D,IAAI,EAAE,cAAc,CAAC,CAAC;YACxB,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,OAAO,IAAI,6DAA+B,CAQxC,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,sBAAsB,CACvB,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AApDD,sFAoDC"}
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,yEAMoC;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;QAED,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,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AA3DD,sFA2DC"}
@@ -8,10 +8,9 @@ export declare type HasManyThroughResolvedDefinition = HasManyDefinition & {
8
8
  };
9
9
  };
10
10
  /**
11
- * Creates constraint used to query target
11
+ * Creates target constraint based on through models
12
12
  * @param relationMeta - resolved hasManyThrough metadata
13
- * @param throughInstances - Instances of through entities used to constrain the target
14
- * @internal
13
+ * @param throughInstances - an array of through instances
15
14
  *
16
15
  * @example
17
16
  * ```ts
@@ -25,8 +24,13 @@ export declare type HasManyThroughResolvedDefinition = HasManyDefinition & {
25
24
  * keyTo: 'productId',
26
25
  * },
27
26
  * };
28
-
29
- * createTargetConstraint(resolvedMetadata, [
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,
@@ -41,13 +45,50 @@ export declare type HasManyThroughResolvedDefinition = HasManyDefinition & {
41
45
  >>> {id: {inq: [9, 8]}}
42
46
  * ```
43
47
  */
44
- export declare function createTargetConstraint<Target extends Entity, Through extends Entity>(relationMeta: HasManyThroughResolvedDefinition, throughInstances: Through | Through[]): DataObject<Target>;
48
+ export declare function createTargetConstraintFromThrough<Target extends Entity, Through extends Entity>(relationMeta: HasManyThroughResolvedDefinition, throughInstances: Through[]): DataObject<Target>;
49
+ /**
50
+ * Returns an array of target fks of the given throughInstances.
51
+ *
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[];
45
87
  /**
46
- * Creates constraint used to query through model
88
+ * Creates through constraint based on the source key
47
89
  *
48
90
  * @param relationMeta - resolved hasManyThrough metadata
49
- * @param fkValue - Value of the foreign key of the source model used to constrain through
50
- * @param targetInstance - Instance of target entity used to constrain through
91
+ * @param fkValue - foreign key of the source instance
51
92
  * @internal
52
93
  *
53
94
  * @example
@@ -62,17 +103,17 @@ export declare function createTargetConstraint<Target extends Entity, Through ex
62
103
  * keyTo: 'productId',
63
104
  * },
64
105
  * };
65
- * createThroughConstraint(resolvedMetadata, 1);
106
+ * createThroughConstraintFromSource(resolvedMetadata, 1);
66
107
  *
67
108
  * >>> {categoryId: 1}
68
109
  * ```
69
110
  */
70
- export declare function createThroughConstraint<Through extends Entity, ForeignKeyType>(relationMeta: HasManyThroughResolvedDefinition, fkValue: ForeignKeyType): DataObject<Through>;
111
+ export declare function createThroughConstraintFromSource<Through extends Entity, SourceID>(relationMeta: HasManyThroughResolvedDefinition, fkValue: SourceID): DataObject<Through>;
71
112
  /**
72
- * Creates constraint used to create the through model
113
+ * Creates through constraint based on the target foreign key
73
114
  *
74
115
  * @param relationMeta - resolved hasManyThrough metadata
75
- * @param targetInstance instance of target entity used to constrain through
116
+ * @param fkValue an array of the target instance foreign keys
76
117
  * @internal
77
118
  *
78
119
  * @example
@@ -87,15 +128,15 @@ export declare function createThroughConstraint<Through extends Entity, ForeignK
87
128
  * keyTo: 'productId',
88
129
  * },
89
130
  * };
90
- * createThroughConstraint(resolvedMetadata, {id: 3, name: 'a product'});
131
+ * createThroughConstraintFromTarget(resolvedMetadata, [3]);
91
132
  *
92
- * >>> {productId: 1}
133
+ * >>> {productId: 3}
93
134
  *
94
- * createThroughConstraint(resolvedMetadata, {id: {inq:[3,4]}});
135
+ * createThroughConstraintFromTarget(resolvedMetadata, [3,4]);
95
136
  *
96
137
  * >>> {productId: {inq:[3,4]}}
97
138
  */
98
- export declare function createThroughFkConstraint<Target, Through extends Entity>(relationMeta: HasManyThroughResolvedDefinition, targetInstance: Target): DataObject<Through>;
139
+ export declare function createThroughConstraintFromTarget<Through extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, fkValues: TargetID[]): DataObject<Through>;
99
140
  /**
100
141
  * Resolves given hasMany metadata if target is specified to be a resolver.
101
142
  * 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.createThroughFkConstraint = exports.createThroughConstraint = exports.createTargetConstraint = void 0;
7
+ exports.resolveHasManyThroughMetadata = exports.createThroughConstraintFromTarget = 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 used to query target
15
+ * Creates target constraint based on through models
16
16
  * @param relationMeta - resolved hasManyThrough metadata
17
- * @param throughInstances - Instances of through entities used to constrain the target
18
- * @internal
17
+ * @param throughInstances - an array of through instances
19
18
  *
20
19
  * @example
21
20
  * ```ts
@@ -29,8 +28,13 @@ const debug = debug_1.default('loopback:repository:has-many-through-helpers');
29
28
  * keyTo: 'productId',
30
29
  * },
31
30
  * };
32
-
33
- * createTargetConstraint(resolvedMetadata, [
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,
@@ -45,27 +49,66 @@ const debug = debug_1.default('loopback:repository:has-many-through-helpers');
45
49
  >>> {id: {inq: [9, 8]}}
46
50
  * ```
47
51
  */
48
- function createTargetConstraint(relationMeta, throughInstances) {
52
+ function createTargetConstraintFromThrough(relationMeta, throughInstances) {
53
+ const fkValues = getTargetKeysFromThroughModels(relationMeta, throughInstances);
49
54
  const targetPrimaryKey = relationMeta.keyTo;
50
- const targetFkName = relationMeta.through.keyTo;
51
- if (!Array.isArray(throughInstances)) {
52
- throughInstances = [throughInstances];
53
- }
54
- let fkValues = throughInstances.map((throughInstance) => throughInstance[targetFkName]);
55
- fkValues = __1.deduplicate(fkValues);
56
55
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
56
  const constraint = {
58
57
  [targetPrimaryKey]: fkValues.length === 1 ? fkValues[0] : { inq: fkValues },
59
58
  };
60
59
  return constraint;
61
60
  }
62
- exports.createTargetConstraint = createTargetConstraint;
61
+ exports.createTargetConstraintFromThrough = createTargetConstraintFromThrough;
63
62
  /**
64
- * Creates constraint used to query through model
63
+ * Returns an array of target fks of the given throughInstances.
65
64
  *
66
65
  * @param relationMeta - resolved hasManyThrough metadata
67
- * @param fkValue - Value of the foreign key of the source model used to constrain through
68
- * @param targetInstance - Instance of target entity used to constrain through
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
69
112
  * @internal
70
113
  *
71
114
  * @example
@@ -80,23 +123,23 @@ exports.createTargetConstraint = createTargetConstraint;
80
123
  * keyTo: 'productId',
81
124
  * },
82
125
  * };
83
- * createThroughConstraint(resolvedMetadata, 1);
126
+ * createThroughConstraintFromSource(resolvedMetadata, 1);
84
127
  *
85
128
  * >>> {categoryId: 1}
86
129
  * ```
87
130
  */
88
- function createThroughConstraint(relationMeta, fkValue) {
131
+ function createThroughConstraintFromSource(relationMeta, fkValue) {
89
132
  const sourceFkName = relationMeta.through.keyFrom;
90
133
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
134
  const constraint = { [sourceFkName]: fkValue };
92
135
  return constraint;
93
136
  }
94
- exports.createThroughConstraint = createThroughConstraint;
137
+ exports.createThroughConstraintFromSource = createThroughConstraintFromSource;
95
138
  /**
96
- * Creates constraint used to create the through model
139
+ * Creates through constraint based on the target foreign key
97
140
  *
98
141
  * @param relationMeta - resolved hasManyThrough metadata
99
- * @param targetInstance instance of target entity used to constrain through
142
+ * @param fkValue an array of the target instance foreign keys
100
143
  * @internal
101
144
  *
102
145
  * @example
@@ -111,24 +154,26 @@ exports.createThroughConstraint = createThroughConstraint;
111
154
  * keyTo: 'productId',
112
155
  * },
113
156
  * };
114
- * createThroughConstraint(resolvedMetadata, {id: 3, name: 'a product'});
157
+ * createThroughConstraintFromTarget(resolvedMetadata, [3]);
115
158
  *
116
- * >>> {productId: 1}
159
+ * >>> {productId: 3}
117
160
  *
118
- * createThroughConstraint(resolvedMetadata, {id: {inq:[3,4]}});
161
+ * createThroughConstraintFromTarget(resolvedMetadata, [3,4]);
119
162
  *
120
163
  * >>> {productId: {inq:[3,4]}}
121
164
  */
122
- function createThroughFkConstraint(relationMeta, targetInstance) {
123
- const targetKey = relationMeta.keyTo;
165
+ function createThroughConstraintFromTarget(relationMeta, fkValues) {
166
+ if (fkValues === undefined || fkValues.length === 0) {
167
+ throw new Error('"fkValue" must be provided');
168
+ }
124
169
  const targetFkName = relationMeta.through.keyTo;
125
170
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
- const constraint = {
127
- [targetFkName]: targetInstance[targetKey],
128
- };
171
+ const constraint = fkValues.length === 1
172
+ ? { [targetFkName]: fkValues[0] }
173
+ : { [targetFkName]: { inq: fkValues } };
129
174
  return constraint;
130
175
  }
131
- exports.createThroughFkConstraint = createThroughFkConstraint;
176
+ exports.createThroughConstraintFromTarget = createThroughConstraintFromTarget;
132
177
  /**
133
178
  * Resolves given hasMany metadata if target is specified to be a resolver.
134
179
  * Mainly used to infer what the `keyTo` property should be from the target's
@@ -159,7 +204,7 @@ function resolveHasManyThroughMetadata(relationMeta) {
159
204
  throughModelProperties[relationMeta.through.keyFrom] &&
160
205
  relationMeta.keyTo &&
161
206
  targetModelProperties[relationMeta.keyTo]) {
162
- // The explict cast is needed because of a limitation of type inference
207
+ // The explicit cast is needed because of a limitation of type inference
163
208
  return relationMeta;
164
209
  }
165
210
  const sourceModel = relationMeta.source;
@@ -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,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,mCAAiC;AACjC,6BAQe;AACf,yDAA4D;AAE5D,MAAM,KAAK,GAAG,eAAY,CAAC,8CAA8C,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GAAG,YAAY,CAAC,OAAO;YACvB,KAAK,EAAE,YAAY;YACnB,OAAO,EAAE,YAAY;SACtB;KACF,CAAC,CAAC;AACL,CAAC;AA9ED,sEA8EC"}
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,CAA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YAAY,SAChB,YAAY,CAAC,OAAO,CAAC,KAAK,mCAAI,kBAAS,CAAC,WAAW,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;IACzE,IAAI,CAAC,sBAAsB,CAAC,YAAY,CAAC,EAAE;QACzC,MAAM,MAAM,GAAG,iBAAiB,YAAY,CAAC,IAAI,8CAA8C,CAAC;QAChG,MAAM,IAAI,wBAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,gBAAgB,SACpB,YAAY,CAAC,KAAK,mCAAI,WAAW,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IACjE,IAAI,CAAC,gBAAgB,IAAI,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,EAAE;QACjE,MAAM,MAAM,GAAG,gBAAgB,WAAW,CAAC,SAAS,8CAA8C,CAAC;QACnG,MAAM,IAAI,wBAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;QACjC,KAAK,EAAE,gBAAgB;QACvB,OAAO,EAAE,YAAY,CAAC,OAAQ;QAC9B,OAAO,EAAE;YACP,GAAG,YAAY,CAAC,OAAO;YACvB,KAAK,EAAE,YAAY;YACnB,OAAO,EAAE,YAAY;SACtB;KACF,CAAC,CAAC;AACL,CAAC;AA9ED,sEA8EC"}
@@ -53,7 +53,7 @@ export interface HasManyThroughRepository<Target extends Entity, TargetID, Throu
53
53
  link(targetModelId: TargetID, options?: Options & {
54
54
  throughData?: DataObject<Through>;
55
55
  throughOptions?: Options;
56
- }): Promise<Target>;
56
+ }): Promise<void>;
57
57
  /**
58
58
  * Removes an association to an existing target model instance
59
59
  * @param targetModelId - The target model to unlink
@@ -74,10 +74,11 @@ export interface HasManyThroughRepository<Target extends Entity, TargetID, Throu
74
74
  export declare class DefaultHasManyThroughRepository<TargetEntity extends Entity, TargetID, TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>, ThroughEntity extends Entity, ThroughID, ThroughRepository extends EntityCrudRepository<ThroughEntity, ThroughID>> implements HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity> {
75
75
  getTargetRepository: Getter<TargetRepository>;
76
76
  getThroughRepository: Getter<ThroughRepository>;
77
- getTargetConstraint: (throughInstances: ThroughEntity | ThroughEntity[]) => DataObject<TargetEntity>;
78
- getThroughConstraint: () => DataObject<ThroughEntity>;
79
- getThroughFkConstraint: (targetInstance: TargetEntity) => DataObject<ThroughEntity>;
80
- constructor(getTargetRepository: Getter<TargetRepository>, getThroughRepository: Getter<ThroughRepository>, getTargetConstraint: (throughInstances: ThroughEntity | ThroughEntity[]) => DataObject<TargetEntity>, getThroughConstraint: () => DataObject<ThroughEntity>, getThroughFkConstraint: (targetInstance: TargetEntity) => DataObject<ThroughEntity>);
77
+ getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>;
78
+ getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[];
79
+ getThroughConstraintFromSource: () => DataObject<ThroughEntity>;
80
+ 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>);
81
82
  create(targetModelData: DataObject<TargetEntity>, options?: Options & {
82
83
  throughData?: DataObject<ThroughEntity>;
83
84
  throughOptions?: Options;
@@ -91,11 +92,11 @@ export declare class DefaultHasManyThroughRepository<TargetEntity extends Entity
91
92
  patch(dataObject: DataObject<TargetEntity>, where?: Where<TargetEntity>, options?: Options & {
92
93
  throughOptions?: Options;
93
94
  }): Promise<Count>;
94
- link(targetModelId: TargetID, options?: Options & {
95
+ link(targetId: TargetID, options?: Options & {
95
96
  throughData?: DataObject<ThroughEntity>;
96
97
  throughOptions?: Options;
97
- }): Promise<TargetEntity>;
98
- unlink(targetModelId: TargetID, options?: Options & {
98
+ }): Promise<void>;
99
+ unlink(targetId: TargetID, options?: Options & {
99
100
  throughOptions?: Options;
100
101
  }): Promise<void>;
101
102
  }
@@ -14,58 +14,64 @@ const __1 = require("../..");
14
14
  * released.
15
15
  */
16
16
  class DefaultHasManyThroughRepository {
17
- constructor(getTargetRepository, getThroughRepository, getTargetConstraint, getThroughConstraint, getThroughFkConstraint) {
17
+ constructor(getTargetRepository, getThroughRepository, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getThroughConstraintFromTarget) {
18
18
  this.getTargetRepository = getTargetRepository;
19
19
  this.getThroughRepository = getThroughRepository;
20
- this.getTargetConstraint = getTargetConstraint;
21
- this.getThroughConstraint = getThroughConstraint;
22
- this.getThroughFkConstraint = getThroughFkConstraint;
20
+ this.getTargetConstraintFromThroughModels = getTargetConstraintFromThroughModels;
21
+ this.getTargetKeys = getTargetKeys;
22
+ this.getThroughConstraintFromSource = getThroughConstraintFromSource;
23
+ this.getThroughConstraintFromTarget = getThroughConstraintFromTarget;
23
24
  }
24
25
  async create(targetModelData, options) {
25
- var _a;
26
26
  const targetRepository = await this.getTargetRepository();
27
- const throughRepository = await this.getThroughRepository();
28
27
  const targetInstance = await targetRepository.create(targetModelData, options);
29
- const targetConstraint = this.getThroughFkConstraint(targetInstance);
30
- const throughConstraint = this.getThroughConstraint();
31
- const constraints = { ...targetConstraint, ...throughConstraint };
32
- await throughRepository.create(__1.constrainDataObject((_a = options === null || options === void 0 ? void 0 : options.throughData) !== null && _a !== void 0 ? _a : {}, constraints), options === null || options === void 0 ? void 0 : options.throughOptions);
28
+ await this.link(targetInstance.getId(), options);
33
29
  return targetInstance;
34
30
  }
35
31
  async find(filter, options) {
36
32
  const targetRepository = await this.getTargetRepository();
37
33
  const throughRepository = await this.getThroughRepository();
38
- const throughConstraint = this.getThroughConstraint();
39
- const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, throughConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
40
- const targetConstraint = this.getTargetConstraint(throughInstances);
34
+ const sourceConstraint = this.getThroughConstraintFromSource();
35
+ const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, sourceConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
36
+ const targetConstraint = this.getTargetConstraintFromThroughModels(throughInstances);
41
37
  return targetRepository.find(__1.constrainFilter(filter, targetConstraint), options);
42
38
  }
43
39
  async delete(where, options) {
44
40
  const targetRepository = await this.getTargetRepository();
45
41
  const throughRepository = await this.getThroughRepository();
46
- const throughConstraint = this.getThroughConstraint();
47
- const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, throughConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
48
- const targetConstraint = this.getTargetConstraint(throughInstances);
49
- // delete throughs that have the targets that are going to be deleted
50
- const throughFkConstraint = this.getThroughFkConstraint(targetConstraint);
51
- await throughRepository.deleteAll(__1.constrainWhereOr({}, [throughConstraint, throughFkConstraint]));
42
+ const sourceConstraint = this.getThroughConstraintFromSource();
43
+ const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, sourceConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
44
+ const targetFkValues = this.getTargetKeys(throughInstances);
45
+ // delete through instances that have the targets that are going to be deleted
46
+ const throughFkConstraint = this.getThroughConstraintFromTarget(targetFkValues);
47
+ await throughRepository.deleteAll(__1.constrainWhereOr({}, [sourceConstraint, throughFkConstraint]));
52
48
  // delete target(s)
49
+ const targetConstraint = this.getTargetConstraintFromThroughModels(throughInstances);
53
50
  return targetRepository.deleteAll(__1.constrainWhere(where, targetConstraint), options);
54
51
  }
55
52
  // only allows patch target instances for now
56
53
  async patch(dataObject, where, options) {
57
54
  const targetRepository = await this.getTargetRepository();
58
55
  const throughRepository = await this.getThroughRepository();
59
- const throughConstraint = this.getThroughConstraint();
60
- const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, throughConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
61
- const targetConstraint = this.getTargetConstraint(throughInstances);
56
+ const sourceConstraint = this.getThroughConstraintFromSource();
57
+ const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, sourceConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
58
+ const targetConstraint = this.getTargetConstraintFromThroughModels(throughInstances);
62
59
  return targetRepository.updateAll(__1.constrainDataObject(dataObject, targetConstraint), __1.constrainWhere(where, targetConstraint), options);
63
60
  }
64
- async link(targetModelId, options) {
65
- throw new Error('Method not implemented.');
61
+ async link(targetId, options) {
62
+ var _a;
63
+ const throughRepository = await this.getThroughRepository();
64
+ const sourceConstraint = this.getThroughConstraintFromSource();
65
+ const targetConstraint = this.getThroughConstraintFromTarget([targetId]);
66
+ const constraints = { ...targetConstraint, ...sourceConstraint };
67
+ await throughRepository.create(__1.constrainDataObject((_a = options === null || options === void 0 ? void 0 : options.throughData) !== null && _a !== void 0 ? _a : {}, constraints), options === null || options === void 0 ? void 0 : options.throughOptions);
66
68
  }
67
- async unlink(targetModelId, options) {
68
- throw new Error('Method not implemented.');
69
+ async unlink(targetId, options) {
70
+ const throughRepository = await this.getThroughRepository();
71
+ const sourceConstraint = this.getThroughConstraintFromSource();
72
+ const targetConstraint = this.getThroughConstraintFromTarget([targetId]);
73
+ const constraints = { ...targetConstraint, ...sourceConstraint };
74
+ await throughRepository.deleteAll(__1.constrainDataObject({}, constraints), options === null || options === void 0 ? void 0 : options.throughOptions);
69
75
  }
70
76
  }
71
77
  exports.DefaultHasManyThroughRepository = DefaultHasManyThroughRepository;
@@ -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;;;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,EAC3B,OAEC;QAED,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAC1D,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC5D,MAAM,iBAAiB,GAAG,IAAI,CAAC,oBAAoB,EAAE,CAAC;QACtD,MAAM,gBAAgB,GAAG,MAAM,iBAAiB,CAAC,IAAI,CACnD,mBAAe,CAAC,SAAS,EAAE,iBAAiB,CAAC,EAC7C,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,cAAc,CACxB,CAAC;QACF,MAAM,gBAAgB,GAAG,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CAAC;QACpE,OAAO,gBAAgB,CAAC,SAAS,CAC/B,uBAAmB,CAAC,UAAU,EAAE,gBAAgB,CAAC,EACjD,kBAAc,CAAC,KAAK,EAAE,gBAAuC,CAAC,EAC9D,OAAO,CACR,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,IAAI,CACR,aAAuB,EACvB,OAGC;QAED,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,MAAM,CACV,aAAuB,EACvB,OAEC;QAED,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;CACF;AAxID,0EAwIC"}
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;QATvB,wBAAmB,GAAnB,mBAAmB,CAA0B;QAC7C,yBAAoB,GAApB,oBAAoB,CAA2B;QAC/C,yCAAoC,GA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, DataObject, NamedParameters, Options, PositionalParameters } from '../common-types';
4
4
  import { Entity, Model } from '../model';
5
5
  import { Filter, FilterExcludingWhere, Inclusion, Where } from '../query';
6
- import { BelongsToAccessor, HasManyRepositoryFactory, HasOneRepositoryFactory, InclusionResolver } from '../relations';
6
+ import { BelongsToAccessor, HasManyRepositoryFactory, HasManyThroughRepositoryFactory, HasOneRepositoryFactory, InclusionResolver } from '../relations';
7
7
  import { IsolationLevel, Transaction } from '../transaction';
8
8
  import { EntityCrudRepository, TransactionalEntityRepository } from './repository';
9
9
  export declare namespace juggler {
@@ -96,6 +96,37 @@ export declare class DefaultCrudRepository<T extends Entity, ID, Relations exten
96
96
  * @param targetRepo - Target repository instance
97
97
  */
98
98
  protected createHasManyRepositoryFactoryFor<Target extends Entity, TargetID, ForeignKeyType>(relationName: string, targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>): HasManyRepositoryFactory<Target, ForeignKeyType>;
99
+ /**
100
+ * Function to create a constrained hasManyThrough relation repository factory
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * class CustomerRepository extends DefaultCrudRepository<
105
+ * Customer,
106
+ * typeof Customer.prototype.id,
107
+ * CustomerRelations
108
+ * > {
109
+ * public readonly cartItems: HasManyRepositoryFactory<CartItem, typeof Customer.prototype.id>;
110
+ *
111
+ * constructor(
112
+ * protected db: juggler.DataSource,
113
+ * cartItemRepository: EntityCrudRepository<CartItem, typeof, CartItem.prototype.id>,
114
+ * throughRepository: EntityCrudRepository<Through, typeof Through.prototype.id>,
115
+ * ) {
116
+ * super(Customer, db);
117
+ * this.cartItems = this.createHasManyThroughRepositoryFactoryFor(
118
+ * 'cartItems',
119
+ * cartItemRepository,
120
+ * );
121
+ * }
122
+ * }
123
+ * ```
124
+ *
125
+ * @param relationName - Name of the relation defined on the source model
126
+ * @param targetRepo - Target repository instance
127
+ * @param throughRepo - Through repository instance
128
+ */
129
+ protected createHasManyThroughRepositoryFactoryFor<Target extends Entity, TargetID, Through extends Entity, ThroughID, ForeignKeyType>(relationName: string, targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>, throughRepoGetter: Getter<EntityCrudRepository<Through, ThroughID>>): HasManyThroughRepositoryFactory<Target, TargetID, Through, ForeignKeyType>;
99
130
  /**
100
131
  * @deprecated
101
132
  * Function to create a belongs to accessor