Hi! Is there a correct way to validate that the us...
# orm-help
p
Hi! Is there a correct way to validate that the user has permissions to modify an entry? I'm a app that has a products CRUD Each product belongs only to one user. Which will be the correct way to validate that the product that will be modified, belongs to the user? I'm currently fetching the product, validating the user is correct, and then updating the product. But can this be done in the same update mutation? Something like this?
1
r
I think the problem is update can only run on a unique field,
updateMany
should work with what you have..
hi @Pedro Bonamin - you could also write what you have as:
Copy code
prisma.product.updateMany({
  where: {
    userId: userId,
    id: req.query.id
  }
});
there's an implicit
AND
between those ..
p
Gotcha! Makes sense! Thanks
It's bad that we can't do it by just using the update, which makes more sense for the type of action
y
You can always check it before-hand if the update semantic is important or more ergonomic
Copy code
let product = await prisma.product.findFirst({where: { userId, id }})
if (!product) throw new NotFoundError()

product = await prisma.product.update({ where: {id}, data: input})