How do I combine two unique fields to make one unique field


#1

So, for instance, if I have a type

type UserCompanyPrivileges {
  id: ID! @id
  user: User!
  company: Company!
  canEditName: Boolean! @default(value:false)
}

A user can have many companies, and a company can have many users, but there will only every be one company to a user.

How do I make it where the combined company and user field are unique so that I can query by the combined unique fields, for instance:

prisma.companyUserPrivileges({
  AND: {
    company: {
      companyname: companyname
    },
    user: {
      username: username
    }
  }
})

and get back a single set of privileges?


#2

I think you want to implement a permission system. You can define an enum that stores all permissions:


enum Permissions {
 EDITNAME
 EDITPROFILE
 ADMIN 
 ORGADMIN
}

type UserCompanyPrivileges {
  id: ID! @id
  user: User!
  company: Company!
  permission: [Permission!]!
}

#3

Yes, I’d prefer to do it the way I had it though, and I have a lot of other use cases where this would be useful.