Skip to content

Shop data model

A shop's data model (kind: er): six tables as a header over typed rows with PK, FK and UQ tags, crow's foot ends from each key to the foreign key that holds it, and order_items as the join table between orders and products.

6 nodes0 groups6 edges
Edit in the playgroundOpen full size
Data model: customers place orders and have addresses, orders ship to an address, contain order items and are settled by one payment, and products are sold as order itemsData model: customers place orders and have addresses, orders ship to an address, contain order items and are settled by one payment, and products are sold as order itemsData model: customers place orders and have addresses, orders ship to an address, contain order items and are settled by one payment, and products are sold as order itemsData model: customers place orders and have addresses, orders ship to an address, contain order items and are settled by one payment, and products are sold as order items
er-ecommerce.yamlOn GitHub
# yaml-language-server: $schema=../references/diagram.schema.json
title: Shop data model
subtitle: "An order holds many products through its items; a payment settles one order"
kind: er
# Parents on the left, the tables that point at them to the right: each line runs from the key a
# row is looked up by to the foreign key that holds it, and the marks say how many on each end.

nodes:
  customers:
    fields:
      - id uuid pk
      - email text unique
      - name text
      - created_at timestamptz
  products:
    fields:
      - id uuid pk
      - sku text unique
      - name text
      - price numeric
  orders:
    fields:
      - id uuid pk
      - customer_id uuid fk
      - address_id uuid fk
      - status text
      - placed_at timestamptz
  addresses:
    fields:
      - id uuid pk
      - customer_id uuid fk
      - line1 text
      - city text
      - postcode text null
  payments:
    fields:
      - id uuid pk
      - order_id uuid fk unique
      - provider text
      - paid_at timestamptz null
  # The join table: orders and products are many to many, one row per product on an order.
  order_items:
    fields:
      - order_id uuid pk fk
      - product_id uuid pk fk
      - quantity int
      - unit_price numeric

edges:
  - customers -- addresses: { card: "1:0..n", label: has }
  - customers -- orders: { card: "1:0..n", label: places }
  # Addresses and orders share a column: this line does not set the layers.
  - addresses -- orders: { card: "1:0..n", label: ships to, constraint: false }
  # A unique foreign key: at most one payment per order.
  - orders -- payments: { card: "1:0..1", label: settled by }
  - orders -- order_items: { card: "1:1..n", label: contains }
  - products -- order_items: { card: "1:0..n", label: sold as }

Start from this one

Copies the spec to diagram.yaml, then renders every format.

$ orrery init er-ecommerce$ orrery render diagram.yaml -f all --theme both