Guard / foldkit-guard

Serving reads

foldkit-guard/server connects your rules to foldkit-remote-server while keeping server code out of browser bundles:

ts
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 Read are absent from by-ID lookups, queries, and relations (one refs become null). Inherited Read rules evaluate the related row's rules (cycles evaluate as refused).
  • Members or relations refused by Read are withheld (Unavailable) or replaced with their redact value, while the fields those rules inspect are still fetched to decide them.
  • Unlisted Entities remain public; custom Sources must use GuardServer.memory or GuardDrizzle.visible + GuardServer.filter to pass GuardServer.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):

ts
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 Read rules become project_id in (select id from projects where …) subqueries.
  • GuardDrizzle.visible validates at build time that inherited targets are listed with Read rules 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 } to GuardDrizzle.visible(Policy, [Todo], { operators }) when rules use custom primitive operators so they match your bind table.
  • visible enforces row Read rules; GuardServer.filter enforces member reads, relation reads, and may.

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:

ts
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 error

GuardServer.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.