Litestone guide / Protected data
Soft delete
@@softDelete turns remove() into stamping a deletedAt column. Every read then hides the row, and restore() brings it back.
model Post {
id Int
title String
deletedAt DateTime?
}
await db.post.create({ data: { id: 1, title: 'Hello' } })
await db.post.remove({ where: { id: 1 } })
const live = await db.post.count()
const all = await db.post.count({ withDeleted: true })
const trashed = await db.post.findMany({ onlyDeleted: true })
if (live !== 0 || all !== 1 || trashed.length !== 1) throw new Error('remove should hide the row, not delete it')
await db.post.restore({ where: { id: 1 } })
if (await db.post.count() !== 1) throw new Error('restore should bring it back')
The model must declare an optional deletedAt, and a schema without one is refused at parse.
What else it can do
findMany,countandexiststakewithDeleted: trueoronlyDeleted: truedelete()still removes the row physically@@softDelete(cascade)stamps every foreign-key child that is soft-deleting too@hardDeleteon a relation field removes that child's rows when the parent is removed@keepon a relation field leaves the children live on purpose, and silences the footgun warningrestore()reverses the cascade, bringing back only the children it stamped- A soft-deleted row keeps its
@uniquevalues, and reusing one raisesSoftDeletedUniqueErrorwith a 409 - Releasing a value is a choice: update the deleted row, or
delete()it for good - An
@@indexon the model is partial to live rows, and a@uniqueis not @@deny(delete, ...)can require a state before removal, becauseremove()honors row policies but not the state machine- A
driver traillog filesremoveas adeleteentry with the row asbefore asSystem()does not lift the soft-delete filter
In depth: Soft delete, § Cascading soft delete, § @keep on a relation field, § @unique and a deleted row