liveBook · Manning

Parent-child relationship

As explained in API Design Patterns

A hierarchical relationship is a parent-child relationship in which resources occupy different levels of an ordered structure.

A parent–child relationship models true ownership or containment between resources. The child belongs to one parent, and actions or properties can cascade from parent to child, such as deletion or access control. section 4.1.1 section 4.2.3

Use hierarchy when the relationship has these characteristics:

  • Deleting the parent should normally delete its children. section 4.2.3
  • Access to the parent should imply the same level of access to the child. section 4.2.3
  • The child has one parent rather than many; use a reference relationship when resources are merely associated or can change parents. section 4.2.3 section 6.3.6

Chat rooms and messages

Model Message resources beneath ChatRoom resources when deleting a chat room should delete its messages and granting access to the room should grant access to those messages. Conversely, do not encode a mutable association as ownership: if a book can move between shelves, store shelfId on Book rather than using an identifier such as shelves/1/books/1. section 4.2.3 section 6.3.6

Figure 4.5 ChatRoom resources act as the owner of Message resources through hierarchical relationships.

Inspect the resource layout showing ChatRoom resources owning Message resources. section 4.1.1

How API Design Patterns covers it

How deep it goes
Defines ownership, single-parent structure, cascading deletion, and access inheritance, but stops short of implementation mechanics.
Examples
Uses company directories, ChatRoom and Message resources, and Book and Shelf identifiers to distinguish ownership from association.
What it assumes
Assumes resource modeling and identifiers, while distinguishing ownership from ordinary association through concrete API scenarios.
What you can do after
Evaluate whether a relationship represents ownership and choose hierarchy or a reference relationship accordingly.
How it connects
Connects reference relationships, cascading deletion, access control, resource identifiers, and mutable resource properties.

Also explained in

Compare how 2 books explain parent-child relationship →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.