#Still unable to get Optimize working in my NestJS app.

21 messages · Page 1 of 1 (latest)

merry oracle
#

Hi everyone. I would really like to try Optimize, and have been trying for some time. See similar discussion here #1270167207204356178 message

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)
    }
  }

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.

covert martenBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

calm rampart
#

Hey @merry oracle!

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:
#1288624617165946943 message

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 🙏

calm rampart
calm rampart
#

@merry oracle Just checking if you had a chance to try out the suggested solution. Let us know in case you still observe any issues.

floral sinew
# calm rampart <@273453619704233986> Just checking if you had a chance to try out the suggested...

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()
  }
}
#

I'm on a monorepo if that helps

calm rampart
#

What happens if you temporarily remove the pagination extension and only use the optimize one. Do you still see no queries in optimize dashboard?

near needle
#

@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!');
  }
}

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.

calm rampart
#

@near needle Do you mind sharing link to your repository?
I would like to share it with our Optimize team

near needle
#

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

calm rampart
#

That would be very helpful 🙏

merry oracle
#

Hi @calm rampart . Got the same issue as @near needle on my main monorepo.

On a repro monorepo, it does show the data (when calling prisma inside the prisma service, after extending Optimize)

merry oracle
#

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 @deep raven because you asked me how you could help with Optimize. This is how 🙂

GitHub

Remix App With NestJS Adapter. Contribute to Varkoff/remix-nestjs-monorepo development by creating an account on GitHub.

calm rampart
#

@merry oracle 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 🙏

marble ravine
#

Hey @merry oracle 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"
  1. 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() {}

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

marble ravine
#

Hey @merry oracle I just wanted to let you know we have released the fix.

merry oracle
merry oracle
#

Using version "@prisma/extension-optimize": "^1.0.3"

marble ravine
#

Hey @merry oracle 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.