Skip to main content
Version: 2.0 🚧

Runtime API Reference

This document provides references for runtime APIs exported from the @zenstackhq/runtime package.

enhance​

Description​

Creates an enhanced wrapper for a PrismaClient. The return value has the same APIs as the original PrismaClient.

Signature​

function enhance<DbClient extends object>(
prisma: DbClient,
context?: WithPolicyContext,
options?: EnhancementOptions
): DbClient;
Parameter prisma​

The PrismaClient instance to enhance.

Parameter context​

The context to for evaluating access policies with the following typing.

type EnhancementContext = {
user?: Record<string, unknown>
};
FieldDescription
userThe user object that provides value for the auth() function call in access policies. If provided. Its shape should be consistent with the User model in your ZModel, with all fields optional except for id field(s). Pass undefined to represent an anonymous user, and the auth() function call will evaluate to null in that case.
Parameter options​

Options with the following typing.

type TransactionIsolationLevel =
| 'ReadUncommitted'
| 'ReadCommitted'
| 'RepeatableRead'
| 'Snapshot'
| 'Serializable';

type EnhancementOptions = {
kinds?: EnhancementKind[];
logPrismaQuery?: boolean;
errorTransformer?: ErrorTransformer;
transactionMaxWait?: number;
transactionTimeout?: number;
transactionIsolationLevel?: TransactionIsolationLevel;
};
FieldDescriptionDefault
kindsThe kinds of enhancements to apply. By default all enhancements are applied. Supported values: "policy", "password", "omit", "delegate".All enhancement kinds
logPrismaQueryWhether to log queries sent to Prisma client. Log will be emitted with "info" level, so please make sure you turn that level on when creating Prisma clientfalse
errorTransformerA function for transforming error thrown by the enhanced PrismaClient into a custom one.
transactionMaxWaitThe maxWait option (in ms) passed to prisma.$transaction() call for transactions initiated by ZenStack.Database default
transactionTimeoutThe timeout option (in ms) passed to prisma.$transaction() call for transactions initiated by ZenStack.Database default
transactionIsolationLevelThe isolationLevel option passed to prisma.$transaction() call for transactions initiated by ZenStack.Database default

Example​

const session = getSession();
const enhancedClient = enhance(prisma,
{ user: session.user },
{ kinds: ['policy', 'password']}
);