Prisma API: how to check if all `Post`s in an array are authored by a `User`

prisma

#1

Hello again Prisma magicians :sunny:

I have a list of unique Post IDs as input to a resolver, i.e. [PostWhereUniqueInput!], and a UserWhereUniqueInput. I intend to check if all these posts are authored by that User.

I am trying to, but not getting on how to use every,some, andnonefeature from the Prisma API here. Prisma API (generatedindex.js`) has this:

export interface UserWhereInput {
  id?: ID_Input;
  ...
  posts_every?: PostWhereInput;
  posts_some?: PostWhereInput;
  posts_none?: PostWhereInput;
  ...
}

Confsion: I was using some filter, but as I understand, if true, it only guarantees that at least one post of this user is one of these posts, not necessarily all of the input list. My confusion is does true with every will return true only if the input list if an exhaustive list of all posts by the user, whereas I don’t want that. Or, is every my answer?

const ifUserWroteAllThesePosts = await context.prisma.$exists.user({
  id: userId,
  posts_some: {
    id_in: postIDs
  }
})

Also, I understand that I could iterate over each Post ID and achieve the intended, but this part is a nested form in my original code.

Note: Context of my work is authN and authZ, where the goal of this example is exactly similar to my work. If your experience suggests something leaky here, do guide me. Thanks! :slight_smile:


#2

So check if:
NOT SOME ID_NOT

Also I do think every is your answer but not sure.

Thinking the query should be like this

const ifUserDidNotWriteAllThesePosts = await context.prisma.$exists.post({
  id_in: postIDs, # array of ids
  user_not: {
    id: userId
  }
})

Note we are checking negatives


#3

Hello @BenoitRanque Thank you for the answer :slight_smile: This is totally cool. Just that I wanted to use my “generic” function to check with UserWhereInput input. Could we not use the same negation approach with that too, like this:

const ifUserDidNotWriteAllThesePosts = await context.prisma.$exists.user({
  id: userId,
  posts_none: {
    id_in: postIDs
  }
})

Note: I realise that this has the same flaw as before. It doesn’t guarantee the intended.


#4

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.