Still unable to get Optimize working in my NestJS app.

Hi everyone. I would really like to try Optimize, and have been trying for some time. See similar discussion here https://discord.com/channels/937751382725886062/1270167207204356178/1275034014025121792 But it does not record anything. There is not a single log. No "errors" logs, no "success" logs. I don't know what's going on. Did Optimize initialize successfully ? Did it error ? Again, using NestJS. Here is current non-working code :
import { Inject, Injectable } from "@nestjs/common";
import type { ConfigType } from "@nestjs/config";
import { PrismaClient } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";
import appConfig, { appConfigFeature } from "../config/app.config";

@Injectable()
export class PrismaService extends PrismaClient {
constructor(
@Inject(appConfigFeature.KEY)
private readonly appConfiguration: ConfigType<typeof appConfig>,
) {
super();
this.$extends(withOptimize({
apiKey: this.appConfiguration.PRISMA_OPTIMIZE_KEY,
}))
try {
if (
this.appConfiguration.NODE_ENV === "production" ||
this.appConfiguration.NODE_ENV === "development"
) {
const extendedClient = this.$extends(withOptimize({
apiKey: this.appConfiguration.PRISMA_OPTIMIZE_KEY,
}));
Object.assign(this, extendedClient);
}
this.user.findMany()
} catch (error) {
console.error(error)
}
}
import { Inject, Injectable } from "@nestjs/common";
import type { ConfigType } from "@nestjs/config";
import { PrismaClient } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";
import appConfig, { appConfigFeature } from "../config/app.config";

@Injectable()
export class PrismaService extends PrismaClient {
constructor(
@Inject(appConfigFeature.KEY)
private readonly appConfiguration: ConfigType<typeof appConfig>,
) {
super();
this.$extends(withOptimize({
apiKey: this.appConfiguration.PRISMA_OPTIMIZE_KEY,
}))
try {
if (
this.appConfiguration.NODE_ENV === "production" ||
this.appConfiguration.NODE_ENV === "development"
) {
const extendedClient = this.$extends(withOptimize({
apiKey: this.appConfiguration.PRISMA_OPTIMIZE_KEY,
}));
Object.assign(this, extendedClient);
}
this.user.findMany()
} catch (error) {
console.error(error)
}
}
Is there a method to know which middlewares have been added ? like this.prisma.listActiveMiddlewares() or something ? So that I could troubleshoot, debug, try things to know where the problem comes from ? Like does it come from my implementation or from Prisma, or from the Class ? Cheers.
7 Replies
Nurul
Nurul4d ago
Hey @Virgile! I was able to reproduce this issue with a NestJS project and have already reported it to Optimize team. Here's a related thread where I shared the reproduction: https://discord.com/channels/937751382725886062/1288624617165946943/1288624617165946943 I'll provide you with an update this week with next steps. If it's a bug with Optimize, then we will work on fixing it. If it's not a bug, we will update the documentation with steps on using Optimize with NestJS. Thank you for your patience and assistance while we work on finding the solution 🙏 @Virgile 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. @Virgile Just checking if you had a chance to try out the suggested solution. Let us know in case you still observe any issues.
ThallesComH
ThallesComH4d ago
Hey, I'm trying to instantiate it like this due to typescript typings but still no luck
import { PrismaClient } from '@siteapi/database'
import { Injectable, OnModuleInit } from '@nestjs/common'
import { pagination } from 'prisma-extension-pagination'
import { withOptimize } from '@prisma/extension-optimize'

@Injectable()
export class PrismaService implements OnModuleInit {
public readonly client = new PrismaClient()
.$extends(pagination())
.$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY! }))

async onModuleInit() {
await this.client.$connect()
}
}
import { PrismaClient } from '@siteapi/database'
import { Injectable, OnModuleInit } from '@nestjs/common'
import { pagination } from 'prisma-extension-pagination'
import { withOptimize } from '@prisma/extension-optimize'

@Injectable()
export class PrismaService implements OnModuleInit {
public readonly client = new PrismaClient()
.$extends(pagination())
.$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY! }))

async onModuleInit() {
await this.client.$connect()
}
}
I'm on a monorepo if that helps
Nurul
Nurul3d ago
What happens if you temporarily remove the pagination extension and only use the optimize one. Do you still see no queries in optimize dashboard?
Anubhav@12
Anubhav@122d 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.
Nurul
Nurul2d ago
@Anubhav@12 Do you mind sharing link to your repository? I would like to share it with our Optimize team
Anubhav@12
Anubhav@122d ago
Its a private repo but i will try to make a reproducible example of this repo in a public repo then i will share the link
Nurul
Nurul2d ago
That would be very helpful 🙏
Want results from more Discord servers?
Add your server