Skip to content

Commit 5035c63

Browse files
bajtosyanamg7
andcommitted
feat(cli): improve scaffolding of complex model settings
Leverage stringify-object package to convert model settings object into well-formatted TypeScript source code. An example of a simple settings object: @model({settings: {strict: false}}) export class MyModel extends Entity { // ... } A more complex example: @model({ settings: { annotations: [{destinationClass: 'class1', argument: 0}], foreignKeys: {fk_destination: {name: 'fk_destination'}}, strict: false } }) export class MyModel extends Entity { // ... } Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com> Co-Authored-By: yanamg7 <Y.Agapeyev@F5.com>
1 parent 76d1177 commit 5035c63

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

packages/cli/generators/model/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const inspect = require('util').inspect;
1414
const utils = require('../../lib/utils');
1515
const chalk = require('chalk');
1616
const path = require('path');
17+
const stringifyObject = require('stringify-object');
1718

1819
const PROMPT_BASE_MODEL_CLASS = 'Please select the model base class';
1920
const ERROR_NO_MODELS_FOUND = 'Model was not found in';
@@ -513,6 +514,17 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
513514
}
514515
});
515516

517+
if (this.artifactInfo.modelSettings) {
518+
this.artifactInfo.modelSettings = stringifyObject(
519+
{settings: this.artifactInfo.modelSettings},
520+
{
521+
indent: ' ', // two spaces
522+
singleQuotes: true,
523+
inlineCharacterLimit: 80,
524+
},
525+
);
526+
}
527+
516528
this.copyTemplatedFiles(
517529
this.templatePath(MODEL_TEMPLATE_PATH),
518530
tsPath,

packages/cli/generators/model/templates/model.ts.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {model, property} from '@loopback/repository';
55
import {<%= modelBaseClass %>} from '.';
66
<% } -%>
77

8-
<% if(Object.keys(modelSettings).length > 0) { -%>
9-
@model({settings: <%- JSON.stringify(modelSettings) %>})
8+
<% if (modelSettings) { -%>
9+
@model(<%- modelSettings %>)
1010
<% } else { -%>
1111
@model()
1212
<% } -%>

packages/cli/package-lock.json

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

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"pluralize": "^7.0.0",
5555
"regenerate": "^1.3.3",
5656
"semver": "^6.0.0",
57+
"stringify-object": "^3.3.0",
5758
"swagger-parser": "^6.0.5",
5859
"swagger2openapi": "^5.1.0",
5960
"typescript": "^3.1.1",

packages/cli/test/integration/generators/model.integration.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('lb4 model integration', () => {
192192
);
193193
assert.fileContent(
194194
expectedModelFile,
195-
/@model\({settings: {"strict":false}}\)/,
195+
/@model\({settings: {strict: false}}\)/,
196196
);
197197
assert.fileContent(
198198
expectedModelFile,
@@ -237,4 +237,28 @@ describe('model generator using --config option', () => {
237237
]),
238238
).to.be.rejectedWith(/Model was not found in/);
239239
});
240+
241+
describe('model generator using --config option with model settings', () => {
242+
it('creates a model with valid settings', async () => {
243+
await testUtils
244+
.executeGenerator(generator)
245+
.inDir(SANDBOX_PATH, () => testUtils.givenLBProject(SANDBOX_PATH))
246+
.withArguments([
247+
'--config',
248+
'{"name":"test", "base":"Entity", \
249+
"modelSettings": {"annotations": \
250+
[{"destinationClass": "class1","argument": 0}],\
251+
"foreignKeys": {"fk_destination": {"name": "fk_destination"}}},\
252+
"allowAdditionalProperties":true}',
253+
'--yes',
254+
]);
255+
256+
basicModelFileChecks(expectedModelFile, expectedIndexFile);
257+
258+
assert.fileContent(
259+
expectedModelFile,
260+
/@model\({\n settings: {\n annotations: \[{destinationClass: 'class1', argument: 0}],\n foreignKeys: {fk_destination: {name: 'fk_destination'}},\n strict: false\n }\n}\)/,
261+
);
262+
});
263+
});
240264
});

0 commit comments

Comments
 (0)