Writing and Drafts
Server code writes content with the same instance it reads from. Every write checks the data against your schema and runs your hooks, the same as a save in the admin.
const post = await cms.create("posts", { title: "Hello world" });
Create, update and delete
Four methods change content:
// A new document, published at once
const post = await cms.create("posts", { title: "Hello world", category: categoryId });
// Change some fields. The others keep their values.
await cms.update("posts", post.id, { featured: true });
// Delete it
await cms.delete("posts", post.id);
// Change a global
await cms.setGlobal("footer", { copyright: "© 2026 Example" });
Use them in seed scripts, imports from other systems, server routes and hooks.
updateandsetGlobalchange only the top-level fields they receive.nullclears a field. A group you send replaces the stored group.createandupdatereturn the stored document. Relations hold ids, not loaded documents.updatereturnsundefinedwhen the document does not exist.- Writes take
localefor a localized collection.
Invalid data
A write with invalid data throws a ValidationError from tenoncms and stores nothing. errors holds one message per field path:
import { ValidationError } from "tenoncms";
try {
// `title` is required
await cms.create("posts", {});
} catch (error) {
if (error instanceof ValidationError) {
// { title: "<message>" }
console.log(error.errors);
}
}
Deletes and references
When you delete a document, every relation that points to it applies its onDelete rule. A restrict rule stops the delete with a ConflictError. See Relations.
Links in rich text and link fields protect their target too. A delete that would break such a link throws a LinkedContentError that lists the documents with the link. To delete anyway, pass allowBrokenLinks:
await cms.delete("posts", post.id, { allowBrokenLinks: true });
Access rules
The instance you import skips the access rules. Your server code is trusted. To write as the user of a request, use cms.withSession(request):
const tenon = await cms.withSession(request);
await tenon.update("posts", id, { title: "New title" });
Its writes apply the rules for that user. A field that the user may not change keeps its stored value, and the input for it is dropped.
Drafts
Every collection and global has drafts, unless it sets drafts: false. A document then has a published version, and may have a pending draft next to it. Visitors see only the published version.
Writes publish by default. { draft: true } saves a draft instead and keeps the published data:
// A new document that is not live yet
const post = await cms.create("posts", { title: "Coming soon" }, { draft: true });
// Change the draft
await cms.update("posts", post.id, { body: "…" }, { draft: true });
// Make the draft live
await cms.publish("posts", post.id);
// Drop the draft
await cms.discard("posts", post.id);
- A draft save checks only the value types.
required,pattern,min,max,validate,uniqueand relation ids wait for the publish. publishruns every check of a normal save on the draft. If one fails, it throws aValidationErrorand the draft stays.discarddrops the draft and returns the published document. A document that was never published is deleted, and the result isundefined.afterChangehooks run on a publish, not on a draft save. Hooks getdraft: trueon a draft save.- Every document has
publishedAt, which isnullwhen it was never published, andhasDraft.
Globals have the same methods: setGlobal(name, data, { draft: true }), publishGlobal(), unpublishGlobal() and discardGlobal().
To read drafts, see Reading.
drafts: false, every save is live at once. { draft: true }, unpublish, discard and schedule then throw a ValidationError. The media library has no drafts.Unpublish
unpublish takes a document offline and keeps its content:
await cms.unpublish("posts", post.id);
// Live again
await cms.publish("posts", post.id);
- A pending draft stays. Without one, the published data becomes the draft, so nothing is lost.
- Its unique values, like the slug, stay reserved. A later publish does not fail because another document took the slug.
- In a localized collection,
{ locale }takes one language offline. The document goes offline with its last published language.
Schedule
schedule: true on a collection or global lets editors plan a publish or an unpublish for a later time. A built-in job runs it. See Scheduled publishing.
Duplicate
duplicate copies a document, with every language, into a new draft:
const copy = await cms.duplicate("posts", post.id);
The title gets (copy). A slug or another unique text gets -copy, then -copy-2 and so on, so the copy saves without edits.
Edit conflicts
Two people can change the same document at once. Without a check, the last save wins. To catch this, send the updatedAt you loaded:
const post = await cms.withDrafts().findById("posts", id);
await cms.update("posts", id, { title: "New title" }, { draft: true, updatedAt: post?.updatedAt });
If someone saved since, the write throws a ConflictError with code: "CHANGED", and changes nothing. publish, unpublish, setGlobal, publishGlobal and unpublishGlobal take updatedAt too. Over HTTP, it is the ?updatedAt= query, and the answer is 409.
The admin sends updatedAt with every save. It also lets one person edit a document at a time. These locks only guide the admin. The API does not enforce them.
Version history
Every document and global keeps its past versions, per language. Editors open them in the admin, under … → History, and can restore one.
- A publish, and every save without drafts, adds a version. An unpublish adds one with the content that went offline.
- The draft saves of one editor update the same version until 30 minutes pass without a save, or someone else saves.
- A restore writes the version back as the draft, through a normal write, so access rules, hooks and validation apply.
- Each document keeps its newest 100 versions per language. Set
maxVersionson the collection or global to change this, orInfinityto keep all.
Server code records no user on a version.
From the browser
The client has the same writes. Draft saves have their own methods there:
| Local API | Client |
|---|---|
create(name, data, { draft: true }) | createDraft(name, data) |
update(name, id, data, { draft: true }) | updateDraft(name, id, data) |
setGlobal(name, data, { draft: true }) | setGlobalDraft(name, data) |
publish, unpublish, discard, schedule and duplicate have the same names. Every write over HTTP needs a session or an API key, unless an access rule allows it.
Related resources
- Hooks: run code on every write.
- Live preview: show drafts on the real page.
- Local API: every method and its options.