Guard / foldkit-guard
Serving reads
foldkit-guard/server connects your rules to foldkit-remote-server while
keeping server code out of browser bundles:
import { GuardServer } from 'foldkit-guard/server'
import { Remote } from 'foldkit-remote'
import { RemoteServer } from 'foldkit-remote-server'
const visitor: Option.Option<typeof User.Type> = Option.none()
const Data = Remote.define({ entities: [Todo, Project] })
const backend = RemoteServer.memory({
domain: Data,
rows: {
Todo: [{ id: 't1', ownerId: 'ada', projectId: 'p1', title: 'Write', project: 'Project:p1' }],
Project: [{ id: 'p1', name: 'Borealis' }],
},
principal: visitor, // reads no todo: every Read rule here needs a principal
...GuardServer.memory(Policy, [Todo]),
})- Rows refused by
Readare absent from by-ID lookups, queries, and relations (onerefs becomenull). InheritedReadrules evaluate the related row's rules (cycles evaluate as refused). - Members or relations refused by
Readare withheld (Unavailable) or replaced with theirredactvalue, while the fields those rules inspect are still fetched to decide them. - Unlisted Entities remain public; custom Sources must use
GuardServer.memoryorGuardDrizzle.visible+GuardServer.filterto passGuardServer.validate.
Filtering Drizzle queries
Over Drizzle, foldkit-guard/drizzle compiles row Read rules into each
binding's visible filter so every query, by-ID read, relation, and count
applies them in SQL (here todos and projects are your Drizzle tables; see
test/drizzle.test.ts for a full SQLite fixture):
import { GuardDrizzle } from 'foldkit-guard/drizzle'
import { bind, source } from 'foldkit-remote-drizzle'
const visible = GuardDrizzle.visible(Policy, [Todo])
const Db = bind(
{ Todo, Project },
{
Todo: {
table: todos,
relations: { project: { field: todos.projectId } },
derived: { may: { supplied: true } }, // computed by `GuardServer.filter`
visible: visible.Todo,
},
Project: { table: projects },
},
)
const TodoSource = source(Db.Todo, { filter: GuardServer.filter(Todo, Policy) })- Principal guards run first in memory; remaining row conditions compile to SQL.
Inherited
Readrules becomeproject_id in (select id from projects where …)subqueries. GuardDrizzle.visiblevalidates at build time that inherited targets are listed withReadrules and free of cycles; inherited relations must store a foreign key on the child table.- Passing a principal value outside
Option<Principal>throws a defect rather than treating it as a visitor. - Pass
{ operators }toGuardDrizzle.visible(Policy, [Todo], { operators })when rules use custom primitive operators so they match yourbindtable. visibleenforces rowReadrules;GuardServer.filterenforces member reads, relation reads, andmay.
Providing services to principal guards
Read seams (GuardDrizzle.visible, GuardServer.memory, GuardSync) require a
Policy with all Effect services already provided at startup, keeping request
context in the principal:
import { Context } from 'effect'
class Memberships extends Context.Service<
Memberships,
{ readonly admins: ReadonlyArray<string> }
>()('app/Memberships') {}
const Checked = G.implement({
isAdmin: principal =>
Effect.map(Memberships, ({ admins }) =>
Option.exists(principal, user => admins.includes(user.id)),
),
})
const ReadPolicy = Checked.provide(Context.make(Memberships, { admins: ['ada'] }))
GuardDrizzle.visible(ReadPolicy, [Todo]) // passing `Checked` directly is a type errorGuardServer.validate(Checked, …) treats ReadPolicy as equivalent to
Checked, while write seams accept Checked directly and resolve its services
per write.
Guarded reads are omitted from edge caches
foldkit-remote-server's serveFetch caches reads at the edge only when one
response applies to every caller. Because guarded Entities attach principal-aware
visible or filter rules, responses for guarded Entities (including row-only
rules) and SSR pages reading them bypass edge caching (no-store). Serve truly
public cached data from an unguarded Entity.