Skip to content

Commit 497b21d

Browse files
awaissaeedforenaxachrinza
authored andcommitted
feat(cli): add option to mark id field as not required in discover command
Signed-off-by: Awais Saeed <[email protected]>
1 parent 6ae1423 commit 497b21d

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

docs/site/Discovering-models.md

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

50+
`--optionalId`: Specify if the Id property of generated models will be marked as
51+
not required
52+
5053
### Interactive Prompts
5154

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

packages/cli/.yo-rc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,13 @@
12701270
"description": "Specify the directory into which the `model.model.ts` files will be placed",
12711271
"name": "outDir",
12721272
"hide": false
1273+
},
1274+
"optionalId": {
1275+
"type": "Boolean",
1276+
"description": "Boolean to mark id property as optional field",
1277+
"default": false,
1278+
"name": "optionalId",
1279+
"hide": false
12731280
}
12741281
},
12751282
"arguments": [

packages/cli/generators/discover/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
5050
),
5151
default: undefined,
5252
});
53+
54+
this.option('optionalId', {
55+
type: Boolean,
56+
description: g.f('Boolean to mark id property as optional field'),
57+
default: false,
58+
});
5359
}
5460

5561
_setupGenerator() {
@@ -289,6 +295,18 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
289295
);
290296
debug(`Discovered: ${modelInfo.name}`);
291297
}
298+
299+
if (this.options.optionalId) {
300+
// Find id property
301+
const idProperty = Object.entries(
302+
this.artifactInfo.modelDefinitions[0].properties,
303+
).find(x => x[1].id === 1)[0];
304+
305+
// Mark as not required
306+
this.artifactInfo.modelDefinitions[0].properties[
307+
idProperty
308+
].required = false;
309+
}
292310
}
293311

294312
/**

packages/cli/snapshots/integration/cli/cli.integration.snapshots.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,13 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
13281328
"description": "Specify the directory into which the \`model.model.ts\` files will be placed",
13291329
"name": "outDir",
13301330
"hide": false
1331+
},
1332+
"optionalId": {
1333+
"type": "Boolean",
1334+
"description": "Boolean to mark id property as optional field",
1335+
"default": false,
1336+
"name": "optionalId",
1337+
"hide": false
13311338
}
13321339
},
13331340
"arguments": [

packages/cli/snapshots/integration/generators/discover.integration.snapshots.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,58 @@
77

88
'use strict';
99

10+
exports[`lb4 discover integration model discovery does not mark id property as required based on optionalId option 1`] = `
11+
import {Entity, model, property} from '@loopback/repository';
12+
13+
@model()
14+
export class Test extends Entity {
15+
@property({
16+
type: 'date',
17+
})
18+
dateTest?: string;
19+
20+
@property({
21+
type: 'number',
22+
})
23+
numberTest?: number;
24+
25+
@property({
26+
type: 'string',
27+
})
28+
stringTest?: string;
29+
30+
@property({
31+
type: 'boolean',
32+
})
33+
booleanText?: boolean;
34+
35+
@property({
36+
type: 'number',
37+
scale: 0,
38+
id: 1,
39+
})
40+
id?: number;
41+
42+
// Define well-known properties here
43+
44+
// Indexer property to allow additional data
45+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46+
[prop: string]: any;
47+
48+
constructor(data?: Partial<Test>) {
49+
super(data);
50+
}
51+
}
52+
53+
export interface TestRelations {
54+
// describe navigational properties here
55+
}
56+
57+
export type TestWithRelations = Test & TestRelations;
58+
59+
`;
60+
61+
1062
exports[`lb4 discover integration model discovery generates all models without prompts using --all --dataSource 1`] = `
1163
import {Entity, model, property} from '@loopback/repository';
1264

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const disableCamelCaseOptions = {
5050
const missingDataSourceOptions = {
5151
dataSource: 'foo',
5252
};
53+
const optionalIdOptions = {
54+
...baseOptions,
55+
optionalId: true,
56+
};
5357

5458
// Expected File Name
5559
const defaultExpectedTestModel = path.join(
@@ -155,5 +159,19 @@ describe('lb4 discover integration', () => {
155159
.withOptions(missingDataSourceOptions),
156160
).to.be.rejectedWith(/Cannot find datasource/);
157161
});
162+
163+
it('does not mark id property as required based on optionalId option', async () => {
164+
await testUtils
165+
.executeGenerator(generator)
166+
.inDir(sandbox.path, () =>
167+
testUtils.givenLBProject(sandbox.path, {
168+
additionalFiles: SANDBOX_FILES,
169+
}),
170+
)
171+
.withOptions(optionalIdOptions);
172+
173+
assert.file(defaultExpectedTestModel);
174+
expectFileToMatchSnapshot(defaultExpectedTestModel);
175+
});
158176
});
159177
});

0 commit comments

Comments
 (0)