Skip to content

Commit

Permalink
fix: add missing keys from config passed to lb4 relation
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <aaqilniz@yahoo.com>
  • Loading branch information
aaqilniz authored and dhmlau committed Apr 24, 2023
1 parent 1d5b9e1 commit d1ddfc2
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/cli/generators/relation/index.js
Expand Up @@ -614,6 +614,9 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
}

async _promptKeyFromOnThroughModel() {
if (this.options.sourceKeyOnThrough) {
this.artifactInfo.sourceKeyOnThrough = this.options.sourceKeyOnThrough;
}
if (this.shouldExit()) return false;
return this.prompt([
{
Expand All @@ -625,7 +628,7 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
)} to define on the through model`,
),
default: this.artifactInfo.defaultSourceKeyOnThrough,
when: !this.options.sourceKeyOnThrough,
when: !this.artifactInfo.sourceKeyOnThrough,
validate: utils.validateKeyName,
},
]).then(props => {
Expand All @@ -637,6 +640,9 @@ module.exports = class RelationGenerator extends ArtifactGenerator {

async _promptKeyToOnThroughModel() {
if (this.shouldExit()) return false;
if (this.options.targetKeyOnThrough) {
this.artifactInfo.targetKeyOnThrough = this.options.targetKeyOnThrough;
}
return this.prompt([
{
type: 'string',
Expand All @@ -647,7 +653,7 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
)} to define on the through model`,
),
default: this.artifactInfo.defaultTargetKeyOnThrough,
when: !this.options.targetKeyOnThrough,
when: !this.artifactInfo.targetKeyOnThrough,
validate: input =>
utils.validateKeyToKeyFrom(
input,
Expand Down
Expand Up @@ -487,6 +487,71 @@ export class Appointment extends Entity {
`;


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`] = `
import {Entity, model, property, hasMany} from '@loopback/repository';
import {Patient} from './patient.model';
import {Appointment} from './appointment.model';
@model()
export class Doctor extends Entity {
@property({
type: 'number',
id: true,
default: 0,
})
id?: number;
@property({
type: 'string',
})
name?: string;
@hasMany(() => Patient, {through: {model: () => Appointment, keyFrom: 'customKeyFrom', keyTo: 'customKeyTo'}})
patients: Patient[];
constructor(data?: Partial<Doctor>) {
super(data);
}
}
`;


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`] = `
import {Entity, model, property} from '@loopback/repository';
@model()
export class Appointment extends Entity {
@property({
type: 'number',
id: true,
default: 0,
})
id?: number;
@property({
type: 'string',
})
des?: string;
@property({
type: 'number',
})
customKeyFrom?: number;
@property({
type: 'number',
})
customKeyTo?: number;
constructor(data?: Partial<Appointment>) {
super(data);
}
}
`;


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`] = `
import {Entity, model, property, hasMany} from '@loopback/repository';
import {Patient} from './patient.model';
Expand Down
Expand Up @@ -213,6 +213,44 @@ describe('lb4 relation HasManyThrough', /** @this {Mocha.Suite} */ function () {
}
});

context(
'generates model relation with custom keyFrom and/or keyTo with --config',
() => {
before(async function runGeneratorWithAnswers() {
await sandbox.reset();
await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {
additionalFiles: SANDBOX_FILES,
}),
)
.withArguments([
'--config',
'{"sourceModel": "Doctor", "destinationModel": "Patient", "throughModel": "Appointment", "relationType": "hasManyThrough", "sourceKeyOnThrough": "customKeyFrom", "targetKeyOnThrough": "customKeyTo"}',
]);
});

it('add custom keyTo and/or keyFrom to the through model', async () => {
const sourceFilePath = path.join(
sandbox.path,
MODEL_APP_PATH,
sourceFileName,
);

const throughFilePath = path.join(
sandbox.path,
MODEL_APP_PATH,
throughFileName,
);
assert.file(sourceFilePath);
assert.file(throughFilePath);
expectFileToMatchSnapshot(sourceFilePath);
expectFileToMatchSnapshot(throughFilePath);
});
},
);

context('checks if the controller file is created ', () => {
const promptArray = [
{
Expand Down

0 comments on commit d1ddfc2

Please sign in to comment.