Skip to content

Commit

Permalink
Merge branch 'master' into discover-relations
Browse files Browse the repository at this point in the history
  • Loading branch information
awaissaeedforenax authored May 20, 2022
2 parents d37b161 + 1a51f32 commit 68e0161
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 73 deletions.
3 changes: 3 additions & 0 deletions docs/site/Discovering-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ placed. Default is `src/models`
`--schema`: Specify the schema which the datasource will find the models to
discover

`--optionalId`: Specify if the Id property of generated models will be marked as
not required

### Interactive Prompts

Based on the option, the tool may prompt you for:
Expand Down
8 changes: 7 additions & 1 deletion docs/site/Rest-Crud-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ src/datasources

`--basePath` : _(Optional)_ base path of the model endpoint

`--readonly` : _(Optional)_ create readonly APIs e.g find and count

### Configuration file

This generator supports a config file with the following format, see the
Expand All @@ -37,7 +39,8 @@ file.
{
"datasource": "validDataSourceName",
"model": "validModelName",
"basePath": "/<base-path>"
"basePath": "/<base-path>",
"readonly": "<boolean>"
}
```

Expand Down Expand Up @@ -74,6 +77,9 @@ The tool will prompt you for:
supplied from the command line with `--basePath` option or more than one
models are selected, the prompt is skipped.

- **Create readonly APIs.** _(readonly)_ If readonly had been supplied from the
command line with `--readonly` option, the prompt is skipped.

### Output

Once all the prompts have been answered, the CLI will do the following for each
Expand Down
14 changes: 7 additions & 7 deletions examples/socketio/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/socketio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@types/debug": "^4.1.7",
"@types/node": "^14.18.18",
"eslint": "^8.15.0",
"socket.io-client": "^4.5.0",
"socket.io-client": "^4.5.1",
"source-map-support": "^0.5.21",
"typescript": "~4.6.4"
}
Expand Down
28 changes: 14 additions & 14 deletions extensions/socketio/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions extensions/socketio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"cors": "^2.8.5",
"debug": "^4.3.4",
"lodash": "^4.17.21",
"socket.io": "^4.5.0"
"socket.io": "^4.5.1"
},
"devDependencies": {
"@loopback/boot": "^5.0.0",
Expand All @@ -62,6 +62,6 @@
"@loopback/testlab": "^5.0.0",
"@types/debug": "^4.1.7",
"p-event": "^4.2.0",
"socket.io-client": "^4.5.0"
"socket.io-client": "^4.5.1"
}
}
15 changes: 15 additions & 0 deletions packages/cli/.yo-rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,13 @@
"description": "Specify the directory into which the `model.model.ts` files will be placed",
"name": "outDir",
"hide": false
},
"optionalId": {
"type": "Boolean",
"description": "Boolean to mark id property as optional field",
"default": false,
"name": "optionalId",
"hide": false
}
},
"arguments": [
Expand Down Expand Up @@ -1584,6 +1591,14 @@
"name": "basePath",
"hide": false
},
"readonly": {
"type": "Boolean",
"required": false,
"description": "Create readonly APIs",
"default": false,
"name": "readonly",
"hide": false
},
"config": {
"type": "String",
"alias": "c",
Expand Down
35 changes: 25 additions & 10 deletions packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
),
default: undefined,
});

this.option('optionalId', {
type: Boolean,
description: g.f('Boolean to mark id property as optional field'),
default: false,
});
}

_setupGenerator() {
Expand Down Expand Up @@ -283,17 +289,26 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
for (let i = 0; i < this.discoveringModels.length; i++) {
const modelInfo = this.discoveringModels[i];
debug(`Discovering: ${modelInfo.name}...`);
this.artifactInfo.modelDefinitions.push(
await modelMaker.discoverSingleModel(
this.artifactInfo.dataSource,
modelInfo.name,
{
schema: modelInfo.owner,
disableCamelCase: this.artifactInfo.disableCamelCase,
associations: this.options.relations,
},
),
const modelDefinition = await modelMaker.discoverSingleModel(
this.artifactInfo.dataSource,
modelInfo.name,
{
schema: modelInfo.owner,
disableCamelCase: this.artifactInfo.disableCamelCase,
associations: this.options.relations,
},
);
if (this.options.optionalId) {
// Find id properties (can be multiple ids if using composite key)
const idProperties = Object.values(modelDefinition.properties).filter(
property => property.id,
);
// Mark as not required
idProperties.forEach(property => {
property.required = false;
});
}
this.artifactInfo.modelDefinitions.push(modelDefinition);
debug(`Discovered: ${modelInfo.name}`);
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/generators/rest-crud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const PROMPT_MESSAGE_MODEL = g.f(
);
const PROMPT_MESSAGE_DATA_SOURCE = g.f('Please select the datasource');
const PROMPT_MESSAGE_BASE_PATH = g.f('Please specify the base path');
const PROMPT_MESSAGE_READONLY = g.f('Do you want to create readonly APIs?');

const ERROR_NO_DATA_SOURCES_FOUND = g.f('No datasources found at');
const ERROR_NO_MODELS_FOUND = g.f('No models found at');
Expand Down Expand Up @@ -93,6 +94,13 @@ module.exports = class RestCrudGenerator extends ArtifactGenerator {
description: g.f('A valid base path'),
});

this.option('readonly', {
type: Boolean,
required: false,
description: g.f('Create readonly APIs'),
default: false,
});

return super._setupGenerator();
}

Expand Down Expand Up @@ -308,6 +316,20 @@ module.exports = class RestCrudGenerator extends ArtifactGenerator {
}
}

async promptReadonly() {
const props = await this.prompt([
{
type: 'confirm',
name: 'readonly',
message: PROMPT_MESSAGE_READONLY,
default: false,
},
]);
if (props) {
this.artifactInfo.readonly = props.readonly;
}
}

async scaffold() {
if (this.shouldExit()) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const config: ModelCrudRestApiConfig = {
pattern: 'CrudRest',
dataSource: '<%= dataSourceName %>',
basePath: '<%= basePath %>',
readonly: <%= readonly %>,
};
module.exports = config;
26 changes: 13 additions & 13 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"minimist": "^1.2.6",
"mkdirp": "^1.0.4",
"natural-compare": "^1.4.0",
"pacote": "^13.3.0",
"pacote": "^13.4.0",
"pluralize": "^8.0.0",
"regenerate": "^1.4.2",
"semver": "^7.3.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,13 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"description": "Specify the directory into which the \`model.model.ts\` files will be placed",
"name": "outDir",
"hide": false
},
"optionalId": {
"type": "Boolean",
"description": "Boolean to mark id property as optional field",
"default": false,
"name": "optionalId",
"hide": false
}
},
"arguments": [
Expand Down Expand Up @@ -1642,6 +1649,14 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"name": "basePath",
"hide": false
},
"readonly": {
"type": "Boolean",
"required": false,
"description": "Create readonly APIs",
"default": false,
"name": "readonly",
"hide": false
},
"config": {
"type": "String",
"alias": "c",
Expand Down
Loading

0 comments on commit 68e0161

Please sign in to comment.