Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After adding a prisma client extension it is not possible anymore to define event based logging handlers #23108

Closed
avan2s opened this issue Feb 13, 2024 · 2 comments
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: clientExtensions

Comments

@avan2s
Copy link

avan2s commented Feb 13, 2024

Bug description

It is not possible (since version >=5.0.0) to define a handler for event based logging after adding an extension to the prisma client. The $on method is missing and was working in the 4 major release even with extensions.
prismaClientWithSomeExtension.$on("query", (event) => queryEvents.push(event));

How to reproduce

  1. Create a prisma client of your choice and instantiate it using log events (like in the docs):
const prisma = new PrismaClient({
  log: [
    {
      emit: 'event',
      level: 'query',
    },
    {
      emit: 'stdout',
      level: 'error',
    },
    {
      emit: 'stdout',
      level: 'info',
    },
    {
      emit: 'stdout',
      level: 'warn',
    },
  ],
}).$extends({}); // I just add an empty extension, the issue will be reproducable with any extension

// define the event handler
prisma.$on("query", (event) => console.log(event)); // ERROR $on!

prisma.$on('query', (e) => {
  console.log('Query: ' + e.query)
  console.log('Params: ' + e.params)
  console.log('Duration: ' + e.duration + 'ms')
});
  1. The error is: Property '$on' does not exist on type 'DynamicClientExtensionThis<TypeMap<InternalArgs & { result: {}; model: {}; query: {}; client: { $begin: () => (txOptions?: TransactionOptions | undefined) => Promise<FlatTransactionClient>; }; }>, TypeMapCb, { ...; }>'.ts(2339)

Expected behavior

The $on method can still be used in order to handle logging events, after adding an extension. This is especially useful when writing integration tests in order to asserting queries, that are running against the database.

Prisma information

You can use any schema, i use:

generator client {
  provider        = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model AppUser {
  id        BigInt @id @default(autoincrement())
  firstname String
  lastname  String
  email     String
  posts     Post[]

  @@map("app_user")
}

model Post {
  id             BigInt       @id @default(autoincrement())
  comment        String
  userId         BigInt
  user           AppUser      @relation(fields: [userId], references: [id])

  @@map("post")
}

Tyoescript code reproducing this issue:

const prisma = new PrismaClient({
  log: [
    {
      emit: 'event',
      level: 'query',
    },
    {
      emit: 'stdout',
      level: 'error',
    },
    {
      emit: 'stdout',
      level: 'info',
    },
    {
      emit: 'stdout',
      level: 'warn',
    },
  ],
}).$extends({}); // I just add an empty extension, the issue will be reproducable with any extension

// define the event handler
prisma.$on("query", (event) => console.log(event)); // ERROR $on!

prisma.$on('query', (e) => {
  console.log('Query: ' + e.query)
  console.log('Params: ' + e.params)
  console.log('Duration: ' + e.duration + 'ms')
});

Environment & setup

  • OS: macOS, Debian
  • Database: PostgreSQL
  • Node.js version: v20.10.0

Prisma Version

All versions >=5

^5.0.0
@avan2s avan2s added the kind/bug A reported bug. label Feb 13, 2024
@jkomyno jkomyno added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: extension/query and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. labels Feb 13, 2024
@jkomyno
Copy link
Contributor

jkomyno commented Feb 13, 2024

Hi @avan2s, the behavior you're describing is working as designed (see Docs).

Can you please share with us whether you had found the linked docs page before, and whether the provided explanation is easy to follow? We're happy to hear feedback to potentially make Prisma easier to use :)

@jkomyno jkomyno added the kind/feedback Issue for gathering feedback. label Feb 13, 2024
@avan2s
Copy link
Author

avan2s commented Feb 13, 2024

Hi @jkomyno ,

thanks for your answer and the doc link. This helps. It is easy to follow. Maybe prisma could add the $on example, not only the $use one into the example. But just to be complete :) I was looking in the event based logging section . If there would be a hint, that $on and $use should be handled before extending a prisma client, this would be awesome. I was on your mentioned doc link several month ago, when client extensions were a previewFeature. Maybe this redesign was happening, when it goes into the release.

Thanks again!

@avan2s avan2s closed this as completed Feb 13, 2024
@janpio janpio closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2024
@janpio janpio added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. topic: clientExtensions and removed kind/feedback Issue for gathering feedback. labels Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: clientExtensions
Projects
None yet
Development

No branches or pull requests

3 participants