June 22, 2023

Prisma Client Extensions Are Now Production Ready

With the release of version 4.16.0 of the Prisma ORM, Client extensions are now Generally Available! This new feature allows users to extend and customize Prisma Client to meet their individual use cases.

Prisma Client Extensions

Tailor Prisma Client to meet your codebase's needs

In 4.7.0, we released Prisma Client extensions as a Preview feature. Today we are happy to announce the General Availability of Prisma Client extensions! Extensions have proven to be extremely useful and powerful during the Preview period, even powering Prisma products like Accelerate and Pulse!

A straightforward and easy to use API

If this is the first time you're hearing about Client extensions, don't worry. We have an existing blog post that covers the usage in-depth. To sum it up here: creating an extension is as easy as using $extends.

This code snippet shows how you can add a new method to the User model using a model extension:

If you instead require a method on all models, you can even use the builtin $allModels feature:

For a more in-depth look into changes we made to the extensions API as a part of this release, please check out our release notes

Extensions built by the community

While client extensions are now generally available, we have already seen some cool examples in the wild. prisma-extension-pagination is an awesome contribution from our community. Importing and using an external client extension is easy too:

Reference examples for various use cases

In addition to community contributions, we have a set of reference examples in the prisma-client-extensions example repository that showcase different areas where we believe Prisma Client extensions can be useful. The repository currently contains the following example extensions:

audit-log-contextProvides the current user's ID as context to Postgres audit log triggers
callback-free-itxAdds a method to start interactive transactions without callbacks
computed-fieldsAdds virtual / computed fields to result objects
input-transformationTransforms the input arguments passed to Prisma Client queries to filter the result set
input-validationRuns custom validation logic on input arguments passed to mutation methods
instance-methodsAdds Active Record-like methods like save() and delete() to result objects
json-field-typesUses strongly-typed runtime parsing for data stored in JSON columns
model-filtersAdds reusable filters that can composed into complex where conditions for a model
obfuscated-fieldsPrevents sensitive data (e.g. password fields) from being included in results
query-loggingWraps Prisma Client queries with simple query timing and logging
readonly-clientCreates a client that only allows read operations
retry-transactionsAdds a retry mechanism to transactions with exponential backoff and jitter
row-level-securityUses Postgres row-level security policies to isolate data a multi-tenant application
static-methodsAdds custom query methods to Prisma Client models
transformed-fieldsDemonstrates how to use result extensions to transform query results and add i18n to an app
exists-fnDemonstrates how to add an exists method to all your models

Show off your extensions!

If you'd like a deeper dive into Prisma Client extensions, be sure to check out our previous write-up: Prisma Client Just Became a Lot More Flexible: Prisma Client Extensions!

We'd also love to hear about your extensions (and maybe even take them for a spin).

Be sure to show of your #MadeWithPrisma work in our Discord

Don’t miss the next post!

Sign up for the Prisma Newsletter