ConstUser ownership policy - users can only access their own rows
Table name
Operations to allow (default: ALL)
Column containing user ID (default: user_id)
Array of PolicyBuilder instances (one per operation)
// Allow all operations on user's own documents
const [policy] = policies.userOwned('documents');
console.log(policy.toSQL());
// Only allow SELECT and INSERT
const [selectPolicy, insertPolicy] = policies.userOwned(
'documents',
['SELECT', 'INSERT']
);
// Custom user ID column
const [policy] = policies.userOwned('posts', 'ALL', 'author_id');
Tenant isolation policy - users can only access rows from their tenant
Creates a RESTRICTIVE policy that enforces tenant isolation across all operations.
Table name
Column containing tenant ID (default: tenant_id)
Session variable key (default: app.current_tenant_id)
A RESTRICTIVE PolicyBuilder instance
// Basic tenant isolation
const policy = policies.tenantIsolation('documents');
console.log(policy.toSQL());
// Custom tenant column and session key
const policy = policies.tenantIsolation(
'projects',
'org_id',
'app.current_org_id'
);
// Combine with other policies
const tenantPolicy = policies.tenantIsolation('documents');
const [userPolicy] = policies.userOwned('documents', 'SELECT');
// Users see only their docs within their tenant
Public access policy - anyone can read public rows
Creates a SELECT policy that allows reading rows marked as public.
Table name
Column indicating public visibility (default: is_public)
A PolicyBuilder instance for SELECT operations
// Allow reading public documents
const policy = policies.publicAccess('documents');
console.log(policy.toSQL());
// Custom visibility column
const policy = policies.publicAccess('posts', 'published');
// Combine with ownership policy
const publicPolicy = policies.publicAccess('documents');
const [ownerPolicy] = policies.userOwned('documents', 'SELECT');
// Users can see public docs OR their own docs
Role-based access policy - only specific roles can access
Creates policies that grant access to users with a specific role.
Table name
Role name
Operations to allow (default: ALL)
Array of PolicyBuilder instances (one per operation)
Policy template helpers for common patterns