---
title: "Nested fields support with Prisma Query Plans"
description: "Supporting nested relation fields with the Prisma Query Plan adapter"
author: "Alex Olivier"
date: "2023-07-17T10:00:00.000Z"
canonical: "https://www.cerbos.dev/blog/nested-fields-support-with-prisma-query-plans"
image: "https://stylish-appliance-1c1cc1c30d.media.strapiapp.com/cover_image_69c3604901.jpg"
tags: ["documentation","integration"]
source: "https://www.cerbos.dev/blog/nested-fields-support-with-prisma-query-plans"
---

# Nested fields support with Prisma Query Plans

[Cerbos Query Plans](https://cerbos.dev/blog/filtering-data-using-authorization-logic) address a significant challenge in separating authorization logic from application code. They offer a dynamic solution by generating a set of conditions from policies. These conditions are then used to filter data retrieval, ensuring that only the instances of a resource that a user has permission to access are returned.

To enhance the usability of Query Plans, [several adapters](https://github.com/cerbos/query-plan-adapters) have been developed. These adapters take a plan as input and produce filters in formats compatible with popular Object-Relational Mapping (ORM) systems. For example, in the case of [Prisma](https://www.prisma.io/), the adapters support increasingly complex filters over time, including the recent addition of support for related fields.

If you take a simple Prisma schema like below, you have a parent model which includes a related (nested) model and internally the database joins these two based on the ID field:

```prisma
model Resource {
  id               String         @id @default(cuid())
  nested           NestedResource @relation(fields: [nestedResourceId], references: [id])
  nestedResourceId String
}

model NestedResource {
  id       String     @id @default(cuid())
  aBool    Boolean
  Resource Resource[]
}
```

When there is a related model it is a common scenario where you want to query the parent model, based on the value in a child relation field:

```javascript
const results = await prisma.resource.find({
  where: {
   "nested": {
     "aBool": {
         "equal": true
     }
   }
})
```

With the latest release of the [Prisma Query Plan Adapter](https://github.com/cerbos/query-plan-adapters/tree/main/prisma) any nested attributes in policy will be converted into nested filter conditions also - for example providing a field mapper such a below:

```javascript
const result = queryPlanToPrisma({
   queryPlan,
   fieldNameMapper: {
     "request.resource.attr.nested.aBool": "nested.aBool",
   },
});
```

and the Cerbos policy contains a condition such as:

```yaml
condition:
  match:
    expr: request.resource.attr.nested.aBool == true
```

the adapter will produce a Prisma filter snippet which will now query the nested relational field which can then be passed to Prisma and work as expected.

```json
{
   "nested": {
     "aBool": {
         "equal": true
     }
}
```

You can find out more about Query Plans in the [documentation](https://docs.cerbos.dev/cerbos/latest/api/index.html#resources-query-plan) and our suite of adapters is available [on GitHub](https://github.com/cerbos/query-plan-adapters/tree/main).
