Apply one policy around queries throughout an application
Centralize query policies
Prisma · Module
$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.
$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.
prisma.$use(middleware)No return value is documented for $use; current middleware is configured as part of client creation.
middleware option, rather than by calling $use afterward.$use registration with an array of middleware objects.$use; register middleware through the client’s middleware option.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.
Centralize query policies
Observe database activity
Migrate legacy Prisma code
$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.
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.
$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.
middlewarebeforeQueryafterQueryPrismaClientPrisma Client extensions