Cascade Deletes with transaction

Hi! Loving Wasp so far. Just one question at this point. How do we take advantage of prisma.$transaction() in actions.ts in order to ensure all deletes are successful? Thank you!
4 Replies
KarateSheets
KarateSheets9mo ago
I should specify this is via the OpenSaas boilerplate.
martinsos
martinsos9mo ago
Hey @KarateSheets , that is a great question! Check this out: https://wasp-lang.dev/docs/data-model/entities#using-entities-directly -> you can get access to prisma by doing import prisma from '@wasp/dbClient' (in docs it is named prismaClient but it is really just prisma. And then you should be able to do:
const [posts, totalPosts] = await prisma.$transaction([
context.entities.Post.findMany({
where: { title: { contains: 'prisma' } }
}),
context.entities.Post.count(),
])
const [posts, totalPosts] = await prisma.$transaction([
context.entities.Post.findMany({
where: { title: { contains: 'prisma' } }
}),
context.entities.Post.count(),
])
Notice I used context.entities.Post instead of prisma.post, because I assumed you are inside of Operation (be it Action or Query) and have that available. If not, you can even go with prisma.post, although context.entities.Post is a bit more "by the book", in Wasp world.
Entities | Wasp
Entities are the foundation of your app's data model. In short, an Entity defines a model in your database.
KarateSheets
KarateSheets9mo ago
Thank you! This is perfect :).
martinsos
martinsos9mo ago
Great @KarateSheets , glad to hear that :)!
Want results from more Discord servers?
Add your server