P
Prisma4w ago
Txt

NestJs + Prisma Optimize

Hello everyone! I would like to ask if anyone has managed to integrate a NestJs server with the Optimize tool and how. I have tried this instantiation part in a few ways
const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }),
);
const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }),
);
such as adding it to the prismaService, using useValue and useFactory in the module and even instantiating it in the function that performs the query. I receive the return log that Optimize is connected "See your Optimize dashboard at: https://optimize.prisma.io", and it does not display any errors, but no information about any query made to the Optimize platform is received. versions
"@prisma/client": "^5.20.0",
"@prisma/extension-optimize": "^1.0.1",
"@prisma/client": "^5.20.0",
"@prisma/extension-optimize": "^1.0.1",
12 Replies
Nurul
Nurul4w ago
Hey @Txt 👋 Can you share the link to your repository if it's open source so that we can debug? Some other users have also reported the same issue where Optimize wasn't working with a NestJS app.
Txt
Txt4w ago
Hey @Nurul (Prisma) , thanks for the reply, Unfortunately, the project is private, so I can only share some snipped codes. But I can create a simple POC based on this project to share, and maybe I’ll find something as well. As soon as I have it, I’ll share it here, but if you have any tips on implementing NestJS with Optimize, they’re welcome. Oh, and one more thing, I tested by adding the Accelerator, and the caching system worked fine, everything's normal on the platform, but nothing happened with Optimize.
Nurul
Nurul4w ago
A simple POC would be great 🙏
Eric
Eric3w ago
Same issue here, also using Nest.js Have you confirmed it works with Nest.js @Nurul (Prisma) ?
Simon
Simon3w ago
I am having the same issue with Next.js (not Nest.js) server components. The same code works fine when invoking via node, but in Next.js routes everything is setup but nothing is sent to the dashboard. Nevermind, I figured it out: On next.js it does not work in combination with Next.js's own instrumentation https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation 😦
Nurul
Nurul3w ago
@Eric Not yet, Is your repository open source? Can you share the link?
Eric
Eric3w ago
No it's private. I don't do much different from the official prisma <> nestjs page, so would be easy to test: https://docs.nestjs.com/recipes/prisma
Documentation | NestJS - A progressive Node.js framework
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Eric
Eric3w ago
If prisma optimize does as advertised, we will most likely be a paying customer. But right now completely blocked
Nurul
Nurul3w ago
Thanks for confirming. I'll try creating an example app with NestJS + Prisma today and check if I can reproduce this. I'll share the findings with our Optimize team and get back to you with an update. I have created this reproduction and can see that the queries are not showing in the Optimize Dashboard. https://github.com/nurul3101/Optimize-NestJS I have shared this with our team and will get back with an update.
Eric
Eric3w ago
thank you for looking into this @Nurul (Prisma) , really appreciate it! Looking forward to hear what the team say
Nurul
Nurul2w ago
@Eric @Txt We were able to make Optimize work with Optimize. Can you instantiate PrismaClient in OnModuleInit like this: https://github.com/nurul3101/Optimize-NestJS/blob/main/src/prisma.service.ts This would make sure that queries are displayed in Optimize Dashboard.
GitHub
Optimize-NestJS/src/prisma.service.ts at main · nurul3101/Optimize-...
Contribute to nurul3101/Optimize-NestJS development by creating an account on GitHub.
Anubhav@12
Anubhav@123d ago
@Nurul (Prisma) I have also facing the same issue on my backend, I am using Nest.js, Prisma and swagger. This is my databaseService code
@Injectable()
export class DatabaseService extends PrismaClient implements OnModuleInit {
constructor( private configService: ConfigService ) {
super()
}

async onModuleInit() {
const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: this.configService.getOrThrow('OPTIMIZE_API_KEY') })
)

Object.assign(this, prisma);

await this.$connect();
console.log('Database initialized!');
}
}
@Injectable()
export class DatabaseService extends PrismaClient implements OnModuleInit {
constructor( private configService: ConfigService ) {
super()
}

async onModuleInit() {
const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: this.configService.getOrThrow('OPTIMIZE_API_KEY') })
)

Object.assign(this, prisma);

await this.$connect();
console.log('Database initialized!');
}
}
I'm getting the log that see your optimize dashboard but not getting anything in the prisma optimize dashboard when running the api's in the swagger.
Want results from more Discord servers?
Add your server