Reference
Archive vs hard delete
Archive hides a row everywhere and keeps it, reversibly, until retention deletes it; hard delete removes the row and the rows that cascade from it, and can still be undone.
In the todo app, finishing a project shouldn't destroy it: people may want it back. Deleting a single todo, on the other hand, should simply remove it. clientdb supports both.
Archive
Archiving hides a row on every device and keeps it in the database. Mark the model archivable; it gets two fields, archivedAt and archivedBy.
With Prisma, add the two columns to the model and set archivable in fromPrisma:
model Project {
id String @id @default(uuid())
name String
inviteCode String @unique
archivedAt DateTime? @db.Timestamptz(3)
archivedBy String?
members Member[]
todos Todo[]
}import { fromPrisma } from "@clientdb/prisma";
import { prismaModels } from "../generated/clientdb";
export const schema = fromPrisma(prismaModels, {
omit: { User: ["passwordHash"] },
models: {
User: { load: "eager" },
Project: { load: "eager", archivable: true },
Member: { load: "eager" },
Todo: { load: "eager" },
},
});
export type Schema = typeof schema;A model without both columns is a type error that names them. In a TypeScript schema, pass { archivable: true } to model(), which adds the fields itself.
Who may archive is the archive rule, for archiving and unarchiving alike:
// in projectRules, src/sync/permissions/project.ts
archive: {
when: (project) => {
return isAdmin(project);
},
},On the device:
"use client";
import type { Project } from "@/sync/db";
export function ArchiveButton({ project }: { project: Project }) {
if (project.isArchived) {
return <button onClick={() => project.unarchive()}>Restore</button>;
}
return <button onClick={() => project.archive()}>Archive</button>;
}- Default collections hide archived rows:
db.project.allleaves them out,db.project.archived.alllists only them,db.project.includeArchived().allboth. - Archived rows keep syncing, so "Restore" works on every device. A row counts as archived while any row it cascades from (
onDelete: Cascadeinschema.prisma,onDelete: "cascade"in a TypeScript schema) is, so a project's todos and their comments are hidden with it. archivedByis the id of the user who archived the row, andarchivedAtthe server's time; the server sets both from the session and its own clock, whatever the device sent, so retention can't be tricked into deleting a row early. A device that creates or restores a row already archived needs thearchiverule as well ascreate.- Archiving is undoable like any change, and Prisma writes to
archivedAtsync like any other column.
Deleting archived rows after a while
Archived rows are kept until retention.archived on the engine deletes them:
import { createEngine } from "@clientdb/server";
import { permissions } from "./permissions";
export const engine = createEngine({
permissions,
retention: { log: "30d", archived: "90d" },
});engine.prune() (and npx clientdb migrate, after migrating) then deletes rows archived more than 90 days ago, with the rows that cascade from them, and the deletes sync to every device. A row that a restrict relation still points at stays, and is counted in archivedKept. See Retention for running it on a schedule.
Hard delete
const { undoable, unloadedDescendants } = todo.delete();A hard delete:
- removes the row;
- deletes the rows whose relation to it says
onDelete: "cascade"(onDelete: Cascadeinschema.prisma), and theirs in turn; - sets nullable references with
onDelete: "setNull"to null; - fails with
Conflictwhile arestrictrelation points at the row.
On every device that held the row, the delete removes it and its cascading children and leaves a tombstone, so a late update can't bring it back.
Undoing a delete
Undo restores the row and its cascading children with their original ids, and points the references that were set to null back at it. It is only complete if all of them were on this device: comments are lazy, so a todo's comments may not be. delete() reports this as undoable and unloadedDescendants. Pass ensureUndoable to fetch what's missing first, in one request; the row then disappears after that round trip instead of at once:
const { undoable } = await todo.delete({ ensureUndoable: true }); // undoable: trueWhich one to use
| Archive | Hard delete | |
|---|---|---|
| Row stays in the database | yes, until retention.archived | no |
| Still syncs | yes, hidden by default | no |
| Cascading children | count as archived | deleted |
| Undo | always | complete when the children were on the device |
| Permission rule | archive | delete |