简体   繁体   中英

How to increment a column of a prisma table by one in nodejs?

It is possible on my javascript application to like posts. And I created an nb_likes column of integer type in the prisma table containing the different posts of the application. I would like when a like is added to a post, the value of the nb_likes column is incremented by 1. So I wrote the following code:

await db.post.update({
         . . .
          nb_likes : {
            increment,
          }
        },
      });      

I expected the value of the nb_likes column to be implicitly increased by 1 but the increment keyword is not recognized. I searched the inte.net, but couldn't find anything about it. Thanks !

With Prisma, you can perform atomic operations for Int and Float fields.

increment: x: Adds x to the current value

decrement: x: Subtracts x from the current value

multiply: x: Multiplies the current value by x

divide: x: Divides the current value by x

set: x: Sets the value to x (equivalent to data: { age: 18 })

You can learn more from the docs

An example is shown

const updatePosts = await prisma.post.updateMany({
  data: {
    views: {
      increment: 1,
    },
    likes: {
      increment: 1,
    },
  },
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM