P
Prismaβ€’4mo ago
simnJS

Issue with prisma optimize

Hello, I have an issue with prisma optimize Error: prisma:client:operation span is expected to be entered in the client extension when tracing is enabled at Array.$allOperations (C:\Users\simon\Desktop\diamond-presence\node_modules@prisma\extension-optimize\dist\index.js:4:1103) at C:\Users\simon\Desktop\diamond-presence\node_modules@prisma\client\runtime\edge.js:5:5358 at i (C:\Users\simon\Desktop\diamond-presence\node_modules@prisma\client\runtime\edge.js:20:963) at PrismaPromise.then (C:\Users\simon\Desktop\diamond-presence\node_modules@prisma\client\runtime\edge.js:20:1038) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) Node.js v20.15.0
import './lib/setup';
import { PrismaClient } from '@prisma/client/edge';
import { withOptimize } from '@prisma/extension-optimize';
import { container, LogLevel, SapphireClient } from '@sapphire/framework';
import { GatewayIntentBits } from 'discord.js';

// Extend Prisma Client with the optimization extension
const prisma = new PrismaClient().$extends(withOptimize());

// Define a correct type for the extended Prisma Client
type ExtendedPrismaClient = PrismaClient & ReturnType<typeof withOptimize>;

// Sapphire client setup
const client = new SapphireClient({
defaultPrefix: '!',
caseInsensitiveCommands: true,
logger: {
level: LogLevel.Debug,
},
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
],
loadMessageCommandListeners: true,
});

// Main function to start the bot
const main = async () => {
try {
client.logger.info('Logging in');
await client.login();
client.logger.info('Logged in');
} catch (error) {
client.logger.fatal(error);
await client.destroy();
process.exit(1);
}
};

// Ensure the container has the correct type
declare module '@sapphire/pieces' {
interface Container {
database: ExtendedPrismaClient;
}
}

// Assign the database to the container
container.database = prisma as ExtendedPrismaClient;

// Execute the main function
void main();
import './lib/setup';
import { PrismaClient } from '@prisma/client/edge';
import { withOptimize } from '@prisma/extension-optimize';
import { container, LogLevel, SapphireClient } from '@sapphire/framework';
import { GatewayIntentBits } from 'discord.js';

// Extend Prisma Client with the optimization extension
const prisma = new PrismaClient().$extends(withOptimize());

// Define a correct type for the extended Prisma Client
type ExtendedPrismaClient = PrismaClient & ReturnType<typeof withOptimize>;

// Sapphire client setup
const client = new SapphireClient({
defaultPrefix: '!',
caseInsensitiveCommands: true,
logger: {
level: LogLevel.Debug,
},
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
],
loadMessageCommandListeners: true,
});

// Main function to start the bot
const main = async () => {
try {
client.logger.info('Logging in');
await client.login();
client.logger.info('Logged in');
} catch (error) {
client.logger.fatal(error);
await client.destroy();
process.exit(1);
}
};

// Ensure the container has the correct type
declare module '@sapphire/pieces' {
interface Container {
database: ExtendedPrismaClient;
}
}

// Assign the database to the container
container.database = prisma as ExtendedPrismaClient;

