Litestone guide / Access
The Warden
The Warden is the access system: everything that decides whether a call may touch a row or a column. It is declared in the schema and enforced at the Data boundary, so a service, a job and an agent all meet the same rules.
model User { id Int (4) }
model Note {
id Int (autoincrement())
ownerId Int (auth().id)
body String
(1.4.4.5)
(read, ownerId == auth().id)
}
const ann = db.$setAuth({ id: 1, role: 'user' })
const bob = db.$setAuth({ id: 2, role: 'user' })
await ann.note.create({ data: { body: 'mine' } })
const annSees = await ann.note.findMany()
const bobSees = await bob.note.findMany()
const system = await db.asSystem().note.findMany()
if (annSees.length !== 1 || bobSees.length !== 0 || system.length !== 1) throw new Error('the row policy did not apply')
try { await db.note.findMany(); throw new Error('a stranger read the table') } catch (e) { if (e.name !== 'AccessDeniedError') throw e }
A call passes down the layers in order: the principal, the gate, a capability, the row policy, then the field rules. A layer that reads no row throws, and a layer that reads the row filters.
What else it can do
db.$setAuth(user)returns a scoped client, and every rule readsauth()off it@@gategrades the kind of caller on a 0 to 9 ladder, per operation@@allowand@@denygrade whose row it is@@capabilitiesgrades which action, as a grid beside the ladder@guarded,@encrypted,@hashedand a field@allowgrade which columns@@transitionsgrades who may make a moveasSystem()lifts authority and still holds integrity: checks,@immutableand the state machine- Tenancy compiles to
@@deny, so no extra@@allowcan widen it
In depth: Warden, § One call, top to bottom, § asSystem() lifts the Warden and holds integrity