Skip to content

feat: create controller and repo while discovering when relations: true #10874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
}
this.artifactInfo.indexesToBeUpdated =
this.artifactInfo.indexesToBeUpdated || [];

const relations = [];
const repositoryConfigs = {
datasource: '',
repositories: new Set(),
repositoryBaseClass: 'DefaultCrudRepository',
};
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < this.artifactInfo.modelDefinitions.length; i++) {
const modelDefinition = this.artifactInfo.modelDefinitions[i];
Expand Down Expand Up @@ -391,23 +396,18 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
);
// If targetModel is not in discovered models, skip creating relation
if (targetModel) {
Object.assign(templateData.properties[relation.foreignKey], {
relation,
});
if (!relationImports.includes(relation.type)) {
relationImports.push(relation.type);
}
relationDestinationImports.push(relation.model);

foreignKeys[relationName] = {};
Object.assign(foreignKeys[relationName], {
name: relationName,
entity: relation.model,
entityKey: Object.entries(targetModel.properties).find(
x => x?.[1].id === 1,
)?.[0],
foreignKey: relation.foreignKey,
});
const configs = {};
configs['sourceModel'] = templateData.name;
configs['destinationModel'] = targetModel.name;
configs['foreignKeyName'] = relation.foreignKey;
configs['relationType'] = relation.type;
configs['registerInclusionResolver'] = true;
configs['yes'] = true;
relations.push(configs);
repositoryConfigs['datasource'] =
this.options.datasource || this.options.dataSource;
repositoryConfigs.repositories.add(templateData.name);
repositoryConfigs.repositories.add(targetModel.name);
}
}
// remove model import if the model relation is with itself
Expand Down Expand Up @@ -462,6 +462,8 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
// This part at the end is just for the ArtifactGenerator
// end message to output something nice, before it was "Discover undefined was created in src/models/"
this.artifactInfo.type = 'Models';
this.artifactInfo.relationConfigs = relations;
this.artifactInfo.repositoryConfigs = repositoryConfigs;
this.artifactInfo.name = this.artifactInfo.modelDefinitions
.map(d => d.name)
.join(',');
Expand Down
12 changes: 11 additions & 1 deletion packages/cli/generators/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,20 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
])
.then(setting => {
Object.assign(this.artifactInfo, setting);

if (this.artifactInfo.allowAdditionalProperties) {
Object.assign(this.artifactInfo.modelSettings, {strict: false});
}

const dsType = this.artifactInfo.dataSourceType;
if (!this.artifactInfo.modelSettings)
this.artifactInfo.modelSettings = {};
if (dsType === 'relational') {
this.artifactInfo.allowAdditionalProperties = false;
this.artifactInfo.modelSettings.strict = true;
} else {
this.artifactInfo.allowAdditionalProperties = true;
this.artifactInfo.modelSettings.strict = false;
}
// inform user what model/file names will be created
super.promptClassFileName(
'model',
Expand Down
48 changes: 47 additions & 1 deletion packages/cli/lib/artifact-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,53 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
.split(this.classNameSeparator)
.map(utils.toClassName);
const classesOutput = classes.join(this.classNameSeparator);

if (
this.artifactInfo.repositoryConfigs &&
this.artifactInfo.repositoryConfigs.repositories.size
) {
const {repositories, datasource, repositoryBaseClass} =
this.artifactInfo.repositoryConfigs;
for (const model of repositories) {
const config = {repositoryBaseClass, datasource, model, name: model};
try {
const {execSync} = require('child_process');
const cmd =
"lb4 repository --config='" + JSON.stringify(config) + "' --yes";
execSync(cmd, {
cwd: process.cwd(),
stdio: ['ignore', 'pipe', 'pipe'],
encoding: 'utf8',
});
} catch (error) {
console.log(error);
}
}
} else {
debug(
'No repository configurations found, skipping repository generation',
);
}
if (
this.artifactInfo.relationConfigs &&
this.artifactInfo.relationConfigs.length
) {
for (const configs of this.artifactInfo.relationConfigs) {
try {
const {execSync} = require('child_process');
const cmd =
"lb4 relation --config='" + JSON.stringify(configs) + "' --yes";
execSync(cmd, {
cwd: process.cwd(),
stdio: ['ignore', 'pipe', 'pipe'],
encoding: 'utf8',
});
} catch (error) {
console.log(error);
}
}
} else {
debug('No relation configurations found, skipping relation generation');
}
// User Output
this.log();
this.log(
Expand Down
Loading