Skip to content

Commit 6cd0ab1

Browse files
feat(cli): add option to create relations in cli discover command
Signed-off-by: Awais Saeed <[email protected]>
1 parent 1a51f32 commit 6cd0ab1

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

docs/site/Discovering-models.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Models can be discovered from a supported datasource by running the
3939

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

42+
`--relations`: Choose whether to create relations. Default is false
43+
4244
`--all`: Skips the model prompt and discovers all of them
4345

4446
`--outDir`: Specify the directory into which the `model.model.ts` files will be

packages/cli/.yo-rc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,13 @@
12511251
"name": "views",
12521252
"hide": false
12531253
},
1254+
"relations": {
1255+
"type": "Boolean",
1256+
"description": "Discover and create relations",
1257+
"default": false,
1258+
"name": "relations",
1259+
"hide": false
1260+
},
12541261
"schema": {
12551262
"type": "String",
12561263
"description": "Schema to discover",

packages/cli/generators/discover/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
3131
default: true,
3232
});
3333

34+
this.option('relations', {
35+
type: Boolean,
36+
description: g.f('Discover and create relations'),
37+
default: false,
38+
});
39+
3440
this.option('schema', {
3541
type: String,
3642
description: g.f('Schema to discover'),
@@ -289,6 +295,7 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
289295
{
290296
schema: modelInfo.owner,
291297
disableCamelCase: this.artifactInfo.disableCamelCase,
298+
associations: this.options.relations,
292299
},
293300
);
294301
if (this.options.optionalId) {
@@ -336,6 +343,46 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
336343
);
337344
debug(`Writing: ${fullPath}`);
338345

346+
if (this.options.relations) {
347+
const relationImports = [];
348+
const relationDestinationImports = [];
349+
const foreignKeys = {};
350+
for (const relationName in templateData.settings.relations) {
351+
const relation = templateData.settings.relations[relationName];
352+
const targetModel = this.artifactInfo.modelDefinitions.find(
353+
model => model.name === relation.model,
354+
);
355+
// If targetModel is not in discovered models, skip creating relation
356+
if (targetModel) {
357+
Object.assign(templateData.properties[relation.foreignKey], {
358+
relation,
359+
});
360+
relationImports.push(relation.type);
361+
relationDestinationImports.push(relation.model);
362+
363+
foreignKeys[relationName] = {};
364+
Object.assign(foreignKeys[relationName], {
365+
name: relationName,
366+
entity: relation.model,
367+
entityKey: Object.entries(targetModel.properties).find(
368+
x => x?.[1].id === 1,
369+
)?.[0],
370+
foreignKey: relation.foreignKey,
371+
});
372+
}
373+
}
374+
templateData.relationImports = relationImports;
375+
templateData.relationDestinationImports = relationDestinationImports;
376+
// Delete relation from modelSettings
377+
delete templateData.settings.relations;
378+
if (Object.keys(foreignKeys)?.length > 0) {
379+
Object.assign(templateData.settings, {foreignKeys});
380+
}
381+
templateData.modelSettings = utils.stringifyModelSettings(
382+
templateData.settings,
383+
);
384+
}
385+
339386
this.copyTemplatedFiles(
340387
modelDiscoverer.MODEL_TEMPLATE_PATH,
341388
fullPath,

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<% if (isModelBaseBuiltin) { -%>
2-
import {<%= modelBaseClass %>, model, property} from '@loopback/repository';
2+
import {<%= modelBaseClass %>, model, property<%if (locals.relationImports) {%><% relationImports.forEach((relation) => {-%>, <%= relation %> <%_})-%><%}%>} from '@loopback/repository';
33
<% } else { -%>
4-
import {model, property} from '@loopback/repository';
4+
import {model, property<%if (locals.relationImports) {%><% relationImports.forEach((relation) => {-%> , <%= relation %> <%_})-%><%}%>} from '@loopback/repository';
55
import {<%= modelBaseClass %>} from '.';
66
<% } -%>
7+
<%_ if (locals.relationDestinationImports && locals.relationDestinationImports.length > 0) { -%>
8+
import {<% relationDestinationImports.forEach((model, index) => {-%><%= model %><%if (index!==relationDestinationImports.length-1) {%>,<% } %> <%_}) -%>} from '.';
9+
<%_ } -%>
710

811
<% if (modelSettings) { -%>
912
@model(<%- modelSettings %>)
@@ -12,13 +15,17 @@ import {<%= modelBaseClass %>} from '.';
1215
<% } -%>
1316
export class <%= className %> extends <%= modelBaseClass %> {
1417
<% Object.entries(properties).forEach(([key, val]) => { -%>
18+
<% if (val.relation) { -%>
19+
@<%= val.relation.type %>(() => <%= val.relation.model %>)
20+
<% } else { -%>
1521
@property({
1622
<%_ Object.entries(val).forEach(([propKey, propVal]) => { -%>
17-
<%_ if (!['tsType'].includes(propKey)) { -%>
23+
<%_ if (!['tsType', 'relation'].includes(propKey)) { -%>
1824
<%= propKey %>: <%- propVal %>,
19-
<%_ } -%>
25+
<%_ } -%>
2026
<%_ }) -%>
2127
})
28+
<% } -%>
2229
<%= key %><%if (!val.required) {%>?<% } %>: <%= val.tsType %>;
2330
2431
<% }) -%>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,13 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
13091309
"name": "views",
13101310
"hide": false
13111311
},
1312+
"relations": {
1313+
"type": "Boolean",
1314+
"description": "Discover and create relations",
1315+
"default": false,
1316+
"name": "relations",
1317+
"hide": false
1318+
},
13121319
"schema": {
13131320
"type": "String",
13141321
"description": "Schema to discover",

0 commit comments

Comments
 (0)