Relationships — embed related records on read

Declare once that a post has an author (addRelationship('posts', 'author', 'authors')), and MQDB will embed the author record into your post whenever you ask for it. Your app gets a single nested object back instead of having to issue a second lookup per post.

How it works

flowchart LR
    P["posts/1
{ title, author_id: 'a1' }"] --> R{includes: author?} R -- no --> OUT1[post only] R -- yes --> L["lookup authors/a1"] L --> M["merge → post.author = {...}"] M --> OUT2["{ ...post, author: {...} }"] style OUT2 fill:#ede7f6,stroke:#5e35b1 style R fill:#e3f2fd,stroke:#1565c0

Try it — a three-step tour

  1. Click Seed test data to create two authors and three posts linked via author_id.
  2. Click Define posts→authors relationship. The rule appears below.
  3. Click Show side-by-side. The Without includes panel shows posts with a bare author_id; the With includes panel shows the same posts with the full author record nested under .author.

Setup

1 · Seed data

Creates authors (Alice, Bob) and three posts with author_id set.

2 · Define relationship

Joins posts.author_id → authors.id

Active relationships

None yet.

Manual query

Read one with includes

List with includes

Side-by-side comparison

The same list, queried with and without includes: ['author'].

Without includes

Bare rows — only author_id.

Click "Show side-by-side".

With includes: ['author']

Same rows, author object nested under .author.

Click "Show side-by-side".

Automated test suite

Run all tests runs these 5 steps and reports pass/fail:

  1. Seed 2 authors + 3 posts
  2. Declare the posts→authors relationship
  3. readWithIncludes('posts', id, ['author']) returns a nested author object
  4. list('posts', { includes: ['author'] }) nests author on every row
  5. Alice's post count matches what was seeded (2 posts)
Click the button to run.

Log