Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Confusion about updating associations dynamically #97

@thec0keman

Description

@thec0keman

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions