From 5fc5eeae55f1c45bfc5e73528aa7526597933108 Mon Sep 17 00:00:00 2001 From: Abubakar Awan Date: Mon, 6 Jul 2026 20:24:46 +0500 Subject: [PATCH] add support for custom keyTo property in belongsTo relation generator and controller template Signed-off-by: Abubakar Awan --- .../relation/belongs-to-relation.generator.js | 21 ++++++++++++++++--- ...roller-relation-template-belongs-to.ts.ejs | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/cli/generators/relation/belongs-to-relation.generator.js b/packages/cli/generators/relation/belongs-to-relation.generator.js index f5a1d46e180d..f26c7c3e8232 100644 --- a/packages/cli/generators/relation/belongs-to-relation.generator.js +++ b/packages/cli/generators/relation/belongs-to-relation.generator.js @@ -49,6 +49,7 @@ module.exports = class BelongsToRelationGenerator extends ( ); this.artifactInfo.relationPropertyName = options.relationName; + this.artifactInfo.keyTo = options.keyTo; this.artifactInfo.targetModelPrimaryKey = options.destinationModelPrimaryKey; this.artifactInfo.targetModelPrimaryKeyType = @@ -85,7 +86,11 @@ module.exports = class BelongsToRelationGenerator extends ( const relationName = options.relationName; const defaultRelationName = options.defaultRelationName; const foreignKeyName = options.foreignKeyName; - const fktype = options.destinationModelPrimaryKeyType; + const keyTo = options.keyTo; + const fktype = keyTo + ? relationUtils.getModelPropertyType(modelDir, targetModel, keyTo) || + options.destinationModelPrimaryKeyType + : options.destinationModelPrimaryKeyType; const project = new relationUtils.AstLoopBackProject(); const sourceFile = relationUtils.addFileToProject( @@ -103,6 +108,7 @@ module.exports = class BelongsToRelationGenerator extends ( defaultRelationName, foreignKeyName, fktype, + keyTo, ); relationUtils.addProperty(sourceClass, modelProperty); @@ -123,12 +129,19 @@ module.exports = class BelongsToRelationGenerator extends ( defaultRelationName, foreignKeyName, fktype, + keyTo, ) { + const keyToOption = keyTo ? `, keyTo: '${keyTo}'` : ''; + // checks if relation name is customized let relationDecorator = [ { name: 'belongsTo', - arguments: [`() => ${className}`], + arguments: [ + keyTo + ? `() => ${className}, {keyTo: '${keyTo}'}` + : `() => ${className}`, + ], }, ]; // already checked if the relation name is the same as the source key before @@ -136,7 +149,9 @@ module.exports = class BelongsToRelationGenerator extends ( relationDecorator = [ { name: 'belongsTo', - arguments: [`() => ${className}, {name: '${relationName}'}`], + arguments: [ + `() => ${className}, {name: '${relationName}'${keyToOption}}`, + ], }, ]; } diff --git a/packages/cli/generators/relation/templates/controller-relation-template-belongs-to.ts.ejs b/packages/cli/generators/relation/templates/controller-relation-template-belongs-to.ts.ejs index 1451cb6cfb7b..e70f3ec0cb78 100644 --- a/packages/cli/generators/relation/templates/controller-relation-template-belongs-to.ts.ejs +++ b/packages/cli/generators/relation/templates/controller-relation-template-belongs-to.ts.ejs @@ -21,7 +21,7 @@ export class <%= controllerClassName %> { @get('/<%= sourceModelPath %>/{id}/<%= targetModelName %>', { responses: { '200': { - description: '<%= targetModelClassName %> belonging to <%= sourceModelClassName %>', + description: '<%= targetModelClassName %> belonging to <%= sourceModelClassName %><%= keyTo ? " (resolved via " + targetModelClassName + "." + keyTo + ")" : "" %>', content: { 'application/json': { schema: getModelSchemaRef(<%= targetModelClassName %>),