CMS / foldkit-cms

cms

When an author edits a live blog post or marketing page, their half-written changes cannot overwrite the public table: visitors would see broken copy, and an unfinished title would fail database constraints. You need working drafts beside your published rows, revision history, scheduled releases, and unique URL slugs, while keeping unpublished work invisible to visitors.

foldkit-cms keeps drafts as unsent form inputs beside your existing content rows and runs your own create or update mutations when an author publishes. Visitors keep reading your existing tables; editors autosave drafts, preview edits in place, schedule releases, and restore earlier revisions.

See the editor, in-app preview, and server rules running in the live CMS demo.

Install

sh
pnpm add effect foldkit foldkit-entity foldkit-form foldkit-crud foldkit-remote foldkit-cms

A first content type

This declares a blog post type with title, slug, and publish timestamp roles, then derives the entry's current state and allowed actions from a draft beside a live row:

ts
import { Schema } from 'effect'
import { Cms } from 'foldkit-cms'
import { Entity } from 'foldkit-entity'
import { Form } from 'foldkit-form'
import { Mutation } from 'foldkit-remote'

const Post = Entity.define(
  'Post',
  Schema.Struct({
    id: Schema.String,
    title: Schema.String,
    slug: Schema.String,
    publishedAt: Schema.NullOr(Schema.String),
  }),
).pipe(Cms.roles({ label: 'title', slug: 'slug', published: 'publishedAt' }))

const PostInput = Schema.Struct({ title: Schema.String, slug: Schema.String })
const Posts = Cms.content('posts', {
  entity: Post,
  form: Form.make('PostForm', Entity.input(Post, PostInput)),
  publish: {
    create: Mutation.make('CreatePost', { Input: PostInput, Output: { id: Schema.String } }),
    update: Mutation.make('UpdatePost', { Input: { ...PostInput.fields, id: Schema.String }, Output: {} }),
  },
  words: { one: 'Post', many: 'Posts' },
})

const facts = { archivedAt: null, row: 'visible' as const, draft: { scheduledFor: null, scheduleError: null } }
const state = Cms.state(facts, new Date())
const actions = Cms.offers(facts, new Date(), Posts)

Because a live row exists alongside an unscheduled draft, state is { _tag: 'Changed', schedule: null } and actions is ['save', 'discard', 'publish', 'schedule', 'unpublish', 'restore', 'archive'].

  • Post is a foldkit-entity Entity annotated with Cms.roles, naming which fields hold the title, URL slug, and publish timestamp. A role on an incompatible field (such as slug on a number, or published on a non-null field) is a type error and throws.
  • Posts binds that Entity to a foldkit-form Form and your own foldkit-remote create and update mutations, so what publishing writes stays your code.
  • Capabilities follow declared roles: omitting slug skips slug handling, and omitting published keeps every row visible without an unpublish action.
  • Cms.rolesOf(Post) reads those roles back as Fields for public queries, audience policies, or your own sitemap query.