Remote / Declared writes
Pinning guards to the written revision
Checking permissions in one query and updating in another leaves a race window if ownership changes in between. Pass { revision: 'revision' } to GuardServer.writer so the update statement pins the exact revision the guard inspected (see examples/writes/src/server.ts):
import { GuardDrizzle } from 'foldkit-guard/drizzle'
import { GuardServer } from 'foldkit-guard/server'
const Db = bind(
{ Product },
{ Product: { table: products, visible: GuardDrizzle.visible(Policy, [Product]).Product } },
)
const guarded = GuardServer.writer(Product, Policy, writer(Db.Product), { revision: 'revision' })The visible clause filters rows by read permissions before guards evaluate. If the row changes between the guard check and the SQL update, GuardServer.writer re-evaluates up to three times before failing with "changed while it was written". GuardServer.validate reports unpinned guarded writes as UnpinnedWrites.
Guard rules that cross to another row (such as team membership) are forwarded to the writer as requires clauses inside the SQL WHERE so they evaluate atomically on commit; see Rules that read another row.
Refusals evaluate in strict security order so callers learn nothing about rows they cannot access:
- Hidden rows or failed guard rules return
Forbidden. - Stale
expectrevisions returnConflict. - Failed
whenpreconditions returnUnmet.
Writes whose when or expect inspects a field hidden by a Read rule are rejected at boot; see Guard's Serving writes.