P
Prisma3mo ago
Virgile

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.
16 Replies
Nurul
Nurul3mo 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
ThallesComH3mo 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
Nurul3mo 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@123mo 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
Nurul3mo ago
@Anubhav@12 Do you mind sharing link to your repository? I would like to share it with our Optimize team
Anubhav@12
Anubhav@123mo 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
Nurul3mo ago
That would be very helpful 🙏
Virgile
VirgileOP2mo ago
Hi @Nurul (Prisma) . Got the same issue as @Anubhav@12 on my main monorepo. On a repro monorepo, it does show the data (when calling prisma inside the prisma service, after extending Optimize)
No description
Virgile
VirgileOP2mo ago
Here is the repro. Check the Loom : https://www.loom.com/share/ba72e31200d447528d5700289f07ef0d Github Repo : https://github.com/Varkoff/remix-nestjs-monorepo/tree/feature/prisma-optimize (on feature/prisma-optimize branch) If you install this repo, you will have to create the .env file in the root folder and the backend folder. Also pinging you @Marc Hess because you asked me how you could help with Optimize. This is how 🙂
Loom
Optimize Usage in Monorepo with NestJS and Linux 🚀
Hello team! In this video, I demonstrate how I utilize Optimize in a Monorepo using NestJS and Linux. I showcase integrating Prisma service, making database calls, and troubleshooting API call detection issues. No specific action requested from viewers.
GitHub
GitHub - Varkoff/remix-nestjs-monorepo at feature/prisma-optimize
Remix App With NestJS Adapter. Contribute to Varkoff/remix-nestjs-monorepo development by creating an account on GitHub.
Nurul
Nurul2mo ago
@Virgile Thank you so much for taking the time to share the repository and the loom recording with us. I am sharing it with our Optimize team to get insights on why queries are not showing up in a monorepo setup 🙏
millsp
millsp2mo ago
Hey @Virgile thanks for your reproduction, it was really helpful. We uncovered a bug for which I have built a fix for. Before we release it, I just want to make sure that everything works as expected. Here are some changes you'll need to make: 1. Install the development version to try it out (make sure not to include any caret in the version ^):
"@prisma/extension-optimize": "0.0.0-dev.202411151707"
"@prisma/extension-optimize": "0.0.0-dev.202411151707"
2. Remove Object.assign workaround, as in my tests it does not work as described. Instead to this:
function ExtendedPrismaClient() {
const client = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.PRISMA_OPTIMIZE_API_KEY ?? "" })
);

return class {
constructor() {
// biome-ignore lint/correctness/noConstructorReturn: <explanation>
return client;
}
} as new () => typeof client;
}

@Injectable()
export class PrismaService extends ExtendedPrismaClient() {}
function ExtendedPrismaClient() {
const client = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.PRISMA_OPTIMIZE_API_KEY ?? "" })
);

return class {
constructor() {
// biome-ignore lint/correctness/noConstructorReturn: <explanation>
return client;
}
} as new () => typeof client;
}

@Injectable()
export class PrismaService extends ExtendedPrismaClient() {}
You will need this workaround until https://github.com/prisma/prisma/issues/18628 is implemented. This drawback specifically impacts Nest.js. With the workaround, full type safety should be preserved. We will likely release this fix early next week, in the mean time let me know if you have any feedback. I don't have a timeline, however, on the specific issue I have linked above. Thanks! Feel free to reach out if you have any questions Hey @Virgile I just wanted to let you know we have released the fix.
Virgile
VirgileOP5w ago
Thank you man. Was looking forward to it 🙂
Virgile
VirgileOP5w ago
You have released it ? I don't see an updated Prisma version yet
No description
Virgile
VirgileOP5w ago
Using version "@prisma/extension-optimize": "^1.0.3"
millsp
millsp2w ago
Hey @Virgile apologies for the late reply. There was a compounded issue that we didn't account for, specific to Nest. I have tried it within your project and was able to record queries. You'll need to update to the latest version.
No description
Virgile
VirgileOP2w ago
Thanks for the reply ! I’ve stopped using NestJS on the company project, very excited about trying Prisma Postgres with all these features builtin
Want results from more Discord servers?
Add your server