Hubert Kowalski
09/14/2022, 2:38 PMmodel Wallet {
user User
balance Int
incomingTransactions Transaction[] @relation("incomingTransaction")
outgoingTransactions Transaction[] @relation("outgoingTransaction")
}
model Transaction {
id Int
change Int
toWallet Wallet @relation(name: "incomingTransaction")
fromWallet Wallet? @relation(name: "outgoingTransaction")
}
how can I create a query that creates the transaction and:
1. decrements balance of fromWallet by change
if specified
2. increments balance of toWallet by change
3. and actually returns Transaction in the end
Couldn't figure it out on my own 😕
Should I maybe just opt in to use $transaction?Harshit
09/16/2022, 3:05 PM$transaction
here:
const data = await prisma.$transaction(async (tx) => {
const record = await tx.transaction.create({
// data here
});
const updatedRecord = await tx.transaction.update({
where: { id: record.id },
data: { toWallet: { increment: change } },
});
});