@loopback/repository 1.16.0 → 1.17.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,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
+ # [1.17.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@1.16.0...@loopback/repository@1.17.0) (2020-01-07)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **repository:** belongsto accessor should return undefined if foreign key is not included ([cbdba15](https://github.com/strongloop/loopback-next/commit/cbdba1554fe103109a21e20c48cd3a0edcf8fffb))
12
+
13
+
14
+ ### Features
15
+
16
+ * **repository:** add interface for hasManyThrough ([ced2643](https://github.com/strongloop/loopback-next/commit/ced26437c6fa8bd34fa897325730610ec017bc16))
17
+ * **repository:** allow custom keyFrom for hasmany/hasone ([58efff9](https://github.com/strongloop/loopback-next/commit/58efff9e166fbe1fc820fe6168e18b5c7d9630ce))
18
+
19
+
20
+
21
+
22
+
6
23
  # [1.16.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@1.15.5...@loopback/repository@1.16.0) (2019-12-09)
7
24
 
8
25
 
@@ -23,6 +23,12 @@ function createBelongsToAccessor(belongsToMetadata, targetRepoGetter, sourceRepo
23
23
  const primaryKey = meta.keyTo;
24
24
  const sourceModel = await sourceRepository.findById(sourceId);
25
25
  const foreignKeyValue = sourceModel[foreignKey];
26
+ // workaround to check referential integrity.
27
+ // should be removed once the memory connector ref integrity is done
28
+ // GH issue: https://github.com/strongloop/loopback-next/issues/2333
29
+ if (!foreignKeyValue) {
30
+ return undefined;
31
+ }
26
32
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
33
  const constraint = { [primaryKey]: foreignKeyValue };
28
34
  const constrainedRepo = new belongs_to_repository_1.DefaultBelongsToRepository(targetRepoGetter, constraint);
@@ -1 +1 @@
1
- {"version":3,"file":"belongs-to-accessor.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to-accessor.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;;AAEhE,kDAAiC;AASjC,6DAA8D;AAC9D,mFAAiF;AACjF,mEAAmE;AAEnE,MAAM,KAAK,GAAG,eAAY,CAAC,yCAAyC,CAAC,CAAC;AActE;;GAEG;AACH,SAAgB,uBAAuB,CAMrC,iBAAsC,EACtC,gBAAgE,EAChE,gBAAwD;IAExD,MAAM,IAAI,GAAG,6CAAwB,CAAC,iBAAiB,CAAC,CAAC;IACzD,KAAK,CAAC,0CAA0C,EAAE,IAAI,CAAC,CAAC;IACxD,MAAM,MAAM,GAGR,KAAK,UAAU,4BAA4B,CAAC,QAAkB;QAChE,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC;QAChC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC;QAC9B,MAAM,WAAW,GAAG,MAAM,gBAAgB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC9D,MAAM,eAAe,GAAG,WAAW,CAAC,UAA0B,CAAC,CAAC;QAChE,8DAA8D;QAC9D,MAAM,UAAU,GAAQ,EAAC,CAAC,UAAU,CAAC,EAAE,eAAe,EAAC,CAAC;QACxD,MAAM,eAAe,GAAG,IAAI,kDAA0B,CACpD,gBAAgB,EAChB,UAAgC,CACjC,CAAC;QACF,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC;IAC/B,CAAC,CAAC;IAEF,MAAM,CAAC,iBAAiB,GAAG,gEAAgC,CACzD,IAAI,EACJ,gBAAgB,CACjB,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AAlCD,0DAkCC"}
1
+ {"version":3,"file":"belongs-to-accessor.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to-accessor.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;;AAEhE,kDAAiC;AASjC,6DAA8D;AAC9D,mFAAiF;AACjF,mEAAmE;AAEnE,MAAM,KAAK,GAAG,eAAY,CAAC,yCAAyC,CAAC,CAAC;AActE;;GAEG;AACH,SAAgB,uBAAuB,CAMrC,iBAAsC,EACtC,gBAAgE,EAChE,gBAAwD;IAExD,MAAM,IAAI,GAAG,6CAAwB,CAAC,iBAAiB,CAAC,CAAC;IACzD,KAAK,CAAC,0CAA0C,EAAE,IAAI,CAAC,CAAC;IACxD,MAAM,MAAM,GAGR,KAAK,UAAU,4BAA4B,CAAC,QAAkB;QAChE,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC;QAChC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC;QAC9B,MAAM,WAAW,GAAG,MAAM,gBAAgB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC9D,MAAM,eAAe,GAAG,WAAW,CAAC,UAA0B,CAAC,CAAC;QAChE,6CAA6C;QAC7C,oEAAoE;QACpE,oEAAoE;QACpE,IAAI,CAAC,eAAe,EAAE;YACpB,OAAQ,SAA+B,CAAC;SACzC;QACD,8DAA8D;QAC9D,MAAM,UAAU,GAAQ,EAAC,CAAC,UAAU,CAAC,EAAE,eAAe,EAAC,CAAC;QACxD,MAAM,eAAe,GAAG,IAAI,kDAA0B,CACpD,gBAAgB,EAChB,UAAgC,CACjC,CAAC;QACF,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC;IAC/B,CAAC,CAAC;IAEF,MAAM,CAAC,iBAAiB,GAAG,gEAAgC,CACzD,IAAI,EACJ,gBAAgB,CACjB,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AAxCD,0DAwCC"}
@@ -36,7 +36,15 @@ function resolveHasManyMetadata(relationMeta) {
36
36
  const reason = 'source model must be defined';
37
37
  throw new errors_1.InvalidRelationError(reason, relationMeta);
38
38
  }
39
- const keyFrom = sourceModel.getIdProperties()[0];
39
+ // keyFrom defaults to id property
40
+ let keyFrom;
41
+ if (relationMeta.keyFrom &&
42
+ relationMeta.source.definition.properties[relationMeta.keyFrom]) {
43
+ keyFrom = relationMeta.keyFrom;
44
+ }
45
+ else {
46
+ keyFrom = sourceModel.getIdProperties()[0];
47
+ }
40
48
  // Make sure that if it already keys to the foreign key property,
41
49
  // the key exists in the target model
42
50
  if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
@@ -50,7 +58,10 @@ function resolveHasManyMetadata(relationMeta) {
50
58
  const reason = `target model ${targetModel.name} is missing definition of foreign key ${defaultFkName}`;
51
59
  throw new errors_1.InvalidRelationError(reason, relationMeta);
52
60
  }
53
- return Object.assign(relationMeta, { keyFrom, keyTo: defaultFkName });
61
+ return Object.assign(relationMeta, {
62
+ keyFrom,
63
+ keyTo: defaultFkName,
64
+ });
54
65
  }
55
66
  exports.resolveHasManyMetadata = resolveHasManyMetadata;
56
67
  //# sourceMappingURL=has-many.helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"has-many.helpers.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;;AAEhE,kDAAiC;AACjC,mCAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAkE;AAElE,MAAM,KAAK,GAAG,eAAY,CAAC,sCAAsC,CAAC,CAAC;AAWnE;;;;;;GAMG;AACH,SAAgB,sBAAsB,CACpC,YAA+B;IAE/B,IAAK,YAAY,CAAC,IAAqB,KAAK,6BAAY,CAAC,OAAO,EAAE;QAChE,MAAM,MAAM,GAAG,+BAA+B,CAAC;QAC/C,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;QACh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 {"version":3,"file":"has-many.helpers.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;;AAEhE,kDAAiC;AACjC,mCAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAkE;AAElE,MAAM,KAAK,GAAG,eAAY,CAAC,sCAAsC,CAAC,CAAC;AAWnE;;;;;;GAMG;AACH,SAAgB,sBAAsB,CACpC,YAA+B;IAE/B,IAAK,YAAY,CAAC,IAAqB,KAAK,6BAAY,CAAC,OAAO,EAAE;QAChE,MAAM,MAAM,GAAG,+BAA+B,CAAC;QAC/C,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,EAAE,CAAC;IAC1C,MAAM,qBAAqB,GACzB,WAAW,CAAC,UAAU,IAAI,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC;IAE9D,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,kCAAkC;IAClC,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,WAAW,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,CAAC;KAC5C;IACD,iEAAiE;IACjE,qCAAqC;IACrC,IAAI,YAAY,CAAC,KAAK,IAAI,qBAAqB,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QACnE,wEAAwE;QACxE,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,EAAC,OAAO,EAAC,CAA8B,CAAC;KAC5E;IAED,KAAK,CACH,2CAA2C,EAC3C,WAAW,CAAC,SAAS,EACrB,WAAW,CACZ,CAAC;IACF,MAAM,aAAa,GAAG,kBAAS,CAAC,WAAW,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;IAC/D,MAAM,oBAAoB,GAAG,qBAAqB,CAAC,aAAa,CAAC,CAAC;IAElE,IAAI,CAAC,oBAAoB,EAAE;QACzB,MAAM,MAAM,GAAG,gBAAgB,WAAW,CAAC,IAAI,yCAAyC,aAAa,EAAE,CAAC;QACxG,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;QACjC,OAAO;QACP,KAAK,EAAE,aAAa;KACQ,CAAC,CAAC;AAClC,CAAC;AAzDD,wDAyDC"}
@@ -36,7 +36,15 @@ function resolveHasOneMetadata(relationMeta) {
36
36
  const reason = 'source model must be defined';
37
37
  throw new errors_1.InvalidRelationError(reason, relationMeta);
38
38
  }
39
- const keyFrom = sourceModel.getIdProperties()[0];
39
+ // keyFrom defaults to id property
40
+ let keyFrom;
41
+ if (relationMeta.keyFrom &&
42
+ relationMeta.source.definition.properties[relationMeta.keyFrom]) {
43
+ keyFrom = relationMeta.keyFrom;
44
+ }
45
+ else {
46
+ keyFrom = sourceModel.getIdProperties()[0];
47
+ }
40
48
  // Make sure that if it already keys to the foreign key property,
41
49
  // the key exists in the target model
42
50
  if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
@@ -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AAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,EAAC,OAAO,EAAE,KAAK,EAAE,aAAa,EAAC,CAAC,CAAC;AACtE,CAAC;AAvDD,sDAuDC"}
@@ -44,13 +44,56 @@ export interface HasManyDefinition extends RelationDefinitionBase {
44
44
  type: RelationType.hasMany;
45
45
  targetsMany: true;
46
46
  /**
47
- * The foreign key used by the target model.
47
+ * keyTo: The foreign key used by the target model for this relation.
48
+ * keyFrom: The source key used by the source model for this relation.
48
49
  *
49
50
  * E.g. when a Customer has many Order instances, then keyTo is "customerId".
50
51
  * Note that "customerId" is the default FK assumed by the framework, users
51
52
  * can provide a custom FK name by setting "keyTo".
53
+ * And Customer.id is keyFrom. keyFrom defaults to the id property of a model.
54
+ * Users can provide a custom source key name by setting "keyTo".
55
+ *
52
56
  */
53
57
  keyTo?: string;
58
+ keyFrom?: string;
59
+ }
60
+ /**
61
+ * A `hasManyThrough` relation defines a many-to-many connection with another model.
62
+ * This relation indicates that the declaring model can be matched with zero or more
63
+ * instances of another model by proceeding through a third model.
64
+ *
65
+ * Warning: The hasManyThrough interface is experimental and is subject to change.
66
+ * If backwards-incompatible changes are made, a new major version may not be
67
+ * released.
68
+ */
69
+ export interface HasManyThroughDefinition extends RelationDefinitionBase {
70
+ type: RelationType.hasMany;
71
+ targetsMany: true;
72
+ /**
73
+ * The foreign key in the source model, e.g. Customer#id.
74
+ */
75
+ keyFrom: string;
76
+ /**
77
+ * The primary key of the target model, e.g Seller#id.
78
+ */
79
+ keyTo: string;
80
+ through: {
81
+ /**
82
+ * The through model of this relation.
83
+ *
84
+ * E.g. when a Customer has many Order instances and a Seller has many Order instances,
85
+ * then Order is through.
86
+ */
87
+ model: TypeResolver<Entity, typeof Entity>;
88
+ /**
89
+ * The foreign key of the source model defined in the through model, e.g. Order#customerId
90
+ */
91
+ keyFrom: string;
92
+ /**
93
+ * The foreign key of the target model defined in the through model, e.g. Order#sellerId
94
+ */
95
+ keyTo: string;
96
+ };
54
97
  }
55
98
  export interface BelongsToDefinition extends RelationDefinitionBase {
56
99
  type: RelationType.belongsTo;
@@ -62,18 +105,22 @@ export interface HasOneDefinition extends RelationDefinitionBase {
62
105
  type: RelationType.hasOne;
63
106
  targetsMany: false;
64
107
  /**
65
- * The foreign key used by the target model.
108
+ * keyTo: The foreign key used by the target model for this relation.
109
+ * keyFrom: The source key used by the source model for this relation.
66
110
  *
67
111
  * E.g. when a Customer has one Address instance, then keyTo is "customerId".
68
112
  * Note that "customerId" is the default FK assumed by the framework, users
69
113
  * can provide a custom FK name by setting "keyTo".
114
+ * And Customer.id is keyFrom. keyFrom defaults to the id property of a model.
115
+ * Users can provide a custom source key name by setting "keyTo".
70
116
  */
71
117
  keyTo?: string;
118
+ keyFrom?: string;
72
119
  }
73
120
  /**
74
121
  * A union type describing all possible Relation metadata objects.
75
122
  */
76
- export declare type RelationMetadata = HasManyDefinition | BelongsToDefinition | HasOneDefinition | RelationDefinitionBase;
123
+ export declare type RelationMetadata = HasManyDefinition | HasManyThroughDefinition | BelongsToDefinition | HasOneDefinition | RelationDefinitionBase;
77
124
  export { Getter } from '@loopback/context';
78
125
  /**
79
126
  * @returns An array of resolved values, the items must be ordered in the same
@@ -1 +1 @@
1
- {"version":3,"file":"relation.types.js","sourceRoot":"","sources":["../../src/relations/relation.types.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAOhE,IAAY,YAQX;AARD,WAAY,YAAY;IACtB,uCAAuB,CAAA;IACvB,iCAAiB,CAAA;IACjB,mCAAmB,CAAA;IACnB,uCAAuB,CAAA;IACvB,yCAAyB,CAAA;IACzB,+CAA+B,CAAA;IAC/B,iDAAiC,CAAA;AACnC,CAAC,EARW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAQvB;AA4FD,6EAA6E;AAC7E,6CAAyC;AAAjC,2BAAA,MAAM,CAAA"}
1
+ {"version":3,"file":"relation.types.js","sourceRoot":"","sources":["../../src/relations/relation.types.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAOhE,IAAY,YAQX;AARD,WAAY,YAAY;IACtB,uCAAuB,CAAA;IACvB,iCAAiB,CAAA;IACjB,mCAAmB,CAAA;IACnB,uCAAuB,CAAA;IACvB,yCAAyB,CAAA;IACzB,+CAA+B,CAAA;IAC/B,iDAAiC,CAAA;AACnC,CAAC,EARW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAQvB;AAkJD,6EAA6E;AAC7E,6CAAyC;AAAjC,2BAAA,MAAM,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loopback/repository",
3
- "version": "1.16.0",
3
+ "version": "1.17.0",
4
4
  "description": "Repository based persistence for LoopBack 4",
5
5
  "engines": {
6
6
  "node": ">=8.9"
@@ -18,17 +18,17 @@
18
18
  "copyright.owner": "IBM Corp.",
19
19
  "license": "MIT",
20
20
  "devDependencies": {
21
- "@loopback/build": "^3.0.0",
22
- "@loopback/eslint-config": "^5.0.0",
23
- "@loopback/testlab": "^1.10.0",
21
+ "@loopback/build": "^3.0.1",
22
+ "@loopback/eslint-config": "^5.0.1",
23
+ "@loopback/testlab": "^1.10.1",
24
24
  "@types/bson": "^4.0.1",
25
25
  "@types/lodash": "^4.14.149",
26
- "@types/node": "^10.17.6",
26
+ "@types/node": "^10.17.13",
27
27
  "bson": "4.0.2"
28
28
  },
29
29
  "dependencies": {
30
- "@loopback/context": "^1.25.0",
31
- "@loopback/core": "^1.12.0",
30
+ "@loopback/context": "^1.25.1",
31
+ "@loopback/core": "^1.12.1",
32
32
  "@types/debug": "^4.1.5",
33
33
  "debug": "^4.1.1",
34
34
  "lodash": "^4.17.15",
@@ -47,5 +47,5 @@
47
47
  "url": "https://github.com/strongloop/loopback-next.git",
48
48
  "directory": "packages/repository"
49
49
  },
50
- "gitHead": "89eb61bacaed75e6eb61ae6840cea266cb888659"
50
+ "gitHead": "598baf6e84de3917bb67aff47a1ab1cb1111e3d2"
51
51
  }
@@ -53,6 +53,12 @@ export function createBelongsToAccessor<
53
53
  const primaryKey = meta.keyTo;
54
54
  const sourceModel = await sourceRepository.findById(sourceId);
55
55
  const foreignKeyValue = sourceModel[foreignKey as keyof Source];
56
+ // workaround to check referential integrity.
57
+ // should be removed once the memory connector ref integrity is done
58
+ // GH issue: https://github.com/strongloop/loopback-next/issues/2333
59
+ if (!foreignKeyValue) {
60
+ return (undefined as unknown) as Target;
61
+ }
56
62
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
63
  const constraint: any = {[primaryKey]: foreignKeyValue};
58
64
  const constrainedRepo = new DefaultBelongsToRepository(
@@ -50,8 +50,16 @@ export function resolveHasManyMetadata(
50
50
  throw new InvalidRelationError(reason, relationMeta);
51
51
  }
52
52
 
53
- const keyFrom = sourceModel.getIdProperties()[0];
54
-
53
+ // keyFrom defaults to id property
54
+ let keyFrom;
55
+ if (
56
+ relationMeta.keyFrom &&
57
+ relationMeta.source.definition.properties[relationMeta.keyFrom]
58
+ ) {
59
+ keyFrom = relationMeta.keyFrom;
60
+ } else {
61
+ keyFrom = sourceModel.getIdProperties()[0];
62
+ }
55
63
  // Make sure that if it already keys to the foreign key property,
56
64
  // the key exists in the target model
57
65
  if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
@@ -72,5 +80,8 @@ export function resolveHasManyMetadata(
72
80
  throw new InvalidRelationError(reason, relationMeta);
73
81
  }
74
82
 
75
- return Object.assign(relationMeta, {keyFrom, keyTo: defaultFkName});
83
+ return Object.assign(relationMeta, {
84
+ keyFrom,
85
+ keyTo: defaultFkName,
86
+ } as HasManyResolvedDefinition);
76
87
  }
@@ -50,7 +50,16 @@ export function resolveHasOneMetadata(
50
50
  throw new InvalidRelationError(reason, relationMeta);
51
51
  }
52
52
 
53
- const keyFrom = sourceModel.getIdProperties()[0];
53
+ // keyFrom defaults to id property
54
+ let keyFrom;
55
+ if (
56
+ relationMeta.keyFrom &&
57
+ relationMeta.source.definition.properties[relationMeta.keyFrom]
58
+ ) {
59
+ keyFrom = relationMeta.keyFrom;
60
+ } else {
61
+ keyFrom = sourceModel.getIdProperties()[0];
62
+ }
54
63
 
55
64
  // Make sure that if it already keys to the foreign key property,
56
65
  // the key exists in the target model
@@ -59,13 +59,62 @@ export interface HasManyDefinition extends RelationDefinitionBase {
59
59
  targetsMany: true;
60
60
 
61
61
  /**
62
- * The foreign key used by the target model.
62
+ * keyTo: The foreign key used by the target model for this relation.
63
+ * keyFrom: The source key used by the source model for this relation.
63
64
  *
64
65
  * E.g. when a Customer has many Order instances, then keyTo is "customerId".
65
66
  * Note that "customerId" is the default FK assumed by the framework, users
66
67
  * can provide a custom FK name by setting "keyTo".
68
+ * And Customer.id is keyFrom. keyFrom defaults to the id property of a model.
69
+ * Users can provide a custom source key name by setting "keyTo".
70
+ *
67
71
  */
68
72
  keyTo?: string;
73
+ keyFrom?: string;
74
+ }
75
+
76
+ /**
77
+ * A `hasManyThrough` relation defines a many-to-many connection with another model.
78
+ * This relation indicates that the declaring model can be matched with zero or more
79
+ * instances of another model by proceeding through a third model.
80
+ *
81
+ * Warning: The hasManyThrough interface is experimental and is subject to change.
82
+ * If backwards-incompatible changes are made, a new major version may not be
83
+ * released.
84
+ */
85
+ export interface HasManyThroughDefinition extends RelationDefinitionBase {
86
+ type: RelationType.hasMany;
87
+ targetsMany: true;
88
+
89
+ /**
90
+ * The foreign key in the source model, e.g. Customer#id.
91
+ */
92
+ keyFrom: string;
93
+
94
+ /**
95
+ * The primary key of the target model, e.g Seller#id.
96
+ */
97
+ keyTo: string;
98
+
99
+ through: {
100
+ /**
101
+ * The through model of this relation.
102
+ *
103
+ * E.g. when a Customer has many Order instances and a Seller has many Order instances,
104
+ * then Order is through.
105
+ */
106
+ model: TypeResolver<Entity, typeof Entity>;
107
+
108
+ /**
109
+ * The foreign key of the source model defined in the through model, e.g. Order#customerId
110
+ */
111
+ keyFrom: string;
112
+
113
+ /**
114
+ * The foreign key of the target model defined in the through model, e.g. Order#sellerId
115
+ */
116
+ keyTo: string;
117
+ };
69
118
  }
70
119
 
71
120
  export interface BelongsToDefinition extends RelationDefinitionBase {
@@ -88,13 +137,17 @@ export interface HasOneDefinition extends RelationDefinitionBase {
88
137
  targetsMany: false;
89
138
 
90
139
  /**
91
- * The foreign key used by the target model.
140
+ * keyTo: The foreign key used by the target model for this relation.
141
+ * keyFrom: The source key used by the source model for this relation.
92
142
  *
93
143
  * E.g. when a Customer has one Address instance, then keyTo is "customerId".
94
144
  * Note that "customerId" is the default FK assumed by the framework, users
95
145
  * can provide a custom FK name by setting "keyTo".
146
+ * And Customer.id is keyFrom. keyFrom defaults to the id property of a model.
147
+ * Users can provide a custom source key name by setting "keyTo".
96
148
  */
97
149
  keyTo?: string;
150
+ keyFrom?: string;
98
151
  }
99
152
 
100
153
  /**
@@ -102,6 +155,7 @@ export interface HasOneDefinition extends RelationDefinitionBase {
102
155
  */
103
156
  export type RelationMetadata =
104
157
  | HasManyDefinition
158
+ | HasManyThroughDefinition
105
159
  | BelongsToDefinition
106
160
  | HasOneDefinition
107
161
  // TODO(bajtos) add other relation types and remove RelationDefinitionBase once