Hey all! I'm working on a side project for keeping...
# orm-help
b
Hey all! I'm working on a side project for keeping track of favorite quotes. I've got a question around updating a field using Prisma. I'm wanting to make sure that
userId
of the currently logged in user matches the
quoteId
that the user is looking to update/delete. This is what I'm currently doing (and it works). But I'm wondering if there's a way I can do this without multiple calls to the database? Would be great if I could do something like
prisma.update({ where: {id AND userId}})
👈 pseudocode.
Copy code
editQuote: async (_, { ...input }, context) => {
      const { prisma, user } = context;
      const { id, quote, author, categories, favorite } = input;

      const originalQuote = await prisma.quote.findUnique({
        where: { id },
      });

      // Check to see if the logged in user owns this quote before updating
      if (user == originalQuote.userId) {
        const editedQuote = await prisma.quote
          .update({
            where: {
              id: id,
            },
            data: {
              quote,
              author,
            },
          })
          .catch((e) => {
            throw e;
          });
        return editedQuote;
      } else {
        console.log("You are not authorized to edit this quote");
        return null;
      }
    },
s
You can simplify it a bit by doing this:
Copy code
if(await prisma.quote.count({ where: { id, userId: user })) {
  return await prisma.quote.update.....
}

return null;
But what you're looking for is directly updating IF multiple conditions are met. That is discussed here.
🙌 1
a
You could use
updateMany
with both the
id
and
userId
conditions, and check the result to see if any rows were updated.
👍 1
👍🏼 1
b
Thank you @Samrith Shankar and @Andre Oliveira!