Skip to content

Commit e0f623e

Browse files
committed
add support for custom keyTo property in belongsTo relation generator and controller template
1 parent d1838ad commit e0f623e

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

packages/cli/generators/relation/belongs-to-relation.generator.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module.exports = class BelongsToRelationGenerator extends (
4949
);
5050

5151
this.artifactInfo.relationPropertyName = options.relationName;
52+
this.artifactInfo.keyTo = options.keyTo;
5253
this.artifactInfo.targetModelPrimaryKey =
5354
options.destinationModelPrimaryKey;
5455
this.artifactInfo.targetModelPrimaryKeyType =
@@ -85,7 +86,11 @@ module.exports = class BelongsToRelationGenerator extends (
8586
const relationName = options.relationName;
8687
const defaultRelationName = options.defaultRelationName;
8788
const foreignKeyName = options.foreignKeyName;
88-
const fktype = options.destinationModelPrimaryKeyType;
89+
const keyTo = options.keyTo;
90+
const fktype = keyTo
91+
? relationUtils.getModelPropertyType(modelDir, targetModel, keyTo) ||
92+
options.destinationModelPrimaryKeyType
93+
: options.destinationModelPrimaryKeyType;
8994

9095
const project = new relationUtils.AstLoopBackProject();
9196
const sourceFile = relationUtils.addFileToProject(
@@ -103,6 +108,7 @@ module.exports = class BelongsToRelationGenerator extends (
103108
defaultRelationName,
104109
foreignKeyName,
105110
fktype,
111+
keyTo,
106112
);
107113

108114
relationUtils.addProperty(sourceClass, modelProperty);
@@ -123,20 +129,29 @@ module.exports = class BelongsToRelationGenerator extends (
123129
defaultRelationName,
124130
foreignKeyName,
125131
fktype,
132+
keyTo,
126133
) {
134+
const keyToOption = keyTo ? `, keyTo: '${keyTo}'` : '';
135+
127136
// checks if relation name is customized
128137
let relationDecorator = [
129138
{
130139
name: 'belongsTo',
131-
arguments: [`() => ${className}`],
140+
arguments: [
141+
keyTo
142+
? `() => ${className}, {keyTo: '${keyTo}'}`
143+
: `() => ${className}`,
144+
],
132145
},
133146
];
134147
// already checked if the relation name is the same as the source key before
135148
if (defaultRelationName !== relationName) {
136149
relationDecorator = [
137150
{
138151
name: 'belongsTo',
139-
arguments: [`() => ${className}, {name: '${relationName}'}`],
152+
arguments: [
153+
`() => ${className}, {name: '${relationName}'${keyToOption}}`,
154+
],
140155
},
141156
];
142157
}

packages/cli/generators/relation/templates/controller-relation-template-belongs-to.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class <%= controllerClassName %> {
2121
@get('/<%= sourceModelPath %>/{id}/<%= targetModelName %>', {
2222
responses: {
2323
'200': {
24-
description: '<%= targetModelClassName %> belonging to <%= sourceModelClassName %>',
24+
description: '<%= targetModelClassName %> belonging to <%= sourceModelClassName %><%= keyTo ? " (resolved via " + targetModelClassName + "." + keyTo + ")" : "" %>',
2525
content: {
2626
'application/json': {
2727
schema: getModelSchemaRef(<%= targetModelClassName %>),

0 commit comments

Comments
 (0)