Skip to main content
CMS

Schemas

Overview

CMS content models live in .schema.ts files right alongside your code. They're versioned in Git, checked by TypeScript, and used to generate template types when you run root-cms generate-types.

Collections

A collection groups documents that share a schema, like blog posts or landing pages. Create one at collections/<Name>.schema.ts:

ts
// @/collections/BlogPosts.schema.ts

import {schema} from '@blinkk/root-cms';

export default schema.collection({
  name: 'BlogPosts',
  description: 'Posts for the blog.',
  url: '/blog/[slug]',
  preview: {
    title: 'title',
    image: 'image',
  },
  fields: [
    schema.string({
      id: 'title',
      label: 'Title',
      translate: true,
    }),
    schema.image({
      id: 'image',
      label: 'Image',
      help: 'Upload a 1600x900 JPG.',
    }),
    schema.richtext({
      id: 'content',
      label: 'Content',
      translate: true,
    }),
  ],
});

url tells the CMS where each document is served so it can build live preview frames and links.

Reusable schemas

Use schema.define() for reusable models shared across collections, such as page modules. schema.glob() picks up every schema matching a pattern, so dropping in a new template schema automatically registers it in the CMS:

ts
// @/templates/TemplateHero/TemplateHero.schema.ts

import {schema} from '@blinkk/root-cms';

export default schema.define({
  name: 'TemplateHero',
  fields: [
    schema.string({id: 'title', translate: true}),
    schema.richtext({id: 'body', translate: true}),
    schema.image({id: 'image'}),
  ],
});
ts
// @/collections/Pages.schema.ts

import {schema} from '@blinkk/root-cms';

export default schema.collection({
  name: 'Pages',
  url: '/[...slug]',
  fields: [
    schema.array({
      id: 'modules',
      label: 'Modules',
      of: schema.oneOf({
        types: schema.glob('/templates/*/*.schema.ts'),
      }),
    }),
  ],
});

Every schema.define() name must be unique across your project.

Collection options

Beyond name, url, preview, and fields, a collection can configure how its docs are listed, named, and published:

  • group: groups collections together in the CMS content list.

  • previewUrl: overrides the URL loaded in the editor's preview pane when it differs from url. Use domain to set the domain for multi-domain sites.

  • preview.defaultImage and preview.fit: sets a fallback image for docs without one, and controls whether thumbnails are cropped ('cover', the default) or letterboxed ('contain').

  • slugRegex: a regular expression (as a string) that new slugs must match.

  • autoSlug: pre-fills new doc slugs from a template like '{date:YYYYMMDD}-{adjective}-{noun}'. Supported tokens are {date}, {date:FORMAT}, {adjective}, {noun}, and {random:N}.

  • autolock and autolockReason: locks publishing whenever a doc is edited, requiring someone to unlock it deliberately before publishing.

  • sortOptions: adds custom sort options to the doc list, like {id: 'title', label: 'Title', field: 'fields.meta.title'}. The first time you use a new sort, Firestore may return an error with a direct link to create the required index.

  • customSorting: lets editors drag docs into a custom order (see below).

  • viewOptions.compact: always opens the collection in compact table view, which works well for collections without preview images or titles (like redirects).

  • publishing.checks: checks that run before docs are published (see Checks).

When customSorting: true is enabled, the doc list includes a Custom order sort where editors can drag docs into place. Each doc's position is saved at sys.sortKey on the draft, so reordering goes live when the moved docs are published, and listDocs() follows that order by default. Docs created outside the CMS won't have a position until an editor assigns one from the banner in the doc list, or a script generates keys with generateKeyBetween() and generateNKeysBetween() from @blinkk/root-cms.

Field types

Available field types include string, richtext, image, file, select, multiselect, boolean, number, date, datetime, reference, object, array, and oneOf. See schema in the API reference for every field and option.

Available as of version 4.2.1. Set maxItems on an array field to cap how many items it can hold (such as the modules on a page). Once the limit is reached, editors can't add, duplicate, or paste more items, and API or Root AI writes that exceed the limit fail validation.

Conditional fields

Available in version 4.1.0 and later.

Use showWhen to show a field only when a sibling field matches a condition, like a CTA label that appears only when "Show CTA" is checked. field is the ID of a sibling in the same object, and dot-paths read nested values (such as 'media._type' for the selected type in a oneOf field).

ts
// @/templates/TemplateHero/TemplateHero.schema.ts

import {schema} from '@blinkk/root-cms';

export default schema.define({
  name: 'TemplateHero',
  fields: [
    schema.boolean({id: 'showCta', checkboxLabel: 'Show CTA'}),
    schema.string({
      id: 'ctaLabel',
      // Shown when the "Show CTA" checkbox is checked.
      showWhen: {field: 'showCta'},
    }),
    schema.oneOf({
      id: 'media',
      types: [ImageSchema, VideoSchema],
    }),
    schema.boolean({
      id: 'autoplay',
      checkboxLabel: 'Autoplay',
      // Shown when a Video is picked for "media".
      showWhen: {field: 'media._type', equals: 'Video'},
    }),
  ],
});

A condition supports these operators:

  • None: matches when the sibling value is truthy, like a checked boolean.

  • equals and notEquals: checks whether the sibling value matches (or doesn't match) an exact value.

  • in and notIn: checks whether the sibling value is (or isn't) in a list, like {field: 'layout', in: ['split', 'stacked']}.

  • exists: with true, matches when the sibling has a value; with false, matches when it's empty (unset, '', or an empty array).

Pass an array of conditions when all of them must match, like showWhen: [{field: 'showCta'}, {field: 'variant', equals: 'button'}].

showWhen only affects visibility in the editor. Hiding a field doesn't erase its value, so editors can toggle a checkbox off and back on without losing what they typed, and the stored value is still published and returned by the API. Check the same condition in your template when rendering, like {fields.showCta && fields.ctaLabel}.

1
2
3
4
5
6
7
8
9
10
11
12
Breakpoint: