Hey everyone :wave:, been exploring Prisma and I’m...
# orm-help
m
Hey everyone šŸ‘‹, been exploring Prisma and I’m curious if there is a way for me to annotate a new field with Prisma READ: For example, if we have a simple Post, User, & Like table:
Copy code
// prisma.schema
Table Post { likes [], ... }

Table User { likes [], ... }

Table Like { userId, postId, ...}
Is there a way for me to create a new
likedByUser
Boolean field (Not part of the Post Schema) like this:
Copy code
prisma.post.findMany({
  where: {..}
  select: {
    likedByUser: { // New annotated field
       where: { 
         likes: [
           { userId: { equals: 2 } },
         ]
       }
    }
  }
});
For more context, I’m trying to optimize for performance. An alternative is to loop through each Post and fire a single query which can be slow.
Copy code
// 1 query
posts = await prisma.post.findMany(..);

// n queries
forEach post:
  const likedByUser = await prisma.like.findFirst({ where: post: post.id, userId: 2 });
r
You cannot create annotated/virtual fields not part of the schema as of now. Have a look at this request and it would be great if you could add a šŸ‘ so that we know the priority for this šŸ™‚
m
Gotcha, thanks for pointing me there Ryan šŸ™‚
šŸ™Œ 1