Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

falkenhawk
Copy link
Contributor

ported from ovos#14

NOTE: I am afraid this PR is not ready to be merged yet, as much as it has proven to be very useful for our use cases, a new section in documentation still needs to be added for this new feature.

Maybe someone would be willing to continue with it, 😊 as I don't have too much time on my hands currently to do it myself.


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 });

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.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant