Add methods to Prisma Client (Prisma ORM v7) (/docs/orm/v7/prisma-client/client-extensions/client)
For the complete Prisma documentation index, see llms.txt. A markdown version of any docs page is available by appending
.mdto its URL.
Extend the functionality of Prisma Client, client component
Location: ORM > v7 > Prisma Client > Client Extensions > Add methods to Prisma Client
You can use the client Prisma Client extensions component to add top-level methods to Prisma Client.
Extend Prisma Client
Section titled “Extend Prisma Client”Use the $extends client-level method to create an extended client. An extended client is a variant of the standard Prisma Client that is wrapped by one or more extensions. Use the client extension component to add top-level methods to Prisma Client.
To add a top-level method to Prisma Client, use the following structure:
const prisma = new PrismaClient().$extends({
client?: { ... }
})Example
Section titled “Example”The following example uses the client component to add two methods to Prisma Client:
$logoutputs a message.$totalQueriesreturns the number of queries executed by the current client instance.
let total = 0;
const prisma = new PrismaClient().$extends({
client: {
$log: (s: string) => console.log(s),
async $totalQueries() {
return total;
},
},
query: {
$allModels: {
async $allOperations({ query, args }) {
total += 1;
return query(args);
},
},
},
});
async function main() {
prisma.$log("Hello world");
const totalQueries = await prisma.$totalQueries();
console.log(totalQueries);
}Related pages
Section titled “Related pages”Add custom fields and methods to query results: Extend the functionality of Prisma Client, result componentAdd custom methods to your models: Extend the functionality of Prisma Client, model componentCreate custom Prisma Client queries: Extend the functionality of Prisma Client, query componentShared packages & examples: Explore the Prisma Client extensions that have been built by Prisma and its communityShared Prisma Client extensions: Share extensions or import shared extensions into your Prisma project