Skip to content
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

Confusion about updating associations dynamically #97

Open
thec0keman opened this issue Mar 25, 2022 · 0 comments
Open

Confusion about updating associations dynamically #97

thec0keman opened this issue Mar 25, 2022 · 0 comments

Comments

@thec0keman
Copy link

thec0keman commented Mar 25, 2022

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:

  1. The different lifecycle hooks are confusing. From the documentation, onCreate and afterCreate seem to be specifically for async side-effects. But out of the 3 lifecycle hooks, only onCreate seems to update the factory result from the hook's return value. Is this the intended result / use case? It's just awkward to have to await a factory when I'm not using any async behavior.

  2. Chaining traits doesn't seem to work correctly. If I have a factory with multiple traits that update associations via onCreate, only the last called trait will stick. This seems like it is either a bug or I'm not using the hook as intended. I missed in the documentation that onCreate can 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant