Guard / How Guard works
How a request is decided in two steps
When a request arrives, your server resolves the signed-in user once and hands that principal to Policy. Policy resolves every permission in two steps:
- Decide principal guards once. The server runs each
guard.principal()body for that user and replaces it withAlways(allowed) orNever(denied). - Simplify what remains into a
Condition. AConditionis a tagged boolean tree (_tag: 'Always' | 'Never' | 'Row' | 'And' | 'Or'). Inside an array of guards (logical OR), anyAlwayscollapses the whole rule toAlways, and anyNeverdrops out.
Attach rules to an Entity with Guards.attach and ask Policy.readable what remains for an admin versus a member:
ts
const Note = NoteFields.pipe(Guards.attach(G, { row: { Read: [G.isAuthor, G.isAdmin] } }))
const forAdmin = Effect.runSync(
Policy.readable(Note, Option.some({ id: 'lin', role: 'admin' })),
) // { _tag: 'Always' }
const forMember = Effect.runSync(
Policy.readable(Note, Option.some({ id: 'ada', role: 'member' })),
) // { _tag: 'Row', guard: G.isAuthor, ... }For Lin the admin, G.isAdmin becomes Always, so Read collapses to { _tag: 'Always' } and SQL adds no extra WHERE clause. For Ada the member, G.isAdmin becomes Never and disappears, leaving only G.isAuthor, which compiles to WHERE author_id = ?.
For a signed-out visitor (Option.none()), every row guard that reads a placeholder field of user collapses to Never. A row guard that reads only columns of the row (such as published = true) still runs for visitors.