Skip to content

Commit 875dc19

Browse files
committed
fix: remove import for model for same model relation
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 19b1184 commit 875dc19

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

packages/cli/generators/discover/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
382382

383383
if (this.options.relations) {
384384
const relationImports = [];
385-
const relationDestinationImports = [];
385+
let relationDestinationImports = [];
386386
const foreignKeys = {};
387387
for (const relationName in templateData.settings.relations) {
388388
const relation = templateData.settings.relations[relationName];
@@ -410,6 +410,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
410410
});
411411
}
412412
}
413+
// remove model import if the model relation is with itself
414+
if (relationDestinationImports.includes(templateData['name'])) {
415+
relationDestinationImports = relationDestinationImports.filter(
416+
e => e !== templateData['name'],
417+
);
418+
}
413419
templateData.relationImports = relationImports;
414420
templateData.relationDestinationImports = relationDestinationImports;
415421
// Delete relation from modelSettings

packages/cli/snapshots/integration/generators/discover.integration.snapshots.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,53 @@ export type ViewWithRelations = View & ViewRelations;
5757
`;
5858

5959

60+
exports[`lb4 discover integration model discovery discovers models with --relations 1`] = `
61+
import {Entity, model, property, belongsTo} from '@loopback/repository';
62+
63+
@model({
64+
settings: {
65+
foreignKeys: {
66+
doctorRel: {name: 'doctorRel', entity: 'Doctor', entityKey: 'id', foreignKey: 'reportsTo'}
67+
}
68+
}
69+
})
70+
export class Doctor extends Entity {
71+
@property({
72+
type: 'number',
73+
scale: 0,
74+
id: 1,
75+
})
76+
id?: number;
77+
78+
@property({
79+
type: 'string',
80+
length: 45,
81+
})
82+
name?: string;
83+
84+
@belongsTo(() => Doctor)
85+
reportsTo?: number;
86+
87+
// Define well-known properties here
88+
89+
// Indexer property to allow additional data
90+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
91+
[prop: string]: any;
92+
93+
constructor(data?: Partial<Doctor>) {
94+
super(data);
95+
}
96+
}
97+
98+
export interface DoctorRelations {
99+
// describe navigational properties here
100+
}
101+
102+
export type DoctorWithRelations = Doctor & DoctorRelations;
103+
104+
`;
105+
106+
60107
exports[`lb4 discover integration model discovery does not mark id property as required based on optionalId option 1`] = `
61108
import {Entity, model, property} from '@loopback/repository';
62109

packages/cli/test/fixtures/discover/mem.datasource.js.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ const fullDefinitions = [
134134
},
135135
{
136136
'name': 'Doctor',
137+
className: 'Doctor',
138+
modelBaseClass: 'Entity',
139+
isModelBaseBuiltin: true,
140+
options: {
141+
relations: {
142+
doctorRel: { model: 'Doctor', type: 'belongsTo', foreignKey: 'reportsTo' },
143+
}
144+
},
137145
'properties': {
138146
'id': {
139147
'type': 'Number',
@@ -150,7 +158,24 @@ const fullDefinitions = [
150158
'precision': null,
151159
'scale': null,
152160
},
161+
reportsTo: {
162+
type: "number",
163+
precision: 10,
164+
scale: 0,
165+
generated: 0,
166+
mysql: "{columnName: 'reportsTo', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'Y', generated: 0}",
167+
tsType: 'number'
168+
}
153169
},
170+
allowAdditionalProperties: true,
171+
modelSettings: {
172+
settings: {
173+
idInjection: false,
174+
relations: {
175+
doctorRel: {model: 'Doctor', type: 'belongsTo', foreignKey: 'reportsTo'},
176+
}
177+
}
178+
}
154179
},
155180
{
156181
'name': 'Patient',

packages/cli/test/integration/generators/discover.integration.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const appointmentModel = path.join(
9696
sandbox.path,
9797
'src/models/appointment.model.ts',
9898
);
99+
const doctorModel = path.join(sandbox.path, 'src/models/doctor.model.ts');
99100

100101
const defaultExpectedIndexFile = path.join(sandbox.path, 'src/models/index.ts');
101102
const movedExpectedTestModel = path.join(sandbox.path, 'src/test.model.ts');
@@ -223,6 +224,18 @@ describe('lb4 discover integration', () => {
223224
assert.file(appointmentModel);
224225
expectFileToMatchSnapshot(appointmentModel);
225226
});
227+
it('discovers models with --relations', async () => {
228+
await testUtils
229+
.executeGenerator(generator)
230+
.inDir(sandbox.path, () =>
231+
testUtils.givenLBProject(sandbox.path, {
232+
additionalFiles: SANDBOX_FILES,
233+
}),
234+
)
235+
.withOptions(relationsSetTrue);
236+
assert.file(doctorModel);
237+
expectFileToMatchSnapshot(doctorModel);
238+
});
226239
});
227240
it('generates specific models without prompts using --models', async () => {
228241
await testUtils

0 commit comments

Comments
 (0)