Skip to content

Commit d1ddfc2

Browse files
aaqilnizdhmlau
authored andcommitted
fix: add missing keys from config passed to lb4 relation
Signed-off-by: Muhammad Aaqil <aaqilniz@yahoo.com>
1 parent 1d5b9e1 commit d1ddfc2

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

packages/cli/generators/relation/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
614614
}
615615

616616
async _promptKeyFromOnThroughModel() {
617+
if (this.options.sourceKeyOnThrough) {
618+
this.artifactInfo.sourceKeyOnThrough = this.options.sourceKeyOnThrough;
619+
}
617620
if (this.shouldExit()) return false;
618621
return this.prompt([
619622
{
@@ -625,7 +628,7 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
625628
)} to define on the through model`,
626629
),
627630
default: this.artifactInfo.defaultSourceKeyOnThrough,
628-
when: !this.options.sourceKeyOnThrough,
631+
when: !this.artifactInfo.sourceKeyOnThrough,
629632
validate: utils.validateKeyName,
630633
},
631634
]).then(props => {
@@ -637,6 +640,9 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
637640

638641
async _promptKeyToOnThroughModel() {
639642
if (this.shouldExit()) return false;
643+
if (this.options.targetKeyOnThrough) {
644+
this.artifactInfo.targetKeyOnThrough = this.options.targetKeyOnThrough;
645+
}
640646
return this.prompt([
641647
{
642648
type: 'string',
@@ -647,7 +653,7 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
647653
)} to define on the through model`,
648654
),
649655
default: this.artifactInfo.defaultTargetKeyOnThrough,
650-
when: !this.options.targetKeyOnThrough,
656+
when: !this.artifactInfo.targetKeyOnThrough,
651657
validate: input =>
652658
utils.validateKeyToKeyFrom(
653659
input,

packages/cli/snapshots/integration/generators/relation.has-many-through.integration.snapshots.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,71 @@ export class Appointment extends Entity {
487487
`;
488488

489489

490+
exports[`lb4 relation HasManyThrough generates model relation with custom keyFrom and/or keyTo with --config add custom keyTo and/or keyFrom to the through model 1`] = `
491+
import {Entity, model, property, hasMany} from '@loopback/repository';
492+
import {Patient} from './patient.model';
493+
import {Appointment} from './appointment.model';
494+
495+
@model()
496+
export class Doctor extends Entity {
497+
@property({
498+
type: 'number',
499+
id: true,
500+
default: 0,
501+
})
502+
id?: number;
503+
504+
@property({
505+
type: 'string',
506+
})
507+
name?: string;
508+
509+
@hasMany(() => Patient, {through: {model: () => Appointment, keyFrom: 'customKeyFrom', keyTo: 'customKeyTo'}})
510+
patients: Patient[];
511+
512+
constructor(data?: Partial<Doctor>) {
513+
super(data);
514+
}
515+
}
516+
517+
`;
518+
519+
520+
exports[`lb4 relation HasManyThrough generates model relation with custom keyFrom and/or keyTo with --config add custom keyTo and/or keyFrom to the through model 2`] = `
521+
import {Entity, model, property} from '@loopback/repository';
522+
523+
@model()
524+
export class Appointment extends Entity {
525+
@property({
526+
type: 'number',
527+
id: true,
528+
default: 0,
529+
})
530+
id?: number;
531+
532+
@property({
533+
type: 'string',
534+
})
535+
des?: string;
536+
537+
@property({
538+
type: 'number',
539+
})
540+
customKeyFrom?: number;
541+
542+
@property({
543+
type: 'number',
544+
})
545+
customKeyTo?: number;
546+
547+
constructor(data?: Partial<Appointment>) {
548+
super(data);
549+
}
550+
}
551+
552+
`;
553+
554+
490555
exports[`lb4 relation HasManyThrough generates model relation with custom relation name answers {"relationType":"hasManyThrough","sourceModel":"Doctor","destinationModel":"Patient","throughModel":"Appointment","relationName":"myPatients"} relation name should be myPatients 1`] = `
491556
import {Entity, model, property, hasMany} from '@loopback/repository';
492557
import {Patient} from './patient.model';

packages/cli/test/integration/generators/relation.has-many-through.integration.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,44 @@ describe('lb4 relation HasManyThrough', /** @this {Mocha.Suite} */ function () {
213213
}
214214
});
215215

216+
context(
217+
'generates model relation with custom keyFrom and/or keyTo with --config',
218+
() => {
219+
before(async function runGeneratorWithAnswers() {
220+
await sandbox.reset();
221+
await testUtils
222+
.executeGenerator(generator)
223+
.inDir(sandbox.path, () =>
224+
testUtils.givenLBProject(sandbox.path, {
225+
additionalFiles: SANDBOX_FILES,
226+
}),
227+
)
228+
.withArguments([
229+
'--config',
230+
'{"sourceModel": "Doctor", "destinationModel": "Patient", "throughModel": "Appointment", "relationType": "hasManyThrough", "sourceKeyOnThrough": "customKeyFrom", "targetKeyOnThrough": "customKeyTo"}',
231+
]);
232+
});
233+
234+
it('add custom keyTo and/or keyFrom to the through model', async () => {
235+
const sourceFilePath = path.join(
236+
sandbox.path,
237+
MODEL_APP_PATH,
238+
sourceFileName,
239+
);
240+
241+
const throughFilePath = path.join(
242+
sandbox.path,
243+
MODEL_APP_PATH,
244+
throughFileName,
245+
);
246+
assert.file(sourceFilePath);
247+
assert.file(throughFilePath);
248+
expectFileToMatchSnapshot(sourceFilePath);
249+
expectFileToMatchSnapshot(throughFilePath);
250+
});
251+
},
252+
);
253+
216254
context('checks if the controller file is created ', () => {
217255
const promptArray = [
218256
{

0 commit comments

Comments
 (0)