prisma: `$on` argument type error when extending prisma client

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 14
  • Comments: 17 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@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'>
}

@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'> {
  ...
}

Hey, @smitssjors you’ll have to explicitly pass generic parameters to PrismaClient when extending it for this code to work:

class MyClient extends PrismaClient<Prisma.PrismaClientOptions, 'query'> {
  ...
}

This is the way extending generic classes works in TS, unfortunately, it can’t infer the parameters from super call

I get this same issue when I try to use $extends on the PrismaClient; the issue is, it’s difficult to define the PrismaClient type when using $extends, so I opt for something like this:

  private _client: ReturnType<typeof this.makeNewClient>;

But then, I am left with the OP error:

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

for this code:

      this._client.$on("query", (e) => {
        console.log("[Prisma] Query: " + e.query);
        if (LOG_PARAMS) {
          console.log("[Prisma] Params: " + e.params);
        }
        console.log("[Prisma] Duration: " + e.duration + "ms");
      });