https://cerbos.dev logo
#help
Title
# help
n

Norberto Cáceres

06/27/2023, 6:17 PM
Does the field mapper for prisma understand relationships? from my testing it seems not but just wanted to check if there is a workaround. So I have a field "request.resource.attr.authz.creatorId" which maps not to an intrinsic property of the model I am querying but to "authz.creatorId" where "authz" is a Prisma virtual field representing the 1:1 relationship between the models. So ideally I would pass a string with dot-notation as "authz.creatorId" but it seems like the query planner takes it literally and expects a column named like that to exist on the queried model.
1
m

Matthew Ebeweber

06/27/2023, 6:19 PM
There’s a
relationMapper
argument here that might solve what you need
a

Alex Olivier (Cerbos)

06/27/2023, 6:20 PM
Hey there is a 3rd parameter called
relationMapper
- there are some examples in the tests which are a starting point https://github.com/cerbos/query-plan-adapters/blob/main/prisma/src/index.test.ts#L455
n

Norberto Cáceres

06/27/2023, 6:39 PM
What I am doing wrong here? "authz" is the Prisma virtual field that represents the 1:1 relation between the model I want to query and another table. The planner is producing these filters
{"authz.creatorId":{"equals":"userId"}}
when I would expect something like
{"authz": {"creatorId": {"equals": "userId"}}}
Meaning it's not nesting
creatorId
within
authz
it still assumes that the property is called
authz.creatorId
I've also tried just as above but passing an empty object for
fieldNameMapper
. But that results in the following filters
{"request.resource.attr.authz.creatorId":{"equals":"userId"}}}
The expected filters for my use case would be: { authz: { creatorId: "userId" } } For an action only the "api_key_owner" can perform where said derived role is defined as below
a

Alex Olivier (Cerbos)

06/28/2023, 5:15 PM
Hey @Norberto Cáceres can you share the relevent snippet of your prisma schema?
n

Norberto Cáceres

06/28/2023, 5:17 PM
Here it is
a

Alex Olivier (Cerbos)

06/28/2023, 5:20 PM
thanks looking at this now
Ok i think i know what is happening - the relation mapper was designed around one level of nesting - this case is a couple of relations
n

Norberto Cáceres

06/28/2023, 5:38 PM
How come this is considered multiple relations? ApiKey is the resource I am trying to create a query plan for and it has a 1:1 with ApiKeyAuthz
basically I want all the ApiKey's where the creatorId of their respective ApiKeyAuthz instance is the principal id, that would be the expected query
a

Alex Olivier (Cerbos)

06/28/2023, 6:08 PM
Yeah I see what you after. Just to check is your prisma query would be something like this:
Copy code
await prisma.ApiKey.findMany({
    where: { 
       authz: {
         creatorId: "..."
       }  
    },
  });
n

Norberto Cáceres

06/28/2023, 6:15 PM
yes
a

Alex Olivier (Cerbos)

06/28/2023, 7:36 PM
I've published it under a pre-release
@cerbos/orm-prisma@1.1.0-alpha.1
- could you try it out for your use case when you have a chance?
n

Norberto Cáceres

06/28/2023, 8:39 PM
will do
it's working
🎉
a

Alex Olivier (Cerbos)

06/28/2023, 9:27 PM
Excellent. I’ll clean up the tests a bit and cut a proper release shortly
n

Norberto Cáceres

06/28/2023, 9:27 PM
awesome, thanks
a

Alex Olivier (Cerbos)

06/28/2023, 9:28 PM
Prisms Extensions have just gone GA which might be a good fit for this
Hey @Norberto Cáceres just to let you know this has now been published under
1.1.0