Skip to content

Commit

Permalink
fix: remove import for model for same model relation
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Feb 23, 2025
1 parent 834572d commit c7d9357
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {

if (this.options.relations) {
const relationImports = [];
const relationDestinationImports = [];
let relationDestinationImports = [];
const foreignKeys = {};
for (const relationName in templateData.settings.relations) {
const relation = templateData.settings.relations[relationName];
Expand Down Expand Up @@ -410,6 +410,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
});
}
}
// remove model import if the model relation is with itself
if (relationDestinationImports.includes(templateData['name'])) {
relationDestinationImports = relationDestinationImports.filter(
e => e !== templateData['name'],
);
}
templateData.relationImports = relationImports;
templateData.relationDestinationImports = relationDestinationImports;
// Delete relation from modelSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,53 @@ export type ViewWithRelations = View & ViewRelations;
`;


exports[`lb4 discover integration model discovery discovers models with --relations 1`] = `
import {Entity, model, property, belongsTo} from '@loopback/repository';
@model({
settings: {
foreignKeys: {
doctorRel: {name: 'doctorRel', entity: 'Doctor', entityKey: 'id', foreignKey: 'reportsTo'}
}
}
})
export class Doctor extends Entity {
@property({
type: 'number',
scale: 0,
id: 1,
})
id?: number;
@property({
type: 'string',
length: 45,
})
name?: string;
@belongsTo(() => Doctor)
reportsTo?: number;
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<Doctor>) {
super(data);
}
}
export interface DoctorRelations {
// describe navigational properties here
}
export type DoctorWithRelations = Doctor & DoctorRelations;
`;


exports[`lb4 discover integration model discovery does not mark id property as required based on optionalId option 1`] = `
import {Entity, model, property} from '@loopback/repository';
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/test/fixtures/discover/mem.datasource.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ const fullDefinitions = [
},
{
'name': 'Doctor',
className: 'Doctor',
modelBaseClass: 'Entity',
isModelBaseBuiltin: true,
options: {
relations: {
doctorRel: { model: 'Doctor', type: 'belongsTo', foreignKey: 'reportsTo' },
}
},
'properties': {
'id': {
'type': 'Number',
Expand All @@ -150,7 +158,24 @@ const fullDefinitions = [
'precision': null,
'scale': null,
},
reportsTo: {
type: "number",
precision: 10,
scale: 0,
generated: 0,
mysql: "{columnName: 'reportsTo', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'Y', generated: 0}",
tsType: 'number'
}
},
allowAdditionalProperties: true,
modelSettings: {
settings: {
idInjection: false,
relations: {
doctorRel: {model: 'Doctor', type: 'belongsTo', foreignKey: 'reportsTo'},
}
}
}
},
{
'name': 'Patient',
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/test/integration/generators/discover.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const appointmentModel = path.join(
sandbox.path,
'src/models/appointment.model.ts',
);
const doctorModel = path.join(sandbox.path, 'src/models/doctor.model.ts');

const defaultExpectedIndexFile = path.join(sandbox.path, 'src/models/index.ts');
const movedExpectedTestModel = path.join(sandbox.path, 'src/test.model.ts');
Expand Down Expand Up @@ -223,6 +224,18 @@ describe('lb4 discover integration', () => {
assert.file(appointmentModel);
expectFileToMatchSnapshot(appointmentModel);
});
it('discovers models with --relations', async () => {
await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {
additionalFiles: SANDBOX_FILES,
}),
)
.withOptions(relationsSetTrue);
assert.file(doctorModel);
expectFileToMatchSnapshot(doctorModel);
});
});
it('generates specific models without prompts using --models', async () => {
await testUtils
Expand Down

0 comments on commit c7d9357

Please sign in to comment.