import type { AppContext } from '@voltro/runtime' // Write the incoming CRDT update to the `body` column. The handler looks like a // plain overwrite — but `body` is a `crdtText()` column, so the runtime's // MutationStore intercepts the write: it reads the STORED state and folds the // incoming update in with `mergeCrdtStates` (from @voltro/local-first) BEFORE // persisting. That authoritative server merge is what makes concurrent edits // converge; the reactive engine then broadcasts the merged row to every // subscriber. This handler stays a one-liner precisely because the convergence // lives in the write path, not here. // No `if (row === null)` branch here, and that is deliberate. `documents` is // `.with(tenant())`, so a keyed write the caller may not perform does not come // back as `null` — the store THROWS `TenantRowNotFound`, identically for "no // such row" and "someone else's row". Re-checking for `null` would be dead code, // and answering it with a 404 would undo the point: two different answers for // the two cases turn any keyed write into a cross-tenant existence oracle. // // The typed error reaches the client as itself (`_tag`, payload and prototype // intact), so a `catchTag('TenantRowNotFound')` on the caller is the handling // site — not a hand-rolled status here. const execute = async ( input: { id: string; update: Uint8Array }, ctx: AppContext, ) => ctx.store.update('documents', input.id, { body: input.update }) export default execute