Authorization is a critical component of modern applications, ensuring users have the right level of access to data without embedding complex rules into application code. Cerbos’ Query Plan API has long been a valuable tool in this space, enabling dynamic filtering of data based on pre-defined policies.
With our latest update to the reference Prisma Query Plan Adapter, we’ve significantly expanded its capabilities, making it even easier to enforce fine-grained access control within applications using Prisma ORM. In this blog post, we’ll explore the new features, use cases enabled, and how you can start using it today.
The new version of the Cerbos Prisma ORM Adapter introduces several powerful enhancements that make it more flexible and robust.
Previously, the adapter supported basic logical and comparison operators. The new release now adds support for:
startsWith
, endsWith
, contains
, isSet
every
, exists
, exists_one
, all
, filter
hasIntersection
This makes it easier to apply more complex conditions directly in your Prisma queries.
One of the biggest enhancements is full support for deep nested relations, allowing policies to filter data based on attributes from related models.
Previously, filtering based on a related model required additional logic outside the adapter. Now, you can express such conditions naturally within your policies:
condition:
match:
expr: request.resource.attr.nested.aBool == true
With a simple field mapper, this is now seamlessly converted into a Prisma where clause:
const result = queryPlanToPrisma({
queryPlan,
mapper: {
"request.resource.attr.nested.aBool": "nested.aBool",
},
});
The adapter can now automatically infer field names and relationships based on policy expressions. And stronger TypeScript support ensures mappings are type-safe and easier to maintain.
With better support for collections, policies can now check for attributes across multiple related records. This enables more granular enforcement of rules such as:
The internal logic of the adapter has been optimized for efficiency, ensuring that generated Prisma queries remain performant even as complexity increases.
These enhancements open up a range of new use cases for Prisma users integrating Cerbos, such as:
If you're already using Cerbos and Prisma, upgrading to the new adapter is straightforward.
npm install @cerbos/orm-prisma
Integrate the adapter into your Prisma queries:
import { queryPlanToPrisma, PlanKind } from "@cerbos/orm-prisma";
const result = queryPlanToPrisma({
queryPlan, // generated by the Cerbos PDP
mapper: {
"request.resource.attr.owner": {
relation: {
name: "owner",
type: "one"
}
},
"request.resource.attr.status": {
field: "status"
},
},
});
if (result.kind === PlanKind.ALWAYS_DENIED) {
return [];
}
const records = await prisma.resource.findMany({
where: result.filters
});
Upgrade your existing integration or start from scratch with our updated documentation and examples.
For more details, check out the official GitHub repository or join the Cerbos community Slack to discuss best practices and real-world implementations.
Book a free Policy Workshop to discuss your requirements and get your first policy written by the Cerbos team
Join thousands of developers | Features and updates | 1x per month | No spam, just goodies.