Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand All @@ -103,6 +108,7 @@ module.exports = class BelongsToRelationGenerator extends (
defaultRelationName,
foreignKeyName,
fktype,
keyTo,
);

relationUtils.addProperty(sourceClass, modelProperty);
Expand All @@ -123,20 +129,29 @@ 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
if (defaultRelationName !== relationName) {
relationDecorator = [
{
name: 'belongsTo',
arguments: [`() => ${className}, {name: '${relationName}'}`],
arguments: [
`() => ${className}, {name: '${relationName}'${keyToOption}}`,
],
},
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>),
Expand Down
Loading