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
:slugsyntax, 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,extraadds paths such as/posts, andlastmodgives each document a<lastmod>: a field's name ('updated', afields.date) or a function returning a date.createMiddleware({ sitemap: { content, collections } })serves it at/sitemap.xml. - Slugs: a page at
/:slugcan't be calledpostswhile posts live under/posts/.createContentrefuses such slugs, soreservedSlugsonly 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 nosort. Pass asortto 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 itsid, so it works with every store, the JSON files included, and shows up in git diffs.createContentmanages it: saves keep the stored position (a stale editor can't undo a move), andcontent.move(collection, id, index)moves a document, rewriting only the documents whose position changes. Nobody types a position. - Collections without
orderablework as before, sorted bysort.
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
def: CollectionDefinition<F>
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: stringStable id: references store it, so it never changes (unlike the slug).
slug: stringThe document's address within its collection, e.g.
hello-world. Unique per collection.position?: numberWhere the document is in its collection's manual order (lower first), when the collection is
orderable.createContentsets it: on the first save and onmove; saves keep the stored one.fields: FThe collection's fields.
entries?: PageEntry[]Brick entries; always an array when the collection has a body.