Model helpers
Add domain-specific operations to a model delegate.
Prisma · Module
Prisma Client’s $extends method creates a new extended client with custom model, client, result, or query behavior. Extensions can be supplied inline or defined separately and then attached.
$extends takes an extension definition or a defined extension and returns a new Prisma Client variant. The definition can add model, client, result, or query behavior; the returned client carries those additions while the original client remains available.
const extendedPrisma = prisma.$extends(extension);| Name | Type | Description |
|---|---|---|
extension | Prisma.Extension | An extension object containing one or more supported components: model, client, result, or query; a reusable extension created with Prisma.defineExtension can also be passed. |
A new extended Prisma Client variant wrapped by the supplied extension or extensions.
$extends supports four extension areas: model methods, client methods, result fields or methods, and query behavior.$extends, or created with Prisma.defineExtension and then supplied to $extends.$extends is a separate extended client, so code using the original client does not automatically receive the added behavior.$extends itself does not imply a particular kind of extension.import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient().$extends({
name: "userHelpers",
model: {
user: {
async findByEmail(email: string) {
return this.findUnique({ where: { email } });
},
},
},
});
const user = await prisma.user.findByEmail("ada@example.com");
console.log(user);Creates an extended Prisma Client with a type-safe method on the User model.
Add domain-specific operations to a model delegate.
Expose cross-model or application-wide operations on the client.
Add computed fields or methods to returned records.
Inspect or modify Prisma query behavior.
$extends do?$extends creates an extended Prisma Client from an existing client. The extension can contain model, client, result, or query components, and the returned client exposes the added behavior.
Follow-up: How can several extensions be combined? Chain $extends calls or associate multiple extensions with one extended client.
A model extension adds methods to one or more Prisma models. For example, a method can wrap an existing model operation and be called through the extended model delegate.
Follow-up: How would you target every model? Use the model extension's $allModels structure rather than repeating the method for each model.
Client-level extensions add methods directly to the Prisma Client object rather than to a model delegate. They are useful for operations that do not belong to one particular model.
Follow-up: How can a reusable extension be organized? Define it with Prisma.defineExtension, then pass the resulting extension to the client-level $extends method.
Prisma.defineExtensionmodelclientresultquery$allModels