Content Modeling

Collections and Globals

Describe content in TypeScript and get typed documents back.

You describe your content in TypeScript. A collection is a list of documents with the same fields, like posts. A global is one document per name, like the site settings. Both are plain objects, and the types of your documents come from them.

Collections

A collection holds many documents that share one set of fields. Define it with defineCollection(), usually in its own file:

cms/collections/posts.ts
import { defineCollection } from "tenoncms";

export const posts = defineCollection({
  label: "Posts",
  titleField: "title",
  permalink: "/posts/{slug}",
  fields: {
    title: { type: "text", required: true },
    slug: { type: "slug", from: "title", unique: true },
    date: { type: "date" },
    body: { type: "richtext" },
  },
});

Register it in tenon.config.ts. The key is the name you read it by:

tenon.config.ts
import { defineCms } from "tenoncms";
import { sqlite } from "tenoncms/sqlite";
import { posts } from "./cms/collections/posts";

export default defineCms({
  database: sqlite("./cms.db"),
  secret: process.env.TENON_SECRET!,
  collections: { posts },
});
const all = await cms.find("posts");
const one = await cms.findOne("posts", { where: { slug: "hello-world" } });
While these examples show that collections should be in there own files, they don't have to be. defineCollection() only adds types. You can also write the object straight into collections.
Every document also gets id, createdAt, updatedAt, publishedAt and hasDraft. Do not use these names for your own fields.

Globals

A global is a single document, such as a footer, a cookie banner or contact details. It has no list and no id to look up. Define it with defineGlobal():

cms/globals/footer.ts
import { defineGlobal } from "tenoncms";

export const footer = defineGlobal({
  label: "Footer",
  fields: {
    copyright: { type: "text" },
    email: { type: "text" },
  },
});
tenon.config.ts
export default defineCms({
  // ...
  globals: { footer },
});
const footer = await cms.getGlobal("footer");

A global that was never saved reads as {}. Handle missing values in your templates.

Tenon adds two globals on its own: general (site title, tagline, favicon) and seo (title template, description, share image). They live on the admin's Settings page. See Pages and SEO.

Options

Collections and globals take the same options, except where the table says otherwise.

The fields of each document. See Fields.The name in the admin. Defaults to the key. Pass an object to translate it.Short text under the name, above the form and on the Settings page.The field the admin shows in lists and pickers. Defaults to the first field.If you want to serve a collection through a route, like /posts/{slug} you have to set the permalink. E.g. {slug} is a field placeholder on that document. You can also choose another field or even multiple. The sitemap, links and the live preview use it.Store the values per language. Default false. See Localization.Keep a draft next to the published version. Default true.Editors can plan a publish or unpublish for later. Needs drafts. Default false.Who can read, create, update and delete. By default, everyone reads published content and signed-in users do the rest. See Access control.Functions that run on reads, writes and deletes. See Hooks.

Where the entry shows in the admin. icon is a Lucide name and display can be

  • "content" (the sidebar, default)
  • "settings" (the built-in Settings page)
  • "main" (the top bar)
  • "none" (don't show at all) :::
UI Tabs in the admin form. A field picks its tab with tab: "name". Fields without a tab show above the tabs.Boxes around fields in the admin form. A field picks its box with fieldset: "name".List the documents in /sitemap.xml. Needs a permalink. Default true.

tabs and fieldsets change only the admin. The stored document keeps its shape:

export const posts = defineCollection({
  tabs: {
    content: { label: "Content", icon: "file-text" },
    meta: { label: "Meta", icon: "tag" },
  },
  fields: {
    title: { type: "text", required: true, tab: "content" },
    body: { type: "richtext", tab: "content" },
    date: { type: "date", tab: "meta" },
  },
});

Presets

Most sites need the same few things: pages with nested URLs, a navigation menu and SEO. Presets give you these as ready-made collections and globals from tenoncms/presets. See Presets.

Types

The schema is the type. You write no interfaces, and reads return typed documents:

const post = await cms.findOne("posts", { where: { slug } });
post?.title; // string
post?.date; // string | null | undefined

cms.find("postz"); // type error: unknown collection
cms.find("posts", { where: { titel: "Hi" } }); // type error: unknown field
  • A required field is never null or missing.
  • select and populate change the result type. See Reading.
  • The same types reach the browser through createClient(). See HTTP API and client.

defineCms() also checks the schema when it starts. A relation to an unknown collection or a condition on an unknown field stops the server with an error that lists every problem.