DEV Community

Cover image for It's Prisma Time - Update
Luca Del Puppo for This is Learning

Posted on

It's Prisma Time - Update

Hi Folks 👋
Today it's time to learn something about the update operation, so don't waste time and let's go!

The update method in Prisma is not that different from the insert. Let's give an example.
Starting from the code of the previous article, you can remove all the code relative to the delete operation and after the insert you can add the following code.

const updatedAuthor = await prisma.author.update({
  data: {
    firstName: "Updated first name",
    lastName: "Updated last name",
  },
  where: {
    id: authors[0].id,
  },
});
console.log({ updatedAuthor });
Enter fullscreen mode Exit fullscreen mode

As you can see, the update method used the patter prisma.[entity].update, not so different from the insert and the delete, obviously the update method updates an existing row. And another thing, if the update method doesn't find the record, it throws an exception that you have to handle in your code.
If you execute that code you get this result.

{
  updatedAuthor: {
    id: 3,
    firstName: 'Updated first name',
    lastName: 'Updated last name'
  }
}
Enter fullscreen mode Exit fullscreen mode

I think this operation is easy to understand, but now let me show you some special features that the update method exposes.
In some cases, when we want to update a row, we don't want to set a new value, but we want to increment, decrement, multiply or divide a field in a atomic update operation. To do this Prisma exposes us these commands in the type IntFieldUpdateOperationsInput

export type IntFieldUpdateOperationsInput = {
  set?: number
  increment?: number
  decrement?: number
  multiply?: number
  divide?: number
}
Enter fullscreen mode Exit fullscreen mode

and we can use it in this way

const updatedAuthor = await prisma.author.update({
  data: {
    id: {
      increment: 1
    }
  },
  where: {
    id: authors[0].id,
  },
});
Enter fullscreen mode Exit fullscreen mode

Obviously, incrementing the primary key doesn't have sense, but this is just a demonstrative example.
Let's go on and see another important feature exposed by the update operation, to work with the relative collections.
In the insert operation, we saw that when we want to insert a new record, we can use create, connect or connectOrCreate operation to create a relation between two records. In the update operation in addition we have:

  • upsert: update or create a related record
  • set: connect the record with an existing records, (if you set this field with an empty you disconnect all record)
  • disconnect: remove the relation with existing records
  • delete: delete the record in relation with it
  • update: update the related record
  • updateMany: update the related records
  • deleteMany: delete the related records

I let you the official document for each type, so you can deepen if you are interested.

As you can imagine, over the update operation there is the updateMany operation. In this article I prefer not to look into this operation because I have already left you many info about the update. But let me spend some words about it, so you can try it by yourself.
The updateMany operation combine the where seen in the deleteMany operation with the features explained above, so it's obvious to understand that this operation allows you to update many records, using a complex filter.

It's all for today guys.
In the next article it's time to see the select operation.

See you soon
Bye Bye 👋

You can find the code of this article here

Top comments (2)

Collapse
 
amgoun profile image
amgoun

help: how can update a post with prisma based on 2 condtion belongs to 2 diffrent tabales ; the user id from user table and post id from post table,here is the issu instackoverfloww if somone can help thank you

Collapse
 
amgoun profile image
amgoun