NestJS with Optimize not working
Hey everyone! I am currently looking to add Optimize to my NestJS app. Can you guys help me out figuring out why I am not receiving anything in my optimize?
I am using the following libraries and version:
-
"@prisma/client": "^6.4.0",
- "prisma": "^6.4.0",
- "@prisma/extension-optimize": "^1.1.4",
- "@prisma/instrumentation": "^6.4.0",
I know I don't have to use the preview anymore in my prisma schema.
Here is my prisma.service.ts
:
3 Replies
Skipping coffee for instant knowledge! Let's brew that AI answer now.
Based on the information provided in the knowledge sources, I can see a potential issue with your implementation of Prisma Optimize in your NestJS application. The problem likely stems from how you're extending the PrismaClient with the Optimize extension. Let me explain the issue and provide a solution.
The main problem is in the constructor of your PrismaService. You're calling
this.$extends()
after the super()
call, which doesn't actually modify the instance. Instead, it returns a new extended client that you're not using.
Here's how you can modify your prisma.service.ts
to correctly implement Prisma Optimize:
This approach creates an extended PrismaClient instance with Optimize before the class is instantiated, and then returns this instance in the constructor. This ensures that all methods called on PrismaService
will use the extended client with Optimize enabled.
Additionally, make sure that your PRISMA_OPTIMIZE_API_KEY
environment variable is correctly set with a valid API key.
If you're still not receiving any data in Optimize after making these changes, ensure that you've started a recording session in the Optimize dashboard before running your queries. As mentioned in the Getting Started guide, you need
to start a recording, run your app and execute some Prisma queries, and then stop the recording to see the insights.
If you continue to face issues after implementing these changes, you might want to check if there are any conflicts with other extensions or middleware, as mentioned in the Getting Started guide.
š¤ Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.i ran into the same issue with Pulse. workarounds were discussed here https://github.com/prisma/prisma/issues/18628
GitHub
Ability to extend
PrismaClient
class w/ Client Extensions before ...Problem Within NestJS, the common solution to implementing an injectable Prisma Client instance is to extend PrismaClient and add the onModuleInit and enableShutdownHooks functions required by Nest...
Thanks for chiming in š
Let me know if you are still unable to get Optimize running