Skip to content

Commit 2d5b659

Browse files
committed
fix: check for existing relationImports
Signed-off-by: Muhammad Aaqil <aaqilniz@yahoo.com>
1 parent e606742 commit 2d5b659

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed

packages/cli/generators/discover/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
383383
Object.assign(templateData.properties[relation.foreignKey], {
384384
relation,
385385
});
386-
relationImports.push(relation.type);
386+
if (!relationImports.includes(relation.type)) {
387+
relationImports.push(relation.type);
388+
}
387389
relationDestinationImports.push(relation.model);
388390

389391
foreignKeys[relationName] = {};

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,56 @@ export interface TestRelations {
272272
export type TestWithRelations = Test & TestRelations;
273273
274274
`;
275+
276+
exports[`lb4 discover integration model discovery generate relations with --relations 1`] = `
277+
import {Entity, model, property, belongsTo} from '@loopback/repository';
278+
import {Doctor,Patient} from '.';
279+
280+
@model({
281+
settings: {
282+
foreignKeys: {
283+
doctorIdRel: {name: 'doctorIdRel', entity: 'Doctor', entityKey: 'id', foreignKey: 'doctorId'},
284+
patientIdRel: {
285+
name: 'patientIdRel',
286+
entity: 'Patient',
287+
entityKey: 'pid',
288+
foreignKey: 'patientId'
289+
}
290+
}
291+
}
292+
})
293+
export class Appointment extends Entity {
294+
@property({
295+
type: 'number',
296+
precision: 10,
297+
scale: 0,
298+
generated: 1,
299+
id: 1,
300+
mysql: {columnName: 'id', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'N', generated: 1},
301+
})
302+
id?: number;
303+
304+
@belongsTo(() => Patient)
305+
patientId?: number;
306+
307+
@belongsTo(() => Doctor)
308+
doctorId?: number;
309+
310+
// Define well-known properties here
311+
312+
// Indexer property to allow additional data
313+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
314+
[prop: string]: any;
315+
316+
constructor(data?: Partial<Appointment>) {
317+
super(data);
318+
}
319+
}
320+
321+
export interface AppointmentRelations {
322+
// describe navigational properties here
323+
}
324+
325+
export type AppointmentWithRelations = Appointment & AppointmentRelations;
326+
327+
`;

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ const modelList = [
2020
name:'Naming',
2121
view: true,
2222
schema: 'Naming'
23+
},
24+
{
25+
name:'Doctor',
26+
view: false,
27+
schema: 'Doctor'
28+
},
29+
{
30+
name:'Patient',
31+
view: false,
32+
schema: 'Patient'
33+
},
34+
{
35+
name:'Appointment',
36+
view: false,
37+
schema: 'Appointment'
2338
}
2439
];
2540
// In real model definitions, the schema is contained in options->connectorName->schema
@@ -117,6 +132,95 @@ const fullDefinitions = [
117132
},
118133
},
119134
},
135+
{
136+
'name': 'Doctor',
137+
'properties': {
138+
'id': {
139+
'type': 'Number',
140+
'length': null,
141+
'precision': null,
142+
'required': false,
143+
'scale': 0,
144+
'id': 1,
145+
},
146+
'name': {
147+
'type': 'String',
148+
'required': false,
149+
'length': 45,
150+
'precision': null,
151+
'scale': null,
152+
},
153+
},
154+
},
155+
{
156+
'name': 'Patient',
157+
'properties': {
158+
'pid': {
159+
'type': 'Number',
160+
'length': null,
161+
'precision': null,
162+
'required': false,
163+
'scale': 0,
164+
'id': 1,
165+
},
166+
'name': {
167+
'type': 'String',
168+
'required': false,
169+
'length': 45,
170+
'precision': null,
171+
'scale': null,
172+
},
173+
},
174+
},
175+
{
176+
name: 'Appointment',
177+
className: 'Appointment',
178+
modelBaseClass: 'Entity',
179+
isModelBaseBuiltin: true,
180+
options: {
181+
relations: {
182+
doctorIdRel: { model: 'Doctor', type: 'belongsTo', foreignKey: 'doctorId' },
183+
patientIdRel: { model: 'Patient', type: 'belongsTo', foreignKey: 'patientId' }
184+
}
185+
},
186+
properties: {
187+
id: {
188+
type: "number",
189+
precision: 10,
190+
scale: 0,
191+
generated: 1,
192+
id: 1,
193+
mysql: "{columnName: 'id', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'N', generated: 1}",
194+
tsType: 'number'
195+
},
196+
patientId: {
197+
type: "number",
198+
precision: 10,
199+
scale: 0,
200+
generated: 0,
201+
mysql: "{columnName: 'patientId', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'Y', generated: 0}",
202+
tsType: 'number'
203+
},
204+
doctorId: {
205+
type: "number",
206+
precision: 10,
207+
scale: 0,
208+
generated: 0,
209+
mysql: "{columnName: 'doctorId', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'Y', generated: 0}",
210+
tsType: 'number'
211+
}
212+
},
213+
allowAdditionalProperties: true,
214+
modelSettings: {
215+
settings: {
216+
idInjection: false,
217+
relations: {
218+
doctorIdRel: {model: 'Doctor', type: 'belongsTo', foreignKey: 'doctorId'},
219+
patientIdRel: {model: 'Patient', type: 'belongsTo', foreignKey: 'patientId'}
220+
}
221+
}
222+
}
223+
},
120224
];
121225

122226
class DiscoverOnly extends DataSource {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ const treatTINYINT1AsTinyIntOptions = {
6565
treatTINYINT1AsTinyInt: false,
6666
};
6767

68+
const relationsSetTrue = {
69+
...baseOptions,
70+
relations: true,
71+
};
72+
6873
// Expected File Name
6974
const defaultExpectedTestModel = path.join(
7075
sandbox.path,
@@ -82,6 +87,10 @@ const defaultExpectedNamingModel = path.join(
8287
sandbox.path,
8388
'src/models/naming.model.ts',
8489
);
90+
const AppointmentModel = path.join(
91+
sandbox.path,
92+
'src/models/appointment.model.ts',
93+
);
8594

8695
const defaultExpectedIndexFile = path.join(sandbox.path, 'src/models/index.ts');
8796
const movedExpectedTestModel = path.join(sandbox.path, 'src/test.model.ts');
@@ -197,6 +206,18 @@ describe('lb4 discover integration', () => {
197206
assert.file(defaultExpectedTestModel);
198207
expectFileToMatchSnapshot(defaultExpectedTestModel);
199208
});
209+
it('generate relations with --relations', async () => {
210+
await testUtils
211+
.executeGenerator(generator)
212+
.inDir(sandbox.path, () =>
213+
testUtils.givenLBProject(sandbox.path, {
214+
additionalFiles: SANDBOX_FILES,
215+
}),
216+
)
217+
.withOptions(relationsSetTrue);
218+
assert.file(AppointmentModel);
219+
expectFileToMatchSnapshot(AppointmentModel);
220+
});
200221
});
201222
it('generates specific models without prompts using --models', async () => {
202223
await testUtils

0 commit comments

Comments
 (0)