P
Prisma3mo 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",
19 Replies
Nurul
Nurul3mo 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
TxtOP3mo 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
Nurul3mo ago
A simple POC would be great 🙏
Eric
Eric3mo ago
Same issue here, also using Nest.js Have you confirmed it works with Nest.js @Nurul (Prisma) ?
Simon
Simon3mo 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
Nurul3mo ago
@Eric Not yet, Is your repository open source? Can you share the link?
Eric
Eric3mo 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
Eric3mo ago
If prisma optimize does as advertised, we will most likely be a paying customer. But right now completely blocked
Nurul
Nurul3mo 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
Eric3mo ago
thank you for looking into this @Nurul (Prisma) , really appreciate it! Looking forward to hear what the team say
Nurul
Nurul3mo 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@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.
tl;dr
tl;dr2mo ago
I have the same issue, and the suggested code doesn't work for me either :). Any updates on this?
Nurul
Nurul2mo ago
@tl;dr Do you mind sharing the link to your GitHub repository?
tl;dr
tl;dr2mo ago
Yes unfortunately, it's a commercial product :/. Is there a way to check from my localhost, if I even reach the Prisma backend for the optimize service? When I go to the UI on https://optimize.prisma.io, theres just nothing comming in. And I have my server running localhost making queries ofc.
tl;dr
tl;dr2mo ago
And I get this, so I guess something is working
No description
Nurul
Nurul2mo ago
It would be great if you can share a minimal reproduction of this issue. I'll be able to then share it with our Optimize team to figure out what's going on.
tl;dr
tl;dr2mo ago
This is my service:
TypeScript
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { from } from 'rxjs';
import { PrismaClient } from '@prisma-types/farm';
import { withOptimize } from '@prisma/extension-optimize';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class PrismaMainMSClient extends PrismaClient implements OnModuleInit {
private readonly logger: Logger = new Logger(`PrismaMainMSClient`);

constructor(
private configService: ConfigService,
) {
super();

// Attempt to disconnect Prisma when server is interrupted
process.on('SIGINT', (): void => {
from(
this.$disconnect(),
)
.subscribe({
next: (): void => {
this.logger.log(`\tPrisma Client disconnected`);
process.exit(0);
},
error: (): void => {
this.logger.error(`\tPrisma Client failed to disconnect!`);
process.exit(1);
},
});
});

}

/**
*
*/
public async onModuleInit(

): Promise<void> {
const customPrismaClient: any = new PrismaClient()
.$extends(
withOptimize({ apiKey: this.configService.getOrThrow('OPTIMIZE_API_KEY') }),
);

Object.assign(this, customPrismaClient);

await this.$connect();

console.log('Database initialized!');
}
}
TypeScript
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { from } from 'rxjs';
import { PrismaClient } from '@prisma-types/farm';
import { withOptimize } from '@prisma/extension-optimize';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class PrismaMainMSClient extends PrismaClient implements OnModuleInit {
private readonly logger: Logger = new Logger(`PrismaMainMSClient`);

constructor(
private configService: ConfigService,
) {
super();

// Attempt to disconnect Prisma when server is interrupted
process.on('SIGINT', (): void => {
from(
this.$disconnect(),
)
.subscribe({
next: (): void => {
this.logger.log(`\tPrisma Client disconnected`);
process.exit(0);
},
error: (): void => {
this.logger.error(`\tPrisma Client failed to disconnect!`);
process.exit(1);
},
});
});

}

/**
*
*/
public async onModuleInit(

): Promise<void> {
const customPrismaClient: any = new PrismaClient()
.$extends(
withOptimize({ apiKey: this.configService.getOrThrow('OPTIMIZE_API_KEY') }),
);

Object.assign(this, customPrismaClient);

await this.$connect();

console.log('Database initialized!');
}
}
` Hmm, I guess I'm going to have to bail on this then :/.. Is there an open issue somewhere I can follow so I can get back to this at some point? It would be too time consuming to begin to pull the app appart etc., and with no guarantee of a positive outcome, I can't make that investment unfortunately :/. Time better spend looking into the performance issues in another way. I guess theres no open issue, thanks for the efford. out.
Nurul
Nurul2mo ago
No worries, I'll try to reproduce based on the service file you shared above. Regarding the Issue, I can tag you in this conversation if I have an update.
Want results from more Discord servers?
Add your server