question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`$on` argument type error when extending prisma client

See original GitHub issue

Bug description

When extending the PrismaClient class it is not possible to pass the query event type to prisma.$on() even though the correct options are passed to the PrismaClient in the super() call.

class Test extends PrismaClient {
  constructor() {
    super({
      log: [
        {
          emit: "event",
          level: "query",
        },
      ],
    });

    this.$on("query", (e) => {
      console.log(e);
    });
  }
}

The code above produces the following error:

TS2345: Argument of type '"query"' is not assignable to parameter of type '"beforeExit"'.

How to reproduce

  1. Download the quick-start project
  2. Paste in the above code snippet
  3. Run yarn dev
  4. See error

Expected behavior

The query parameter is valid.

Prisma information

Nothing relevant

Environment & setup

  • OS: Ubuntu (WSL on Windows 11)
  • Database: SQLite
  • Node.js version: 16.14.0

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0

Also happens on

prisma                  : 3.9.2
@prisma/client          : 3.9.2
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : bcc2ff906db47790ee902e7bbc76d7ffb1893009
Studio                  : 0.457.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:13
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
aqrlncommented, Jun 11, 2022

@r1si the problem is mostly likely in your definition of the Global type.

I suppose you probably have a definition that looks like this in your code:

interface Global {
  // ....
  prisma: PrismaClient
}

but you need to have something like this instead:

interface Global {
  // .....
  prisma: PrismaClient<Prisma.PrismaClientOptions, 'info' | 'warn' | 'error'>
}
2reactions
SevInfcommented, May 6, 2022

@smitssjors in that case, I think, you should be able to specify all events during compile time and then just subscribe to the ones you need in run time:

class MyClient extends PrismaClient<Prisma.PrismaClientOptions, 'query' | 'info' | 'warn' | 'error'> {
  ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma Client API (Reference)
API reference documentation for Prisma Client. ... 1export type LogLevel = 'info' | 'query' | 'warn' | 'error' ... This argument is optional....
Read more >
Prisma/Typescript: how to extend client? - Stack Overflow
I implemented such class: import { PrismaClient } from '@prisma/client'; export type NotStartsWith< S, Prefix extends string, > = S extends ...
Read more >
prisma is not assignable to type - You.com | The AI Search ...
Environment & setup · Bug description. A TS error is thrown when orderBy clause is defined as a variable and passed into findMany...
Read more >
Prisma plugin for Pothos GraphQL
prismaField({ type: 'User', resolve: async (query, root, args, ctx, info) => prisma.user.findUniqueOrThrow({ // the `query` argument will add in `include`s or ` ...
Read more >
Dynamic return type based on input parameter in TypeScript ...
Prisma does a good job of type safety After using TypeORM for many ... User } from "@prisma/client"; type PostGetPayload<S extends Prisma.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found