Guides

Collections

Sets of documents that share a schema, such as pages, posts and products.

A collection is a named set of documents with the same fields. Pages, posts, products and categories are all collections. Mortar ships none: your site declares what it needs.

import { defineCollection, fields } from '@mortar-kit/core';

export const posts = defineCollection({
  name: 'posts',
  label: 'Posts',
  fields: {
    title: fields.text({ label: 'Title', required: true }),
    date: fields.date({ label: 'Published', required: true }),
    excerpt: fields.textarea({ label: 'Excerpt' }),
  },
  body: true,
  sort: { field: 'date', order: 'desc' },
});
Option Meaning
name Stable identifier and storage folder. Lowercase, e.g. posts or case-studies.
label The plural name the admin shows. Defaults to name.
fields The fields every document has.
body Whether documents also have a body of bricks. Default false.
title The text field shown as a document's title in lists. Defaults to the first text field, then the slug.
sort The default list order: a field (or 'slug') and 'asc' or 'desc'. Defaults to the title.
orderable Whether editors put the documents in order by hand. Lists then follow that order. Default false. See Manual order.
reservedSlugs Slugs documents may not use, such as your own route segments next to /:slug (admin).
canWrite Who may change its documents, given the user and the document. Checked with the content server's own rule (see Sign-in).
url Where a document lives on the site: a path with one :slug segment, e.g. /posts/:slug. See URLs.
home With url: the slug of the document at the pattern's prefix instead, e.g. home at / for /:slug.

Documents

A document is a plain object:

interface Doc {
  id: string;        // stable: references store it, and it never changes
  slug: string;      // the address, unique in the collection, e.g. 'hello-world'
  position?: number; // its place in a manual order (orderable collections only)
  fields: { ... };   // the collection's fields
  entries?: PageEntry[]; // the body, when the collection has one
}

Slugs are lowercase letters and digits with single dashes between, up to 100 characters. Renaming a slug keeps the id, so links by reference still work.

Keys of fields that the schema doesn't describe are kept, so other code can store its own data alongside. Use DocOf<typeof posts> for a typed document.

URLs

Give a collection a url and its pattern drives everything that needs a document's address:

export const pages = defineCollection({ name: 'pages', fields, body: true, url: '/:slug', home: 'home', reservedSlugs: ['admin'] });
export const posts = defineCollection({ name: 'posts', fields, body: true, url: '/posts/:slug' });
  • Routes: the pattern uses the router's :slug syntax, so routes take it as their path: { path: posts.url!, component: Post }. Mortar still doesn't route; your app declares its routes.
  • Links: posts.urlOf(doc) gives /posts/hello; pages.urlOf({ slug: 'home' }) gives /.
  • Admin preview links: ⌘/Ctrl-clicking a link in the preview opens the document it shows, matched by the patterns (resolveUrl); <Admin resolveLink> overrides it.
  • Sitemap: sitemap(content, collections, { origin }) returns the XML for every document with a URL. include(collection, doc) leaves some out, extra adds paths such as /posts, and lastmod gives each document a <lastmod>: a field's name ('updated', a fields.date) or a function returning a date. createMiddleware({ sitemap: { content, collections } }) serves it at /sitemap.xml.
  • Slugs: a page at /:slug can't be called posts while posts live under /posts/. createContent refuses such slugs, so reservedSlugs only needs your own routes.

When patterns overlap, the more specific one wins (/posts/:slug over /:slug), as in routers.

Manual order

Some sets are curated rather than sorted: photos, a timeline, project cards. Make the collection orderable and editors put its documents in order in the admin, by dragging a row of the document list or with its move up and down buttons.

export const projects = defineCollection({
  name: 'projects',
  label: 'Projects',
  fields: { title: fields.text({ label: 'Title', required: true }), image: fields.image({ label: 'Image' }) },
  orderable: true,
});
  • Lists return documents in that order: content.list(projects) needs no sort. Pass a sort to order them another way for one list.
  • New documents go to the end.
  • The order is kept in each document's position (1, 2, 3…), next to its id, so it works with every store, the JSON files included, and shows up in git diffs. createContent manages it: saves keep the stored position (a stale editor can't undo a move), and content.move(collection, id, index) moves a document, rewriting only the documents whose position changes. Nobody types a position.
  • Collections without orderable work as before, sorted by sort.

Making an existing collection orderable

Nothing to migrate. Documents without a position come after those with one, in the collection's sort order, so a collection you make orderable keeps its current order. The first move, or the first save of a new document, gives every document a position in that order (one write per document, once). Files you add by hand without a position join the end the same way.

If your app wraps Content for the admin (as the example's server functions do), add move to the wrapper: without it the admin shows no reorder controls.

Many collections, one admin

Pass them all to <Admin> in the order its rail should list them, and to createContent, which checks that every reference field names a known collection.

export const collections = [pages, posts, products, categories] as const;

defineCollectionfunction

import { defineCollection } from '@mortar-kit/core'

function defineCollection<F extends Readonly<Record<string, AnyField>>>(def: CollectionDefinition<F>): Collection<F>

Registers a collection.

Parameters

Returns

Collection<F>

Example

export const posts = defineCollection({
  name: 'posts',
  label: 'Posts',
  fields: { title: fields.text({ label: 'Title', required: true }), date: fields.date({ label: 'Date' }) },
  body: true,
  sort: { field: 'date', order: 'desc' },
});

Docinterface

import { Doc } from '@mortar-kit/core'

interface Doc<F = Record<string, unknown>>

One document of a collection.

Properties

id: string

Stable id: references store it, so it never changes (unlike the slug).

slug: string

The document's address within its collection, e.g. hello-world. Unique per collection.

position?: number

Where the document is in its collection's manual order (lower first), when the collection is orderable. createContent sets it: on the first save and on move; saves keep the stored one.

fields: F

The collection's fields.

entries?: PageEntry[]

Brick entries; always an array when the collection has a body.