Litestone guide / Querying
Writing rows
create, update, upsert and remove each write one row and return it. The Many forms return { count }. An explicit null clears a field; a key left out is untouched.
model Contact {
id Int
email String
name String
phone String?
views Int (0)
}
const c = await db.contact.create({ data: { email: 'ann@example.com', name: 'Ann', phone: '555-0100' } })
const cleared = await db.contact.update({ where: { id: c.id }, data: { phone: null } })
if (cleared.phone !== null || cleared.name !== 'Ann') throw new Error('null should clear phone and leave name')
const same = await db.contact.update({ where: { id: c.id }, data: { name: undefined, views: { increment: 1 } } })
if (same.name !== 'Ann' || same.views !== 1) throw new Error('undefined is omitted, increment is atomic')
await db.contact.upsert({ where: { email: 'ann@example.com' }, create: { email: 'ann@example.com', name: 'New' }, update: { name: 'Annie' } })
const bulk = await db.contact.updateMany({ where: { name: 'Annie' }, data: { name: 'Ann' } })
if (bulk.count !== 1) throw new Error('updateMany returns a count')
await db.contact.delete({ where: { id: c.id } })
if (await db.contact.count() !== 0) throw new Error('row should be gone')
What else it can do
createreturns the inserted row, andcreateManyreturns{ count }updatereturnsnullwhen nothing matchedupdateMany,upsertMany,removeManyanddeleteManyreturn{ count }onlyupsertManytakes aconflictTargetand the columns toupdateremovesoft-deletes on a@@softDeletemodel, andrestorebrings a row backdeletealways removes the row, even on a soft-delete modelincrement,decrement,multiplyanddividechange a number in one statementpushappends to an array column, and$mergepatches one key of aJsondocument
In depth: Querying § Write operations, § Atomic update operators, § $merge