Skip to main content
Prisma Documentation Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Add methods to Prisma Client (Prisma ORM v7)

Extend the functionality of Prisma Client, client component

You can use the client Prisma Client extensions component to add top-level methods to 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:

TypeScript
const prisma = new PrismaClient().$extends({
  client?: { ... }
})

The following example uses the client component to add two methods to Prisma Client:

  • $log outputs a message.
  • $totalQueries returns the number of queries executed by the current client instance.
TypeScript
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);
}
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu