Drafts of every record, previewed on the real page

Migration 077 keeps one draft per record of any entity. Saving a draft
checks it by applying it in a transaction that is rolled back; Preview
opens the record's page in a tab that sends X-Draft-Preview, and the
server answers those reads from a second connection holding the applied
draft, then rolls back, so nobody else sees it. Publish is the ordinary
save, which drops the draft. tx() now nests as a savepoint.

Also commits TODO.md: the answered questions, and parts 1 to 3 moved to
Done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aidan Dona 2026-10-16 18:21:53 -05:00
parent e4c7e165d1
commit a6c66d4607
13 changed files with 576 additions and 65 deletions

View file

@ -6,7 +6,11 @@
GET /api/admin/:entity/:id row with side tables and children
POST /api/admin/:entity create
PATCH /api/admin/:entity/:id replace the row and its children
(publishing a draft too: it's dropped)
DELETE /api/admin/:entity/:id
GET /api/admin/:entity/:id/draft its draft, or null (drafts.js)
PUT /api/admin/:entity/:id/draft save the form as its draft
DELETE /api/admin/:entity/:id/draft discard it
Mounted alongside the existing admin router, which keeps the
feedback routes. Both sit under the same requireAuth.
@ -29,6 +33,7 @@ import {
deleteRow,
} from "../admin-crud.js";
import { NO_STORE } from "../shape.js";
import { draftIds, dropDraft, readDraft, saveDraft } from "../drafts.js";
const entities = new Hono();
@ -84,7 +89,8 @@ entities.get("/options", (c) => {
entities.get("/:entity", (c) => {
const entity = entityOr404(c);
const { rows, total } = listRows(c.get("db"), entity, c.req.query());
return c.json({ rows, total }, 200, NO_STORE);
// Which of them have a draft waiting, for the list to mark.
return c.json({ rows, total, drafts: draftIds(c.get("db"), entity.key) }, 200, NO_STORE);
});
entities.get("/:entity/:id", (c) => {
@ -103,6 +109,8 @@ entities.patch("/:entity/:id", requireRole("editor"), async (c) => {
const entity = entityForWrite(c);
const id = c.req.param("id");
const row = visible(c, entity, updateRow(c.get("db"), entity, id, await json(c)));
// Saved for real: any draft of it is published, or overtaken.
dropDraft(c.get("db"), entity.key, id);
console.log(`${entity.key} ${id} updated by ${c.get("user").email}`);
return c.json({ row }, 200, NO_STORE);
});
@ -111,10 +119,34 @@ entities.delete("/:entity/:id", requireRole("admin"), (c) => {
const entity = entityOr404(c);
const id = c.req.param("id");
deleteRow(c.get("db"), entity, id);
dropDraft(c.get("db"), entity.key, id);
console.log(`${entity.key} ${id} deleted by ${c.get("user").email}`);
return c.body(null, 204);
});
/* ── Drafts (drafts.js) ──────────────────────────────────────── */
entities.get("/:entity/:id/draft", (c) => {
const entity = entityOr404(c);
return c.json({ draft: readDraft(c.get("db"), entity.key, c.req.param("id")) }, 200, NO_STORE);
});
entities.put("/:entity/:id/draft", requireRole("editor"), async (c) => {
const entity = entityForWrite(c);
const id = c.req.param("id");
const draft = saveDraft(c.get("db"), entity.key, id, await json(c), c.get("user").id);
console.log(`${entity.key} ${id} draft saved by ${c.get("user").email}`);
return c.json({ draft }, 200, NO_STORE);
});
entities.delete("/:entity/:id/draft", requireRole("editor"), (c) => {
const entity = entityForWrite(c);
const id = c.req.param("id");
dropDraft(c.get("db"), entity.key, id);
console.log(`${entity.key} ${id} draft discarded by ${c.get("user").email}`);
return c.body(null, 204);
});
async function json(c) {
try {
return await c.req.json();