Litestone guide / Advanced
Traits
A trait is a reusable fragment of fields and model attributes, spliced into a model with @@trait(T). Nothing outside the parser needs to know it existed.
trait Dates {
createdAt DateTime (now())
updatedAt DateTime
([createdAt])
}
trait SoftDelete {
deletedAt DateTime?
}
model Post {
id Int
title String
(Dates)
(SoftDelete)
}
const post = await db.post.create({ data: { title: 'Hello' } })
if (!post.createdAt || post.deletedAt !== null) throw new Error('expected the trait fields')
await db.post.remove({ where: { id: post.id } })
if ((await db.post.count()) !== 0 || (await db.post.count({ withDeleted: true })) !== 1) throw new Error('expected a soft delete')
Post is exactly what you would get writing the fields inline, so adding a field to a trait changes every model that uses it.
What else it can do
- A trait may hold fields, validators, relations,
@@allow,@@gate,@@trail,@@indexand@@unique - A trait may include other traits
@@createdByand@@updatedBystamp authorship from the principal- A host replaces a trait's field only with
@override - Two traits supplying the same field is a parse error
- Model-level attributes are additive, spliced in declaration order with the host's own last
@id,@@id,@@map,@@dband@@ftsstay with the host and are refused in a traitextend modeladds fields to a model from the other direction
In depth: Traits § Use site, § Conflict resolution, § Nested traits, § extend model