-
-
Couldn't load subscription status.
- Fork 39
Description
I'm trying to figure out how to dynamically build associations, and I'm a bit confused about the API and what the correct way to handle this is.
My primary use case is we have a complex data model where we need to call multiple traits to set up a variety of associations. These traits can't use associations() though, because we need to generate the association in the context of a specific record (i.e. we need to be able to set foreign keys).
This is an example that I have been working with:
withQaReports() {
return this.onCreate(workflow => ({
...workflow,
qualityAssuranceReports: qualityAssuranceReportFactory.buildList(
2,
{},
{
associations: {
assignment: assignmentFactory.build({ workflowId: workflow.id }),
},
},
),
}));
}
I'm running into a few issues that either may be my misunderstanding and/or possible bugs:
-
The different lifecycle hooks are confusing. From the documentation,
onCreateandafterCreateseem to be specifically for async side-effects. But out of the 3 lifecycle hooks, onlyonCreateseems to update the factory result from the hook's return value. Is this the intended result / use case? It's just awkward to have toawaita factory when I'm not using any async behavior. -
Chaining traits doesn't seem to work correctly. If I have a factory with multiple traits that update associations viaI missed in the documentation thatonCreate, only the last called trait will stick. This seems like it is either a bug or I'm not using the hook as intended.onCreatecan only be defined once. Given this limitation, this seems like a poor option for my use case.
Is there a way to use composable traits that update the shape of the object?
Thanks!