Skip to content

Commit

Permalink
fix: change dataSource to datasource for consistency
Browse files Browse the repository at this point in the history
Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>
  • Loading branch information
warisniz02 committed Sep 1, 2024
1 parent ec7c6a0 commit 4657666
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/site/Discovering-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Models can be discovered from a supported datasource by running the

### Options

`--dataSource`: Put a valid datasource name here to skip the datasource prompt
`--dataSource or --datasource`: Put a valid datasource name here to skip the
datasource prompt

`--views`: Choose whether to discover views. Default is true

Expand Down
6 changes: 6 additions & 0 deletions packages/cli/.yo-rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,12 @@
"name": "dataSource",
"hide": false
},
"datasource": {
"type": "String",
"description": "The name of the datasource to discover",
"name": "datasource",
"hide": false
},
"views": {
"type": "Boolean",
"description": "Boolean to discover views",
Expand Down
19 changes: 13 additions & 6 deletions packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
description: g.f('The name of the datasource to discover'),
});

this.option('datasource', {
type: String,
description: g.f('The name of the datasource to discover'),
});

this.option('views', {
type: Boolean,
description: g.f('Boolean to discover views'),
Expand Down Expand Up @@ -88,10 +93,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
*/
setOptions() {
/* istanbul ignore next */
if (this.options.dataSource) {
debug(`Data source specified: ${this.options.dataSource}`);
if (this.options.dataSource || this.options.datasource) {
debug(
`Data source specified: ${this.options.dataSource || this.options.datasource}`,
);
this.artifactInfo.dataSource = modelMaker.loadDataSourceByName(
this.options.dataSource,
this.options.dataSource || this.options.datasource,
);
}
// remove not needed .env property
Expand Down Expand Up @@ -132,15 +139,15 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
path.resolve(dsDir, `${utils.toFileName(s)}.datasource.js`),
),
);
if (this.options.dataSource) {
if (this.options.dataSource || this.options.datasource) {
if (
this.dataSourceChoices
.map(d => d.name)
.includes(this.options.dataSource)
.includes(this.options.dataSource || this.options.datasource)
) {
Object.assign(this.artifactInfo, {
dataSource: this.dataSourceChoices.find(
d => d.name === this.options.dataSource,
d => d.name === this.options.dataSource || this.options.datasource,
),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,12 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"name": "dataSource",
"hide": false
},
"datasource": {
"type": "String",
"description": "The name of the datasource to discover",
"name": "datasource",
"hide": false
},
"views": {
"type": "Boolean",
"description": "Boolean to discover views",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,56 @@

'use strict';

exports[`lb4 discover integration generates all models without prompts using --all --datasource 1`] = `
import {Entity, model, property} from '@loopback/repository';
@model()
export class Schema extends Entity {
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<Schema>) {
super(data);
}
}
export interface SchemaRelations {
// describe navigational properties here
}
export type SchemaWithRelations = Schema & SchemaRelations;
`;


exports[`lb4 discover integration generates all models without prompts using --all --datasource 2`] = `
import {Entity, model, property} from '@loopback/repository';
@model()
export class View extends Entity {
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<View>) {
super(data);
}
}
export interface ViewRelations {
// describe navigational properties here
}
export type ViewWithRelations = View & ViewRelations;
`;


exports[`lb4 discover integration model discovery does not mark id property as required based on optionalId option 1`] = `
import {Entity, model, property} from '@loopback/repository';
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/test/integration/generators/discover.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ const specificModelsOptions = {
views: false,
disableCamelCase: true,
};

const baseOptionsWithSmallS = {
all: true,
datasource: 'mem',
};

// Expected File Name
const defaultExpectedTestModel = path.join(
sandbox.path,
Expand Down Expand Up @@ -231,4 +237,20 @@ describe('lb4 discover integration', () => {
basicModelFileChecks(defaultExpectedTestModel, defaultExpectedIndexFile);
assert.file(defaultExpectedTestModel);
});

it('generates all models without prompts using --all --datasource', /** @this {Mocha.Context} */ async function () {
this.timeout(10000);
await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {
additionalFiles: SANDBOX_FILES,
}),
)
.withOptions(baseOptionsWithSmallS);

basicModelFileChecks(defaultExpectedTestModel, defaultExpectedIndexFile);
expectFileToMatchSnapshot(defaultExpectedSchemaModel);
expectFileToMatchSnapshot(defaultExpectedViewModel);
});
});

0 comments on commit 4657666

Please sign in to comment.