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.

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:

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient().$extends({
model: {
user: {
async signUp(email: string) {
// code for the new method goes inside the brackets
},
},
},
});
// The new method can then be used like this:
const newUser = await prisma.user.signUp('myemail@email.com');

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

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient().$extends({
model: {
$allModels: {
async exists<T>(
this: T,
where: Prisma.Args<T, 'findFirst'>['where']
): Promise<boolean> {
// code for the new method goes inside the brackets
},
},
},
});
// You can now invoke `exists` on any of your models. For example:
const postExists = await prisma.post.exists({ id: 1 });

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:

import { PrismaClient } from '@prisma/client';
import pagination from 'prisma-extension-pagination';
const prisma = new PrismaClient().$extends(pagination);
const [users, meta] = prisma.user
.paginate({
select: {
id: true,
}
})
.withPages({
limit: 10,
});

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:

ExampleDescription
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-methodDemonstrates 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 Slack or Discord

Don’t miss the next post!

Sign up for the Prisma Newsletter

Key takeaways from the Discover Data DX virtual event

December 13, 2023

Explore the insights from the Discover Data DX virtual event held on December 7th, 2023. The event brought together industry leaders to discuss the significance and principles of the emerging Data DX category.

Prisma Accelerate now in General Availability

October 26, 2023

Now in General Availability: Dive into Prisma Accelerate, enhancing global database connections with connection pooling and edge caching for fast data access.

Support for Serverless Database Drivers in Prisma ORM Is Now in Preview

October 06, 2023

Prisma is releasing Preview support for serverless database drivers from Neon and PlanetScale. This feature allows Prisma users to leverage the existing database drivers for communication with their database without long-lived TCP connections!