Skip to content

Commit

Permalink
feat(cli): add option to create relations in cli discover command
Browse files Browse the repository at this point in the history
Signed-off-by: Awais Saeed <[email protected]>
  • Loading branch information
awaissaeedforenax committed May 18, 2022
1 parent a59eb57 commit d37b161
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/site/Discovering-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Models can be discovered from a supported datasource by running the

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

`--relations`: Choose whether to create relations. Default is false

`--all`: Skips the model prompt and discovers all of them

`--outDir`: Specify the directory into which the `model.model.ts` files will be
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/.yo-rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,13 @@
"name": "views",
"hide": false
},
"relations": {
"type": "Boolean",
"description": "Discover and create relations",
"default": false,
"name": "relations",
"hide": false
},
"schema": {
"type": "String",
"description": "Schema to discover",
Expand Down
47 changes: 47 additions & 0 deletions packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
default: true,
});

this.option('relations', {
type: Boolean,
description: g.f('Discover and create relations'),
default: false,
});

this.option('schema', {
type: String,
description: g.f('Schema to discover'),
Expand Down Expand Up @@ -284,6 +290,7 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
{
schema: modelInfo.owner,
disableCamelCase: this.artifactInfo.disableCamelCase,
associations: this.options.relations,
},
),
);
Expand Down Expand Up @@ -321,6 +328,46 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
);
debug(`Writing: ${fullPath}`);

if (this.options.relations) {
const relationImports = [];
const relationDestinationImports = [];
const foreignKeys = {};
for (const relationName in templateData.settings.relations) {
const relation = templateData.settings.relations[relationName];
const targetModel = this.artifactInfo.modelDefinitions.find(
model => model.name === relation.model,
);
// If targetModel is not in discovered models, skip creating relation
if (targetModel) {
Object.assign(templateData.properties[relation.foreignKey], {
relation,
});
relationImports.push(relation.type);
relationDestinationImports.push(relation.model);

foreignKeys[relationName] = {};
Object.assign(foreignKeys[relationName], {
name: relationName,
entity: relation.model,
entityKey: Object.entries(targetModel.properties).find(
x => x?.[1].id === 1,
)?.[0],
foreignKey: relation.foreignKey,
});
}
}
templateData.relationImports = relationImports;
templateData.relationDestinationImports = relationDestinationImports;
// Delete relation from modelSettings
delete templateData.settings.relations;
if (Object.keys(foreignKeys)?.length > 0) {
Object.assign(templateData.settings, {foreignKeys});
}
templateData.modelSettings = utils.stringifyModelSettings(
templateData.settings,
);
}

this.copyTemplatedFiles(
modelDiscoverer.MODEL_TEMPLATE_PATH,
fullPath,
Expand Down
15 changes: 11 additions & 4 deletions packages/cli/generators/model/templates/model.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<% if (isModelBaseBuiltin) { -%>
import {<%= modelBaseClass %>, model, property} from '@loopback/repository';
import {<%= modelBaseClass %>, model, property<%if (locals.relationImports) {%><% relationImports.forEach((relation) => {-%>, <%= relation %> <%_})-%><%}%>} from '@loopback/repository';
<% } else { -%>
import {model, property} from '@loopback/repository';
import {model, property<%if (locals.relationImports) {%><% relationImports.forEach((relation) => {-%> , <%= relation %> <%_})-%><%}%>} from '@loopback/repository';
import {<%= modelBaseClass %>} from '.';
<% } -%>
<%_ if (locals.relationDestinationImports && locals.relationDestinationImports.length > 0) { -%>
import {<% relationDestinationImports.forEach((model, index) => {-%><%= model %><%if (index!==relationDestinationImports.length-1) {%>,<% } %> <%_}) -%>} from '.';
<%_ } -%>

<% if (modelSettings) { -%>
@model(<%- modelSettings %>)
Expand All @@ -12,13 +15,17 @@ import {<%= modelBaseClass %>} from '.';
<% } -%>
export class <%= className %> extends <%= modelBaseClass %> {
<% Object.entries(properties).forEach(([key, val]) => { -%>
<% if (val.relation) { -%>
@<%= val.relation.type %>(() => <%= val.relation.model %>)
<% } else { -%>
@property({
<%_ Object.entries(val).forEach(([propKey, propVal]) => { -%>
<%_ if (!['tsType'].includes(propKey)) { -%>
<%_ if (!['tsType', 'relation'].includes(propKey)) { -%>
<%= propKey %>: <%- propVal %>,
<%_ } -%>
<%_ } -%>
<%_ }) -%>
})
<% } -%>
<%= key %><%if (!val.required) {%>?<% } %>: <%= val.tsType %>;
<% }) -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,13 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"name": "views",
"hide": false
},
"relations": {
"type": "Boolean",
"description": "Discover and create relations",
"default": false,
"name": "relations",
"hide": false
},
"schema": {
"type": "String",
"description": "Schema to discover",
Expand Down

0 comments on commit d37b161

Please sign in to comment.