-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): add option to create relations in cli discover command #8461
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'), | ||
|
@@ -289,6 +295,7 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator { | |
{ | ||
schema: modelInfo.owner, | ||
disableCamelCase: this.artifactInfo.disableCamelCase, | ||
associations: this.options.relations, | ||
}, | ||
); | ||
if (this.options.optionalId) { | ||
|
@@ -336,6 +343,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; | ||
Comment on lines
+376
to
+377
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some connectors support "strong relations" (i.e. Constraints enforced by the database engine) through See https://github.com/loopbackio/loopback-connector-mysql/tree/f8f40a1199b6ed63ba01d6d173a9314004c08c93#auto-migrateauto-update-models-with-foreign-keys for an example of the syntax. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More info can be found here: https://github.com/loopbackio/loopback-datasource-juggler/blob/055efd3e89e2326bc81e348cbb1c6ef973ab6428/types/model.d.ts#L160-L169 ...which is part of this PR: loopbackio/loopback-datasource-juggler#1904 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @achrinza Thank you for the review. and sure we can do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @achrinza, we have updated the code to populate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @achrinza, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @sohaibahsan007, we're aware of the issue and looking into it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. |
||
if (Object.keys(foreignKeys)?.length > 0) { | ||
Object.assign(templateData.settings, {foreignKeys}); | ||
} | ||
templateData.modelSettings = utils.stringifyModelSettings( | ||
templateData.settings, | ||
); | ||
} | ||
|
||
this.copyTemplatedFiles( | ||
modelDiscoverer.MODEL_TEMPLATE_PATH, | ||
fullPath, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note for myself: We'll need to figure out how to handle composite foreign keys in the future.