Relationships & Bidirectional Backrefs
In traditional content management systems, relationships are one-way: an Article references an Author by ID, but the Author has no inherent awareness of which Articles reference them without running expensive, unbounded table scans.
BeechCMS solves this with Automatic Bidirectional Backrefs:
- When Seeds declare relations (
type: 'relation', targeting another Seed), the Botanical Engine registers an in-memory graph mapping all inbound and outbound edges at boot time. - Reverse lookups are pre-indexed into a fast, read-only
backrefMap. - The CMS safeguards against accidental broken links, cascading delete failures, and orphaned records via native SQLite constraints and dashboard guards.
Defining Relations in Seeds
Relations are declared using standard Seed branches with type: 'relation':
import { defineSeed } from '@beechcms/core'
export const ArticleSeed = defineSeed({
slug: 'articles',
label: 'Article',
labelPlural: 'Articles',
displayNameAlias: 'title', // Used as the human label across backrefs
branches: [
{
id: 'br_title',
alias: 'title',
label: 'Title',
type: 'text',
requiredOnCreate: true,
},
{
id: 'br_author',
alias: 'author',
label: 'Author',
type: 'relation',
targetSeed: 'authors',
requiredOnCreate: true,
onDelete: 'RESTRICT', // Blocks deleting author while referenced
},
{
id: 'br_categories',
alias: 'categories',
label: 'Categories',
type: 'relation',
targetSeed: 'categories',
multiple: true, // Many-to-many relationship
onDelete: 'CASCADE',
},
],
})Key Seed Configuration Options
| Property | Type | Description |
|---|---|---|
targetSeed | string | Required. The slug of the target seed being referenced (e.g. 'authors'). |
multiple | boolean | When false (default), stores a single foreign key column (content_<slug>.<branch>). When true, automatically manages a many-to-many junction table (rel_<slug>_<branch>). |
onDelete | 'SET NULL' | 'RESTRICT' | 'CASCADE' | SQLite foreign key action on target deletion. Defaults to 'SET NULL'. Setting 'RESTRICT' activates the BeechCMS deletion guard. |
displayNameAlias | string | Required on Seed. Specifies which field supplies the human-readable name (e.g. 'title') displayed in backref previews and dialogs. |
At engine boot, BeechCMS inspects all registered seeds and builds an in-memory backrefMap via buildBackrefMap(seeds). Single relations map to direct foreign keys, while many-to-many relations (multiple: true) automatically create and query junction tables without manual mapping code.
The Backrefs API
You can query all incoming relationships for any record using the protected /backrefs endpoint:
GET /api/content/:targetSlug/:targetId/backrefs
Authorization: Bearer <JWT_TOKEN>Response Example
{
"groups": [
{
"sourceSlug": "articles",
"sourceLabel": "Articles",
"branchAlias": "author",
"branchLabel": "Author",
"relationship": "single",
"restricts": true,
"total": 12,
"items": [
{
"id": "art_01",
"displayName": "Building Edge CMS with Cloudflare",
"status": "published",
"updated_at": 1709683200
},
{
"id": "art_02",
"displayName": "Understanding Botanical Seeds",
"status": "published",
"updated_at": 1709679600
},
{
"id": "art_03",
"displayName": "Direct-to-R2 Upload Architecture",
"status": "published",
"updated_at": 1709676000
}
]
},
{
"sourceSlug": "projects",
"sourceLabel": "Projects",
"branchAlias": "lead_contributor",
"branchLabel": "Lead Contributor",
"relationship": "single",
"restricts": false,
"total": 2,
"items": [
{
"id": "proj_10",
"displayName": "BeechCMS Core",
"status": "published",
"updated_at": 1709600000
},
{
"id": "proj_20",
"displayName": "Botanical UI Kit",
"status": "published",
"updated_at": 1709590000
}
]
}
]
}Field Reference
sourceSlug: Slug of the seed containing the relation branch.sourceLabel: Display label of the source seed (fromseed.labelPluralorseed.label).branchAlias: The alias of the relational branch.branchLabel: Human label of the relational branch (frombranch.label).relationship:'single'for direct foreign key column, or'multi'for many-to-many junction tables.restricts:truewhenbranch.onDelete === 'RESTRICT', signaling that deleting this target record is restricted.total: Total number of inbound records in this group.items: Up to 3 preview records in default mode (PREVIEW_LIMIT = 3), ordered byupdated_at DESC.displayName: Entry title resolved viasourceSeed.displayNameAlias. Respects field privacy policies (returns'••••••••'ifvisibility: 'masked', ornullifvisibility: 'hidden').status: Publication status ('published','draft').updated_at: Epoch timestamp in seconds.
Deep Pagination
For high-volume relations, clients can paginate specific inbound edges:
GET /api/content/authors/auth_123/backrefs?group=articles:author&page=2&limit=20| Parameter | Type | Description |
|---|---|---|
group | string | Group identifier in format <sourceSlug>:<branchAlias> (e.g. articles:author). |
page | number | Page number (1-based, default: 1). |
limit | number | Number of items per page (default: 20, maximum: 100). |
When group is supplied, the endpoint queries only the requested relationship edge, returning the paginated subset in items along with the full total count.
Dashboard Integration
In the BeechCMS React Dashboard, backrefs power two essential editorial experiences:
1. "Referenced By" Inspector
When editing any existing record in the Entry Editor, a collapsible card (ReferencedByPanel) is mounted under the form:
- Grouped Overview: Displays incoming references grouped by source seed and branch label.
- Inline Preview: Shows up to 3 preview entries per group with their title, publication badge, relative updated time, and a direct link to open the referenced entry.
- Paginated Dialog: Clicking "Show all N" opens a paginated dialog (
GroupDialog) powered byuseBackrefsGroupto navigate large relation sets. - Context-Aware Visibility: The panel is hidden automatically during record creation (
isCreate) and when no incoming references exist.
2. Deletion Guard & Orphan Prevention
Accidental deletions are prevented both in the UI and at the database layer:
- UI Guard: If any referencing group has
restricts: true(onDelete: 'RESTRICT') andtotal > 0, the Delete button in the form footer is disabled upfront with an informative tooltip ("Cannot delete: X entries depend on this record"). - Database Invariant: Cloudflare D1 (SQLite) foreign key constraints enforce
ON DELETE RESTRICTon a hard delete (purge). Moving a record to the Trash is only anUPDATE, so the API runs the same check beforehand: while an active entry still references the record through aRESTRICTrelation, both are rejected with HTTP409 Conflict. Entries already in the Trash do not block moving their parent to the Trash, but the foreign key still blocks the parent's purge. - Safe Cascade: If
onDelete: 'SET NULL'(the default), deleting a record safely nulls the foreign key column or deletes the junction table row without blocking deletions.
Frontend Querying with the Client SDK
The /backrefs endpoint is an authenticated administrative endpoint for the CMS Dashboard. Public frontends consume published content via @beechcms/client.
Because BeechCMS uses an edge-native relational data model (Cloudflare D1) rather than heavy nested ORM hydration, relations emit foreign key IDs. To query records by relationship or perform reverse lookups in frontend applications, query the referencing collection using filter:
import { createBeechClient } from '@beechcms/client/server' // browser: @beechcms/client/browser with publishableKey
const client = createBeechClient({
baseUrl: 'https://cms.example.com', // the SDK appends /api/v1/public itself
secretKey: process.env.BEECH_SECRET_KEY!,
})
// 1. Fetch an author entry
const { data: author } = await client.collection('authors').where({ id: 'auth_123' }).first()
// 2. Query articles referencing this author (reverse lookup)
const { data: articles } = await client
.collection('articles')
.where({ author: 'auth_123' })
.orderBy('created_at', 'desc')
.limit(10)
.list()Forward lookups without N+1
A foreign key can be resolved in the same request with .include() instead of a second round trip:
const result = await client
.collection('articles')
.where({ id: 'art_01' })
.include(['author'])
.first()
if (!result.error) {
const article = result.data.data as Article & { _includes?: { author?: Author } }
article.author // the raw foreign key, unchanged
article._includes?.author // the expanded author entry
}Expansion is depth 1, capped at 3 branches per request, and honours the target seed's allowPublicRead and field policies. See Relation Expansion.
Filtering by a property of the related entry
When the id is unknown, .whereRelation() pushes the condition to the relation target server-side — no client-side pre-query:
// Articles whose author is named Jane, in one request
const result = await client
.collection('articles')
.whereRelation('author', { where: { name: { contains: 'Jane' } } })
.orderBy('created_at', 'desc')
.list()Limits and error codes: Relation Subquery Filters.