Query plan adapters convert the output from the CerbosDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/cerbos/cerbos/llms.txt
Use this file to discover all available pages before exploring further.
PlanResources API into database-specific query filters. This enables efficient filtering of large datasets at the database level based on what a principal is authorized to access.
Overview
ThePlanResources API returns a query plan that describes which resources a principal can access. Query plan adapters translate this abstract plan into concrete database queries for your ORM or query builder.
When to Use Query Plan Adapters
Use query plan adapters when you need to:- Filter lists or search results based on authorization
- Implement pagination for authorized resources
- Generate database queries that respect access control
- Avoid loading all resources into memory for permission checks
Available Adapters
Prisma Adapter
Convert query plans to Prisma query filters for TypeScript/JavaScript
SQLAlchemy Adapter
Convert query plans to SQLAlchemy filters for Python
How Query Plans Work
When you callPlanResources, Cerbos evaluates your policies and returns one of three plan types:
1. Unconditional Allow
All resources of the specified kind are allowed. No filtering is needed.2. Unconditional Deny
No resources of the specified kind are allowed.3. Conditional
Only resources matching specific conditions are allowed.WHERE owner = 'user123').
Prisma Adapter Example
Installation
Usage
- Calls
PlanResourcesto get the query plan - Converts the plan to a Prisma
whereclause - Executes the filtered query
- Returns only authorized records
Advanced Filtering
Combine authorization filters with application filters:SQLAlchemy Adapter Example
Installation
Usage
- Calls
PlanResourcesto get the query plan - Converts the plan to SQLAlchemy filter expressions
- Returns a modified query with authorization filters applied
- You can further refine the query before execution
Combining Filters
Query Plan Mapping
Query plan adapters map Cerbos expressions to database operations:| Cerbos Operator | Database Equivalent | Example |
|---|---|---|
eq | = or == | owner = 'user123' |
ne | != or <> | status != 'deleted' |
in | IN | role IN ('admin', 'editor') |
lt | < | age < 18 |
lte | <= | price <= 100 |
gt | > | score > 50 |
gte | >= | year >= 2024 |
and | AND | public = true AND active = true |
or | OR | owner = 'user123' OR public = true |
not | NOT | NOT flagged = true |
Performance Considerations
Database Indexes
Ensure your database has indexes on columns used in authorization conditions:Query Complexity
Complex policies result in complex queries. Monitor query performance and optimize policies if needed:- Avoid deeply nested conditions when possible
- Use simple equality checks for better index usage
- Consider denormalizing authorization-relevant attributes
Caching Query Plans
For frequently-used queries with stable permissions, consider caching the query plan:Building Custom Adapters
If your ORM or query builder isn’t supported, you can build a custom adapter:- Call the
PlanResourcesAPI - Parse the returned query plan
- Translate conditions to your query builder’s syntax
- Apply the filters to your base query