Hubert Kowalski
09/12/2022, 10:01 PMmodel Wallet {
user User
balance Int
transactions Transaction[]
}
model Transaction {
id Int
change Int
wallet Wallet
}
and now, I want to make a query that: creates a Transaction and increments the connected wallet.balance
by the change
, but I have no idea if it's even possible.nikolasburk
prisma.wallet
like so:
const walletId = 42
const changeValue = 100
const updatedWallet = await prisma.wallet.update({
where: {
id: walletId
},
data: {
balance: {
increment: changeValue
},
transactions: {
create: {
change: changeValue
}
}
}
})
nikolasburk
Transaction
object to be returned as well you can use include
in the query:
const updatedWallet = await prisma.wallet.update({
where: {
id: walletId
},
data: {
balance: {
increment: changeValue
},
transactions: {
create: {
change: changeValue
}
}
},
include: {
transactions: true
}
})
Hubert Kowalski
09/13/2022, 8:34 AMnikolasburk
Hubert Kowalski
09/13/2022, 6:43 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 a Transaction and:
1. decrements balance of fromWallet by change
if specified
2. increments balance of toWallet by change
3. and actually returns TransactionHubert Kowalski
09/14/2022, 8:11 AM