Skip to main content
Version: 2.x

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?: EnhancementContext,
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. See the next section for more details.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

Enhancement Kinds

Here are the kinds of enhancements available:

  • policy

    Enforces model-level and field-level access policies defined with @@allow, @@deny, @allow, and @deny.

  • validation

    Validates create and update input data against rules defined with data validation attributes.

  • delegate

    Support for modeling polymorphic relations with delegated types pattern.

  • password

    Automatically hashes fields marked with the @password attribute using bcryptjs before saving to the database.

  • omit

    Automatically omits fields marked with the @omit attribute from read results.

Example

const session = getSession();
const enhancedClient = enhance(prisma,
{ user: session.user },
{ kinds: ['policy', 'password']}
);
Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below