Litestone guide / Schema
Computed fields
A computed field is a value the schema derives instead of storing it a caller's way. Which attribute you pick decides whether you can filter and sort by it.
model Person {
id Int
firstName String
lastName String?
dueAt DateTime?
doneAt DateTime?
fullName String? (`{firstName} {lastName}`)
overdue Boolean (dueAt < now() && doneAt == null)
}
await db.person.create({ data: { firstName: 'Cher' } })
await db.person.create({ data: { firstName: 'Ada', lastName: 'Lovelace', dueAt: '2000-01-01T00:00:00Z' } })
const names = (await db.person.findMany({ orderBy: { id: 'asc' } })).map(p => p.fullName)
const late = await db.person.findMany({ where: { overdue: true } })
if (names.join('|') !== 'Cher|Ada Lovelace' || late.length !== 1) throw new Error(names.join('|'))
A missing last name takes its separator with it, so Cher has no trailing space. Neither field can be written.
What else it can do
@generated(a * b)is arithmetic a form can preview, with+ - *andround()@generated("sql expr")is SQL, server-only@generated(`...`, stored)materializes the column@derived(expr)is computed in the SELECT, withnow()offsets likenow() + 2d@from(Model, count: true)counts, sums or picks across a relation@computedis a JavaScript function incomputed.js; it cannot be filtered or sortedneeds: [...]on a computed function narrows what it fetches
In depth: Schema § Derived & generated, § @generated(a * b), § @derived(expr)