diff --git a/docs/site/Discovering-models.md b/docs/site/Discovering-models.md index a5064cb6a61d..9fd0b009a49e 100644 --- a/docs/site/Discovering-models.md +++ b/docs/site/Discovering-models.md @@ -49,7 +49,10 @@ placed. Default is `src/models` `--schema`: Specify the schema which the datasource will find the models to discover +**`--models`**: Specify the models to be generated e.g:--models=table1,table2 + `--optionalId`: Specify if the Id property of generated models will be marked as + not required ### Interactive Prompts diff --git a/packages/cli/.yo-rc.json b/packages/cli/.yo-rc.json index 36ea32e39dc9..25ebb973f6b5 100644 --- a/packages/cli/.yo-rc.json +++ b/packages/cli/.yo-rc.json @@ -1278,6 +1278,12 @@ "name": "outDir", "hide": false }, + "models": { + "type": "String", + "description": "Discover specific models without prompting users to select e.g:--models=table1,table2", + "name": "models", + "hide": false + }, "optionalId": { "type": "Boolean", "description": "Boolean to mark id property as optional field", diff --git a/packages/cli/generators/discover/index.js b/packages/cli/generators/discover/index.js index 3f3d5cc3fe7e..891b797f2f7e 100644 --- a/packages/cli/generators/discover/index.js +++ b/packages/cli/generators/discover/index.js @@ -57,6 +57,14 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator { default: undefined, }); + this.option('models', { + type: String, + description: g.f( + 'Discover specific models without prompting users to select e.g:--models=table1,table2', + ), + default: undefined, + }); + this.option('optionalId', { type: Boolean, description: g.f('Boolean to mark id property as optional field'), @@ -201,6 +209,16 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator { this.discoveringModels = this.modelChoices; } + if (this.options.models) { + const answers = {discoveringModels: this.options.models.split(',')}; + debug(`Models specified: ${JSON.stringify(answers)}`); + this.discoveringModels = []; + answers.discoveringModels.forEach(m => { + this.discoveringModels.push(this.modelChoices.find(c => c.name === m)); + }); + return; + } + const prompts = [ { name: 'discoveringModels', diff --git a/packages/cli/snapshots/integration/cli/cli.integration.snapshots.js b/packages/cli/snapshots/integration/cli/cli.integration.snapshots.js index 902b96b7db2b..b0ed39183e17 100644 --- a/packages/cli/snapshots/integration/cli/cli.integration.snapshots.js +++ b/packages/cli/snapshots/integration/cli/cli.integration.snapshots.js @@ -1336,6 +1336,12 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = ` "name": "outDir", "hide": false }, + "models": { + "type": "String", + "description": "Discover specific models without prompting users to select e.g:--models=table1,table2", + "name": "models", + "hide": false + }, "optionalId": { "type": "Boolean", "description": "Boolean to mark id property as optional field", diff --git a/packages/cli/test/integration/generators/discover.integration.js b/packages/cli/test/integration/generators/discover.integration.js index 3a23e02826ee..77dffdc3b09f 100644 --- a/packages/cli/test/integration/generators/discover.integration.js +++ b/packages/cli/test/integration/generators/discover.integration.js @@ -50,6 +50,12 @@ const disableCamelCaseOptions = { const missingDataSourceOptions = { dataSource: 'foo', }; +const specificmodelsOptions = { + models: 'Test', + dataSource: 'mem', + views: false, + disableCamelCase: true, +}; const optionalIdOptions = { ...baseOptions, optionalId: true, @@ -174,4 +180,17 @@ describe('lb4 discover integration', () => { expectFileToMatchSnapshot(defaultExpectedTestModel); }); }); + it('generates specific models without prompts using --models', async () => { + await testUtils + .executeGenerator(generator) + .inDir(sandbox.path, () => + testUtils.givenLBProject(sandbox.path, { + additionalFiles: SANDBOX_FILES, + }), + ) + .withOptions(specificmodelsOptions); + + basicModelFileChecks(defaultExpectedTestModel, defaultExpectedIndexFile); + assert.file(defaultExpectedTestModel); + }); });