Martïn
02/04/2021, 3:43 PMtokenGenerationCount that I need to increment +1 every time logic generates the access_token, but it doesn’t seems to work. Below is the schema:
model userSessionInfo {
id String @id @default(cuid())
userId String @unique
deviceSignature String @unique
...
tokenGenerationCount Int @default(0)
}
And here’s the code to update:
const updateTokenCount = await prisma.userSessionInfo.update({
where: {
id: “sh3883ejJdiwoQ”
},
data: {
tokenGenerationCount: {
increment: 1
}
}
})
It fails to increment tokenGenerationCount without any error.
The example in the doc only provides sample code for updateMany method:
const updatePosts = await prisma.post.updateMany({
data: {
views: {
increment: 1,
},
likes: {
increment: 1,
},
},
})Ryan
02/04/2021, 4:33 PMconst session = await prisma.userSessionInfo.create({
data: { deviceSignature: 'ds', userId: 'uid' },
})
console.log(session)
const updatedSession = await prisma.userSessionInfo.update({
where: {
id: session.id,
},
data: {
tokenGenerationCount: {
increment: 1,
},
},
})
console.log(updatedSession)
And here’s the output:Martïn
02/04/2021, 9:26 PMdeviceSignature to the where argument
const updatedSession = await prisma.userSessionInfo.update({
where: {
id: <http://session.id|session.id>,
deviceSignature: ‘ds’
},
data: {
tokenGenerationCount: {
increment: 1,
},
},
})
console.log(updatedSession)
I only want to update based on the user device agent string or deviceType in my case.Ryan
02/05/2021, 6:30 AMupdateMany as you are specifying multiple arguments. update only expects a single unique argument in the where clause. You can read more about that here 🙂Martïn
02/06/2021, 7:04 PMuserId, country, email, profileId and `rating.`Now I want to update the rating field in just one row that matches this condition where the userId is “Ryan”, email is “ryan@prisma.io” AND company is “Prisma” only. So userId must be “Ryan” and company must be “Prisma” to be able to update the rating field. We can have multiple users like Nikolas working also for Prisma, but only Ryan meets this condition. Somewhere in the doc (Combining operators) listed OR and NOT. Something like AND is needed in this case if not already in view:
const result = await prisma.user.update({
where: {
AND:[
{ userId: "ryan"},
{ email: "<mailto:ryan@prisma.io|ryan@prisma.io>" },
{ company: "Prisma" }
]
},
data: {
rating: {
increment: 1
}
})
MongoDB supports this operations for single and multi-row updates like this:
db.user.find(
{
"_id" : "ryan",
"email":"<mailto:ryan@prisma.io|ryan@prisma.io>",
"company": "Prisma"} ,
{
$inc:
{ rating: 1 }
}
)Martïn
02/07/2021, 10:44 PM