// Execute the main function
void main();
Solution:
Hey @simnJS Which version of optimize are you using? Is it 0.10.0?...
Jump to solution
18 Replies
simnJS
simnJSOPβ€’4mo ago
Up
Solution
Nurul
Nurulβ€’4mo ago
Hey @simnJS Which version of optimize are you using? Is it 0.10.0?
Nurul
Nurulβ€’4mo ago
Could you try out using this dev version of optimize extension and check if the issue gets resolved? @prisma/extension-optimize: 0.0.0-dev.202407222340
simnJS
simnJSOPβ€’4mo ago
I will check ty It work @Nurul !
Nurul
Nurulβ€’4mo ago
I am glad to hear that this resolved the issue πŸ™Œ
Virgile
Virgileβ€’4mo ago
Hi Nurul, I'm having an issue with Optimize version 0.10.1 : I pressed record, I initialized optimize and I don't see any rows displayed. It seems to work though as, if I press 'stop recording', it tells me it cannot record because no of recording. Any suggestions please ?
Virgile
Virgileβ€’4mo ago
Nurul
Nurulβ€’4mo ago
Hey @Virgile Thanks for sharing the recording. Please allow me to share this with our Optimize team and get back to you with a solution.
Virgile
Virgileβ€’4mo ago
thank you Nurul πŸ™‚ appreciated
Nurul
Nurulβ€’4mo ago
$extends returns a new client which you should use. Here you aren't using it. Could you try using the instance that you get when calling $extends. At the moment, you are discarding it. Regarding the error:
the error they get when the recording is stopped is because the new client attempts to send the schema in the background even before they do any queries, but they aren’t actually using the optimize-enabled client themselves
No description
Virgile
Virgileβ€’3mo ago
Can you provide a code snippet please ? I don't understand here is the code
export class PrismaService extends PrismaClient {
constructor(
@Inject(appConfigFeature.KEY)
private readonly appConfiguration: ConfigType<typeof appConfig>,
) {
super();
this.$extends(withOptimize())
}
export class PrismaService extends PrismaClient {
constructor(
@Inject(appConfigFeature.KEY)
private readonly appConfiguration: ConfigType<typeof appConfig>,
) {
super();
this.$extends(withOptimize())
}
Hi Nurul, I didn't quite understand your answer here. Sorry to notify you again but would really enjoy trying Prisma Optimize !
Nurul
Nurulβ€’3mo ago
Hey, Apologies i missed your last message. The main idea is that you should use the Prisma Client instance returned by the withOptimize() extension, which doesn't seem to be happening in your NestJS app. Normally, you would do:
const prisma = new PrismaClient().$extends(withOptimize());
const prisma = new PrismaClient().$extends(withOptimize());
We are using the PrismClient instance that is being retuned by this line "new PrismaClient().$extends(withOptimize());" In your case, you do this.$extends(withOptimize()) but doesn't use the value returned by it Can you do something like const prisma = this.$extends(withOptimize()) or equivalent in your code and use the prisma variable to perform database operations?
Virgile
Virgileβ€’3mo ago
I think we can do this yeah. But I think you should try it as well and add it to the docs, create a nestjs docs page ? For class base prisma What do you think @Nurul (Prisma) ? So that when people have the same issue they know how to fix it. I plan to make other videos using NestJS and Prisma, and I would like an easy fix in case people use paid features Because I think it’s the same with accelerate
Nurul
Nurulβ€’2mo ago
I agree that we should add this to our docs. I’ll try integrating Optimize in a Nest project and see what the workflow would look like. We were able to make Optimize work with Optimize. If you instantiate PrismaClient in OnModuleInit like this: https://github.com/nurul3101/Optimize-NestJS/blob/main/src/prisma.service.ts The queries would show up in Optimize Dashboard.
tl;dr
tl;drβ€’4w ago
Hi @Nurul (Prisma), I tried your repo, is it still working on your end :)? Is there something about generating the keys that I need to be aware of? Cheers! (https://github.com/nurul3101/Optimize-NestJS/issues/2)
GitHub
No data appears in my Optimize Dashboard when following instruction...
Hi, I'm currently having some issues with using NestJS + Prisma Optimize and saw this repository. I started up a local PG server using docker-compose: services: nestjs-optimize-pg-db: image: po...
Nurul
Nurulβ€’4w ago
Yes it works for me πŸ™‚ Does it not work for you?
tl;dr
tl;drβ€’4w ago
@Nurul Thanks for getting back πŸ‘. It didn't for two days. but then I tried this morning, and now it works perfectly (https://github.com/nurul3101/Optimize-NestJS/issues/2#issuecomment-2440944354) 😊. I'm still trying to track down an issue in my original setup which is a bit different (first of all running mssql), so I'm trying to dig into it a little deeper.
GitHub
No data appears in my Optimize Dashboard when following instruction...
Hi, I'm currently having some issues with using NestJS + Prisma Optimize and saw this repository. I started up a local PG server using docker-compose: services: nestjs-optimize-pg-db: image: po...
tl;dr
tl;drβ€’3w ago
It ended up working with mssql in a simple setup too, but still not in my original repository. So I started tearing my original repository apart to find out what was different, and while doing that, I discovered what caused the slow requests I had in the first place. Still didn't get optimize to work there though :/.
Want results from more Discord servers?
Add your server