Does the field mapper for prisma understand relati...
# help
n
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
There’s a
relationMapper
argument here that might solve what you need
a
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
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
Hey @Norberto Cáceres can you share the relevent snippet of your prisma schema?
n
Here it is
a
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
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
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
yes
a
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
will do
it's working
🎉
a
Excellent. I’ll clean up the tests a bit and cut a proper release shortly
n
awesome, thanks
a
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