From 73792a90a411a2dca9f0fddede0bbb90890ff699 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Mon, 7 Apr 2025 08:39:17 +0500 Subject: [PATCH 1/2] feat: create controller and repo while discovering when relations: true Signed-off-by: Muhammad Aaqil --- packages/cli/generators/discover/index.js | 38 +++++++++--------- packages/cli/lib/artifact-generator.js | 48 ++++++++++++++++++++++- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/packages/cli/generators/discover/index.js b/packages/cli/generators/discover/index.js index 2f0b19492af1..7a8f3c00ca9a 100644 --- a/packages/cli/generators/discover/index.js +++ b/packages/cli/generators/discover/index.js @@ -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]; @@ -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 @@ -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(','); diff --git a/packages/cli/lib/artifact-generator.js b/packages/cli/lib/artifact-generator.js index 337d14af1e52..17f28fea30aa 100644 --- a/packages/cli/lib/artifact-generator.js +++ b/packages/cli/lib/artifact-generator.js @@ -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( From 3cb4841b758639dac4d756532cbd7ec6de40b064 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Fri, 18 Apr 2025 20:35:42 +0500 Subject: [PATCH 2/2] fix: misc Signed-off-by: Muhammad Aaqil --- packages/cli/generators/model/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/cli/generators/model/index.js b/packages/cli/generators/model/index.js index bb57ce87cb8b..00f3fa08b246 100644 --- a/packages/cli/generators/model/index.js +++ b/packages/cli/generators/model/index.js @@ -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',