P
Prisma•3mo 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
14 Replies
simnJS
simnJS•3mo ago
Up
Solution
Nurul
Nurul•3mo ago
Hey @simnJS Which version of optimize are you using? Is it 0.10.0?
Nurul
Nurul•3mo 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
simnJS•3mo ago
I will check ty It work @Nurul !
Nurul
Nurul•3mo ago
I am glad to hear that this resolved the issue 🙌
Virgile
Virgile•3mo 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•3mo ago
Nurul
Nurul•3mo 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•3mo ago
thank you Nurul 🙂 appreciated
Nurul
Nurul•3mo 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•2mo 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•2mo 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•2mo 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•2w 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.
Want results from more Discord servers?
Add your server