Collections and Globals
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:
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:
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" } });
defineCollection() only adds types. You can also write the object straight into collections.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():
import { defineGlobal } from "tenoncms";
export const footer = defineGlobal({
label: "Footer",
fields: {
copyright: { type: "text" },
email: { type: "text" },
},
});
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.
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.
/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.false. See Localization.true.drafts. Default false.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) :::
tab: "name". Fields without a tab show above the tabs.fieldset: "name"./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
requiredfield is nevernullor missing. selectandpopulatechange 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.