CodamAIDocs
Topicdone

Lists as target state

A list in the payload describes what the list should look like afterwards. This page explains what happens to members that no longer appear.

Variants
removed with DELETE flag → deletedremoved without DELETE flag → detachedn:m: connection removed, even with the DELETE flagnew membersempty listlist missing: PUT vs. PATCHorderduplicate entries

What this is about

If you send a list field, for example employees on a company, the list describes the state afterwards: The list should have exactly these members. So the list is not an “add this”. It is a target state.

Before and after

flowchart LR
    subgraph vorher ["before: employees"]
      A1[Anna]
      B1[Ben]
      C1[Cem]
    end
    subgraph payload ["in the request"]
      A2["{ id: Anna }"]
      C2["{ id: Cem }"]
      D2["{ firstname: Dora }"]
    end
    subgraph nachher ["after: employees"]
      A3[Anna]
      C3[Cem]
      D3["Dora (new)"]
    end
    vorher --> payload --> nachher
Memberbeforein the requestafter
Annayeswith idstays
Benyesmissinggets removed: deleted (DELETE flag) or detached
Cemyeswith idstays
Doranowithout idis created (needs CREATE)

What happens to a removed member

Ben drops out of the list

When: The relation employees has the DELETE flag, Ben is a dependent child.

  1. 1
    CDMS
    checks the delete role of the employee model
  2. 2
    Hook
    Ben's DELETE hooks run
  3. 3
    CDMS→Database
    deletes Ben, together with his own dependent children and files

Result: Ben no longer exists, read returns 404.

When: The relation items of an invoice has no DELETE flag, the items are independent.

  1. 1
    CDMS→Database
    sets the field of the other side on the item to empty, that is sponsorInvoice = null
  2. 2
    CDMS
    the item itself stays

Result: The item is detached: It still exists, but no longer belongs to any invoice. This does not need a role on the item model.

When: A role is removed from groups, the relation has no DELETE flag.

  1. 1
    CDMS→Database
    deletes the row in the join table

Result: Both objects stay, only their connection is gone.

When: A role is removed from the roles of a group, the relation has the DELETE flag.

  1. 1
    CDMS→Database
    deletes the row in the join table, just as without the flag

Result: The role stays, also for all other groups. DELETE has no effect on n:m, because the member can belong to other partners as well. See Many-to-many through a join table.

The list step by step

How CDMS reconciles a list
  1. 1
    CDMS
    collects the ids of all entries in the request
  2. 2
    CDMS
    removes every previous member whose id is not included from the list: delete (DELETE) or detach
  3. 3
    CDMS
    entries with id: link or update along, depending on flag and verb
  4. 4
    CDMS
    entries without id: create new, if the relation has CREATE
  5. 5
    CDMS→Database
    saves everything in one transaction

How entries with and without id are handled is described in The four cases in nested writing.

Empty list, missing list

What a list in the request does
VerbList field in the requestResult
PUTmissingall members removed
PUTnull or []all members removed
PATCHmissingunchanged
PATCHnull or []all members removed
PUT, PATCHpartial listexactly these members
Createmissing, null or []empty list

For every member, “removed” means: deleted with the DELETE flag, otherwise detached.

Order and duplicate entries

  • Order: A list in CDMS is a set, without positions. The order in the request is not saved. When you read, you set the order with order in the response of the list. Otherwise the order is not defined. See Filters in nested lists.
  • Duplicate entries: Two identical entries without id create two objects with Create and PUT, and one with PATCH. The same id twice with different values: the last entry wins. Send each member only once.

Pitfalls

What comes next

Sources in the code and the knowledge base
  • CDMS/cdms-system-layer – AbstractLayer.reduceToTargetState, detachOrDeleteMember, removeBackReference, recursiveDelete, recursiveUpdate/recursivePatch (lists), fetchAndSetList (order)
  • CDMS/cdms-integrationtest – AbstractUpdateTest (updateOneToManyListRecursiveDeleteReallyDeletes), AbstractRecursiveUpdate, AbstractRecursivePatch, AbstractManyToManyTest; probe against SponsorInvoice, Group, system Role/Group
  • documentation/20-api/04-schreibsemantik.md
Search