I wish to be able to enforce uniqueness on a per-u...
# prisma-whats-new
v
I wish to be able to enforce uniqueness on a per-user basis. For example; I have a company type with a name field. Each logged user may have several companies related to them. The name of the company is set by the user on company creation. When running the createCompany mutation I want to check that a name with the specified name has not already been created by that user. In an SQL database I would’ve used a composite primary key to make this kind of uniqueness but in graphcool I don’t know how to do it. Suggestions?
d
There are two options for this. Right now, you can use a Request Pipeline function to apply the constraint. You'd want to use the Transform_Argument pipeline step, as this is triggered before anything is written to the database
When schema extensions come out of beta (or if you join the beta) you could write your own custom mutation that includes the logic that you want.
v
How can I join beta? 🙂
d
I'd recommend a pipeline function for this use case though, as you are only adding a constraint on the existing mutation rather than changing the bahviour of it whokesale
you need to ask @nilan for access to the schema extensions beta
Thinking about it, you may also be able to use a permission query to prevent users from adding a company with a name that they have already added. That would be more efficient than using either kind of function.
Your permission query (on create of Company and requiring authentication) would look something like this:
query ($input_name: String!, $user_id: ID!) { SomeUserExists( filter: { id: $user_id, companies_none:{ name: $input_name } } ) }
v
a create permission query, right. Need to refresh my memory of how those work
aha!
d
So bascially we are saying that for a company to be added, there must exist a user with the same Id as the current user who has no company with a matching name
v
Yup, I’m testing right away. Brb 🙂
Yay, that seems to do it!! Much easier than I believed it to be! Thanks a lot dankent!
d
no worries