Litestone guide / Introduction
The client
createClient reads the schema and returns the db object: one accessor per model, each with the same read and write methods.
A request should use a scoped client, db.$setAuth(user), so row policies and field rules see the current user. The unscoped db is for your own code.
model User { id Int } // the principal model: where auth() claims come from
model Note {
id Int
ownerId Int (auth().id)
body String
(read, ownerId == auth().id)
(create, auth() != null)
}
const ann = db.$setAuth({ id: 1 })
const bob = db.$setAuth({ id: 2 })
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() // bypasses policies; use sparingly
if (annSees.length !== 1 || bobSees.length !== 0 || system.length !== 1) throw new Error('policies did not apply')
What else it can do
- Schema source:
path,schema(inline text) orparsed(fromparseFile) dbsets the database file;:memory:is a throwawayencryptionKeyfor@encryptedfieldscomputedsupplies the functions behind@computedfieldsplugins,hooks,onEventandonQueryhook into the clientfiltersapplies a defaultwhereto every read of a modelreadOnly: truerefuses every writeuntrusted: truebuilds a schema that came from data
In depth: Getting started § createClient options, § Auth scoping