CMS / foldkit-cms-drizzle

cms-drizzle

Storing drafts inside your published database table forces every query in your app to remember WHERE published_at IS NOT NULL. Miss that filter once on a relation, reference lookup, or search endpoint, and visitors see unfinished drafts or unpublished rows.

foldkit-cms-drizzle stores entries, working drafts, and revision history in three separate Drizzle tables (cms_entries, cms_drafts, cms_revisions) beside your content tables, and enforces visitor visibility on every table binding through foldkit-guard. Authors read drafts and unpublished rows; visitors see only rows permitted by your content Entity's read rules, whether fetched by id, relation, reference, or query. It supports SQLite and Postgres (both provide RETURNING for atomic conflict checks).

See the server and editor running together in the live CMS demo.

Install

sh
pnpm add effect drizzle-orm foldkit-cms foldkit-cms-drizzle foldkit-guard foldkit-remote-drizzle foldkit-remote-server

A first guarded blog server

Given your User principal schema, Blog.Post entity, Posts content type from foldkit-cms, Drizzle posts table, bind helper, and createPost/updatePost handlers (shown whole in the runnable server and compiled in test/readme.test-d.ts), this creates the CMS tables, attaches visitor visibility, and builds the server handlers:

ts
import { Option } from 'effect'
import { CmsServer, Transaction, sqliteTables } from 'foldkit-cms-drizzle'
import { Expr } from 'foldkit-entity'
import { Guards } from 'foldkit-guard'
import { GuardDrizzle } from 'foldkit-guard/drizzle'

const G = Guards.declare('Cms', User, guard => ({
  isAuthor: guard.principal(),
  isPublished: guard.row(Blog.Post, () => Expr.isNotNull(Blog.Post.fields.publishedAt)),
}), { key: user => user.id, unguarded: 'allow' })

const Policy = G.implement({
  isAuthor: principal => Option.exists(principal, user => user.role !== 'reader'),
})
const Post = Blog.Post.pipe(Guards.attach(G, { row: { Read: [G.isPublished, G.isAuthor] } }))
const Db = bind({ ...Blog, Post }, {
  Post: { table: posts, visible: GuardDrizzle.visible(Policy, [Post]).Post },
})

const cms = CmsServer.make({
  tables: sqliteTables(), // or pgTables(); include in your migrations
  content: [{ type: Posts, entity: Post, binding: Db.Post, create: createPost, update: updatePost }],
  transaction: Transaction.statements, // one connection; use Transaction.drizzle for Postgres
  policy: Policy,
  authors: [G.isAuthor],
})

cms gives you cms.sources, cms.queries, and cms.mutations to spread into RemoteServer.make, plus cms.due for scheduled releases and cms.import for seeding existing content. Visitors reading Post through any query or relation see only rows where publishedAt is non-null, while authors see all rows and working drafts.