The following example logs the time taken for await next(params) to run:

script.ts
1const prisma = new PrismaClient();
2
3prisma.$use(async (params, next) => {
4 const before = Date.now();
5
6 const result = await next(params);
7
8 const after = Date.now();
9
10 console.log(
11 `Query ${params.model}.${params.action} took ${after - before}ms`
12 );
13
14 return result;
15});
16
17const create = await prisma.post.create({
18 data: {
19 title: "Welcome to Prisma Day 2020",
20 },
21});
22
23const createAgain = await prisma.post.create({
24 data: {
25 title: "All about database collation",
26 },
27});

Example output:

Query Post.create took 92ms
Query Post.create took 15ms

The example is based on the following sample schema:

generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Post {
authorId Int?
content String?
id Int @default(autoincrement()) @id
published Boolean @default(false)
title String
user User? @relation(fields: [authorId], references: [id])
language String?
@@index([authorId], name: "authorId")
}
model User {
email String @unique
id Int @default(autoincrement()) @id
name String?
posts Post[]
extendedProfile Json?
role Role @default(USER)
}
enum Role {
ADMIN
USER
MODERATOR
}
Edit this page on GitHub