Skip to content

Commit 67ce8fd

Browse files
committed
fix: enable control update in case of multiple belongsTo relation
Signed-off-by: Muhammad Aaqil <aaqilniz@yahoo.com>
1 parent 5425762 commit 67ce8fd

1 file changed

Lines changed: 48 additions & 8 deletions

File tree

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

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,54 @@ module.exports = class BelongsToRelationGenerator extends (
6464
const dest = this.destinationPath(
6565
path.join(this.artifactInfo.outDir, this.artifactInfo.outFile),
6666
);
67-
68-
this.copyTemplatedFiles(source, dest, this.artifactInfo);
69-
await relationUtils.addExportController(
70-
this,
71-
path.resolve(this.artifactInfo.outDir, 'index.ts'),
72-
this.artifactInfo.controllerClassName,
73-
utils.toFileName(this.artifactInfo.name) + '.controller',
74-
);
67+
if (this.fs.exists(dest)) {
68+
const project = new relationUtils.AstLoopBackProject();
69+
const sourceFile = project.addSourceFileAtPath(dest);
70+
const sourceClass = relationUtils.getClassObj(
71+
sourceFile,
72+
this.artifactInfo.controllerClassName,
73+
);
74+
const structure = sourceClass.getStructure();
75+
structure.methods.forEach((method, index) => {
76+
if (method.name.startsWith('get')) {
77+
const {statements} = method;
78+
const lastStatementIndex = statements.length - 1;
79+
let returnStatement = statements[lastStatementIndex];
80+
returnStatement = returnStatement.substring(
81+
returnStatement.indexOf('return') + 6,
82+
returnStatement.lastIndexOf(';'),
83+
);
84+
returnStatement = returnStatement.trim();
85+
86+
if (returnStatement.startsWith('[')) {
87+
returnStatement = returnStatement.substring(
88+
returnStatement.indexOf('[') + 1,
89+
returnStatement.lastIndexOf(']'),
90+
);
91+
}
92+
returnStatement = `${returnStatement},\n\t this.${this.artifactInfo.paramSourceRepository}.${this.artifactInfo.relationPropertyName}(${this.artifactInfo.sourceModelPrimaryKey})`;
93+
structure.methods[index].statements[
94+
lastStatementIndex
95+
] = `return [${returnStatement}];`;
96+
structure.methods[
97+
index
98+
].returnType = `Promise<Promise<${this.artifactInfo.targetModelClassName}>[]>`;
99+
}
100+
});
101+
structure.methods[0].parameters[0].decorators[0].name =
102+
'param.path.' + structure.methods[0].parameters[0].decorators[0].name;
103+
sourceClass.set(structure);
104+
sourceClass.formatText();
105+
await sourceFile.save();
106+
} else {
107+
this.copyTemplatedFiles(source, dest, this.artifactInfo);
108+
await relationUtils.addExportController(
109+
this,
110+
path.resolve(this.artifactInfo.outDir, 'index.ts'),
111+
this.artifactInfo.controllerClassName,
112+
utils.toFileName(this.artifactInfo.name) + '.controller',
113+
);
114+
}
75115
}
76116

77117
async generateModels(options) {

0 commit comments

Comments
 (0)