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
- Download the quick-start project
- Paste in the above code snippet
- Run
yarn dev
- 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
- fix extending prisma client #23 https://github.com/prisma/prisma/issues/11986#issue-1147401285 — committed to le-ar/nestjs-prisma by le-ar 2 years ago
- fix extending prisma client #23 (#30) https://github.com/prisma/prisma/issues/11986#issue-1147401285 — committed to notiz-dev/nestjs-prisma by le-ar 2 years ago
@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:
but you need to have something like this instead:
@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:
Hey, @smitssjors you’ll have to explicitly pass generic parameters to
PrismaClient
when extending it for this code to work:This is the way extending generic classes works in TS, unfortunately, it can’t infer the parameters from
super
callI get this same issue when I try to use
$extends
on thePrismaClient
; the issue is, it’s difficult to define thePrismaClient
type when using$extends
, so I opt for something like this:But then, I am left with the OP error:
for this code:
Can be worked around by following this: https://github.com/prisma/prisma/issues/4746#issuecomment-750949556