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

upsertGraph: allow marking individual models in graph to be unrelated or deleted #14

Merged

Conversation

falkenhawk
Copy link
Member

upsertGraph: allow marking individual models in graph to be unrelated or deleted with #unrelate or #delete (prop names configurable with Model.graphUnrelateProp and Model.graphDeleteProp)

which override GraphOptions i.e. even if noDelete or noUnrelate is set, specific models marked with those special props can still be unrelated/deleted. This allows partial unrelate/deletes to HasMany and ManyToMany relations without the need of passing the whole list of existing models which should be preserved.

Example:

await Model1.query(trx).upsertGraph({
  id: 1,
  
  // update idCol=1
  // delete idCol=2 with `#delete: true` special prop
  // insert one new
  // and keep all existing models intact - `noDelete: true`
  model1Relation2: [
    {
      idCol: 1,
      model2Prop1: 'updated hasMany 1',

      // unrelate id=4 with `#unrelate: true` special prop
      // don't unrelate other existing models because of `noDelete: true`
      // relate id=6
      // and insert one new
      model2Relation1: [
        {
          id: 4,
          '#unrelate': true,
        },
        {
          // This is the new row.
          model1Prop1: 'inserted manyToMany',
        },
        {
          id: 6,
        },
      ],
    },
    {
      idCol: 2,
      '#delete': true,
    },
    {
      // This is the new row.
      model2Prop1: 'inserted hasMany',
    },
  ],
}, { relate: true, noDelete: true });

@falkenhawk falkenhawk self-assigned this Feb 6, 2023
@falkenhawk falkenhawk force-pushed the upsertgraph-unrelate-delete-props-on-individual-models branch from 77a2cde to 0f9faec Compare February 6, 2023 22:12
with `#unrelate` or `#delete` (prop names configurable with `Model.graphUnrelateProp` and `Model.graphDeleteProp`)
which override `GraphOptions` i.e. even if `noDelete` or `noUnrelate` is set, specific models marked with those special props can still be unrelated/deleted.
This allows partial unrelate/deletes to HasMany and ManyToMany relations without the need of passing the whole list of existing models which should be preserved.
…alid id

`NotFoundError: model (id=x) is not a child of model (id=y). If you want to relate it, use the relate option. If you want to insert it with an id, use the insertMissing option`
- let upsertGraph ignore those errors when `#delete` or `#unrelate` props are used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant