Skip to content

Commit 68e0161

Browse files
Merge branch 'master' into discover-relations
2 parents d37b161 + 1a51f32 commit 68e0161

File tree

21 files changed

+295
-73
lines changed

21 files changed

+295
-73
lines changed

docs/site/Discovering-models.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ placed. Default is `src/models`
4949
`--schema`: Specify the schema which the datasource will find the models to
5050
discover
5151

52+
`--optionalId`: Specify if the Id property of generated models will be marked as
53+
not required
54+
5255
### Interactive Prompts
5356

5457
Based on the option, the tool may prompt you for:

docs/site/Rest-Crud-generator.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ src/datasources
2727

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

30+
`--readonly` : _(Optional)_ create readonly APIs e.g find and count
31+
3032
### Configuration file
3133

3234
This generator supports a config file with the following format, see the
@@ -37,7 +39,8 @@ file.
3739
{
3840
"datasource": "validDataSourceName",
3941
"model": "validModelName",
40-
"basePath": "/<base-path>"
42+
"basePath": "/<base-path>",
43+
"readonly": "<boolean>"
4144
}
4245
```
4346

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

80+
- **Create readonly APIs.** _(readonly)_ If readonly had been supplied from the
81+
command line with `--readonly` option, the prompt is skipped.
82+
7783
### Output
7884

7985
Once all the prompts have been answered, the CLI will do the following for each

examples/socketio/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/socketio/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@types/debug": "^4.1.7",
6868
"@types/node": "^14.18.18",
6969
"eslint": "^8.15.0",
70-
"socket.io-client": "^4.5.0",
70+
"socket.io-client": "^4.5.1",
7171
"source-map-support": "^0.5.21",
7272
"typescript": "~4.6.4"
7373
}

extensions/socketio/package-lock.json

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/socketio/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"cors": "^2.8.5",
5353
"debug": "^4.3.4",
5454
"lodash": "^4.17.21",
55-
"socket.io": "^4.5.0"
55+
"socket.io": "^4.5.1"
5656
},
5757
"devDependencies": {
5858
"@loopback/boot": "^5.0.0",
@@ -62,6 +62,6 @@
6262
"@loopback/testlab": "^5.0.0",
6363
"@types/debug": "^4.1.7",
6464
"p-event": "^4.2.0",
65-
"socket.io-client": "^4.5.0"
65+
"socket.io-client": "^4.5.1"
6666
}
6767
}

packages/cli/.yo-rc.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,13 @@
12771277
"description": "Specify the directory into which the `model.model.ts` files will be placed",
12781278
"name": "outDir",
12791279
"hide": false
1280+
},
1281+
"optionalId": {
1282+
"type": "Boolean",
1283+
"description": "Boolean to mark id property as optional field",
1284+
"default": false,
1285+
"name": "optionalId",
1286+
"hide": false
12801287
}
12811288
},
12821289
"arguments": [
@@ -1584,6 +1591,14 @@
15841591
"name": "basePath",
15851592
"hide": false
15861593
},
1594+
"readonly": {
1595+
"type": "Boolean",
1596+
"required": false,
1597+
"description": "Create readonly APIs",
1598+
"default": false,
1599+
"name": "readonly",
1600+
"hide": false
1601+
},
15871602
"config": {
15881603
"type": "String",
15891604
"alias": "c",

packages/cli/generators/discover/index.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
5656
),
5757
default: undefined,
5858
});
59+
60+
this.option('optionalId', {
61+
type: Boolean,
62+
description: g.f('Boolean to mark id property as optional field'),
63+
default: false,
64+
});
5965
}
6066

6167
_setupGenerator() {
@@ -283,17 +289,26 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
283289
for (let i = 0; i < this.discoveringModels.length; i++) {
284290
const modelInfo = this.discoveringModels[i];
285291
debug(`Discovering: ${modelInfo.name}...`);
286-
this.artifactInfo.modelDefinitions.push(
287-
await modelMaker.discoverSingleModel(
288-
this.artifactInfo.dataSource,
289-
modelInfo.name,
290-
{
291-
schema: modelInfo.owner,
292-
disableCamelCase: this.artifactInfo.disableCamelCase,
293-
associations: this.options.relations,
294-
},
295-
),
292+
const modelDefinition = await modelMaker.discoverSingleModel(
293+
this.artifactInfo.dataSource,
294+
modelInfo.name,
295+
{
296+
schema: modelInfo.owner,
297+
disableCamelCase: this.artifactInfo.disableCamelCase,
298+
associations: this.options.relations,
299+
},
296300
);
301+
if (this.options.optionalId) {
302+
// Find id properties (can be multiple ids if using composite key)
303+
const idProperties = Object.values(modelDefinition.properties).filter(
304+
property => property.id,
305+
);
306+
// Mark as not required
307+
idProperties.forEach(property => {
308+
property.required = false;
309+
});
310+
}
311+
this.artifactInfo.modelDefinitions.push(modelDefinition);
297312
debug(`Discovered: ${modelInfo.name}`);
298313
}
299314
}

packages/cli/generators/rest-crud/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const PROMPT_MESSAGE_MODEL = g.f(
2626
);
2727
const PROMPT_MESSAGE_DATA_SOURCE = g.f('Please select the datasource');
2828
const PROMPT_MESSAGE_BASE_PATH = g.f('Please specify the base path');
29+
const PROMPT_MESSAGE_READONLY = g.f('Do you want to create readonly APIs?');
2930

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

97+
this.option('readonly', {
98+
type: Boolean,
99+
required: false,
100+
description: g.f('Create readonly APIs'),
101+
default: false,
102+
});
103+
96104
return super._setupGenerator();
97105
}
98106

@@ -308,6 +316,20 @@ module.exports = class RestCrudGenerator extends ArtifactGenerator {
308316
}
309317
}
310318

319+
async promptReadonly() {
320+
const props = await this.prompt([
321+
{
322+
type: 'confirm',
323+
name: 'readonly',
324+
message: PROMPT_MESSAGE_READONLY,
325+
default: false,
326+
},
327+
]);
328+
if (props) {
329+
this.artifactInfo.readonly = props.readonly;
330+
}
331+
}
332+
311333
async scaffold() {
312334
if (this.shouldExit()) return false;
313335

packages/cli/generators/rest-crud/templates/src/model-endpoints/model.rest-config-template.ts.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ const config: ModelCrudRestApiConfig = {
66
pattern: 'CrudRest',
77
dataSource: '<%= dataSourceName %>',
88
basePath: '<%= basePath %>',
9+
readonly: <%= readonly %>,
910
};
1011
module.exports = config;

0 commit comments

Comments
 (0)