Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Module

$use

$use was Prisma Client’s middleware API for code around queries, but it is deprecated and removed in current Prisma ORM. Use the client’s middleware array and named lifecycle hooks instead.

How it works

$use was Prisma Client’s middleware-registration method for running custom code around queries, but it is deprecated and unavailable in current Prisma ORM. Current documentation describes a middleware array supplied during client setup; middleware objects declare a name and lifecycle hooks instead of using a next() wrapper.

Syntax

prisma.$use(middleware)

Returns

No return value is documented for $use; current middleware is configured as part of client creation.

Key points

  • The API provides application-wide query interception, allowing one policy to apply consistently across the client’s queries.
  • Define middleware as a named object containing the lifecycle functions the client should invoke.
  • Registration happens during client construction through the middleware option, rather than by calling $use afterward.
  • For Prisma ORM 7, replace $use registration with an array of middleware objects.

Common mistakes

  • Do not migrate current Prisma ORM 7 code by retaining $use; register middleware through the client’s middleware option.

Example: Register query middleware

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient({
  middleware: [
    {
      name: "log",
      async beforeQuery(plan) {
        console.log(plan.sql);
      },
    },
  ],
});

await prisma.user.findMany();
await prisma.$disconnect();

Creates a Prisma ORM 7 client with a middleware object that logs SQL before a query runs.

When to use it

Apply one policy around queries throughout an application

Centralize query policies

Log query plans or SQL before execution

Observe database activity

Replace `$use` registration with client-setup middleware

Migrate legacy Prisma code

Interview questions

QWhat was Prisma Client $use, and what replaces it?

$use was the older Prisma Client API for registering middleware around queries. In current Prisma ORM, it is replaced by a middleware array passed when the client is created.

Follow-up: How would you adapt middleware that used one function before and after a query? Split the behavior into the appropriate before-query and after-query hooks, because the current API has no next() wrapper.

QWhat does a current Prisma middleware contain?

A middleware is a plain object with a name and one or more hook functions. The client invokes those hooks at defined points around a query.

Follow-up: How is registration performed? Put the middleware object in the middleware array in the client setup options.

QWhat should an application do with existing $use code?

$use is deprecated and was removed from Prisma Client in Prisma ORM 6.14.0. Prisma Client extensions were previously recommended as its replacement, while current Prisma ORM 7 documentation uses the middleware client option.

Follow-up: What should a migration target be for the stable Prisma ORM 7 API? Use a middleware object in the client setup rather than calling $use.

  • middleware
  • beforeQuery
  • afterQuery
  • PrismaClient
  • Prisma Client extensions

Source: How middleware works in Prisma ORM. Checked 2026-10-16.