Fields

Relation and join

Point to other documents, and list the documents that point back.

A relation stores the id of another document, like the author of a post. A join is the other side: it lists the documents whose relation points here, like the posts of an author.

The relation field in the admin

Example

cms/collections/posts.ts
export const posts = defineCollection({
  fields: {
    title: { type: "text", required: true },
    author: { type: "relation", collection: "authors" },
    tags: { type: "relation", collection: "tags", many: true },
  },
});

A read loads the related documents in place of the ids:

const post = await cms.findOne("posts", { where: { slug } });
post?.author?.name; // string
post?.tags?.map((tag) => tag.title);

const raw = await cms.findOne("posts", { where: { slug }, populate: false });
raw?.author; // string, the id

Render it

<p>By {post.author?.name ?? "Unknown"}</p>
<ul>
  {post.tags?.map((tag) => <li>{tag.title}</li>)}
</ul>

How deep a read loads, and how to keep the loaded documents small, is in Relations.

  • Each write checks that every id exists. A document cannot point to itself.
  • A many relation keeps its order and rejects duplicates.
  • A relation works at the top level, in groups and in list items, in collections and globals. It can only point to collections.
  • A typo in collection is a type error in tenon.config.ts.

You can filter by a single top-level relation: cms.find("posts", { where: { author: id } }). Filters do not accept many relations, relations in groups or joins.

Join

A join stores nothing. It lists the documents of collection whose relation on points to this document:

cms/collections/authors.ts
export const authors = defineCollection({
  titleField: "name",
  fields: {
    name: { type: "text", required: true },
    posts: { type: "join", collection: "posts", on: "author" },
  },
});
The join field in the admin

A read loads a join only when you name it:

const author = await cms.findById("authors", id, { populate: { posts: true } });
author?.posts?.map((post) => post.title);
<h2>Posts by {author.name}</h2>
<ul>
  {author.posts?.map((post) => (
    <li><a href={`/posts/${post.slug}`}>{post.title}</a></li>
  ))}
</ul>
  • A join works only at the top level of a collection.
  • on can name a relation inside a group, like seo.image.
  • required, default, hidden and private do not apply, because a join stores nothing.

Options

The collection the ids point to. It can be the collection that holds the field, like a parent page.Store a list of ids instead of one. With required, at least one. Default false.The fields a loaded document keeps, like { name: true, avatar: true }. See Keep related documents small.What happens when the related document is deleted. Default "restrict" for a required relation, otherwise "set-null". See When a document is deleted.

The options of a join:

The collection whose documents point here.The relation field in collection that points here, like author or seo.image.