P
Prisma3w ago
SoxZz5

Optimize + NestJS not working

GM, I checked the whole forum before asking I tried to test with the provided example : https://github.com/nurul3101/Optimize-NestJS/tree/main But without success. This is how we are instantiating our prisma service :
generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema", "prismaSchemaFolder", "views", "tracing"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = [LIST_OF_SCHEMA_HERE ie: 'a', 'b' ...]
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema", "prismaSchemaFolder", "views", "tracing"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = [LIST_OF_SCHEMA_HERE ie: 'a', 'b' ...]
}
We use some multiSchema and prismaSchemaFolder dunno if it can interfere On nestjs :
import { PrismaClient } from '@prisma/client'
import { withOptimize } from '@prisma/extension-optimize'
import { withPulse } from '@prisma/extension-pulse'

function extendClient(
base: PrismaClient,
): PrismaClient &
ReturnType<typeof withPulse> &
ReturnType<typeof withOptimize> {
const extended = base
.$extends(
withOptimize({
apiKey: process.env.OPTIMIZE_API_KEY as string,
}),
)
.$extends(
withPulse({
apiKey: process.env.PULSE_API_KEY as string,
}),
) as unknown as PrismaClient &
ReturnType<typeof withPulse> &
ReturnType<typeof withOptimize>
return extended
}

export class UntypedExtendedClient extends PrismaClient {
constructor(options?: ConstructorParameters<typeof PrismaClient>[0]) {
super(options)

// biome-ignore lint/correctness/noConstructorReturn: Extended client is returned
return extendClient(this) as PrismaClient &
ReturnType<typeof withPulse> &
ReturnType<typeof withOptimize>
}
}

export const ExtendedPrismaClient = UntypedExtendedClient as unknown as new (
options?: ConstructorParameters<typeof PrismaClient>[0],
) => ReturnType<typeof extendClient>

export type ExtendedPrismaClientType = ReturnType<typeof extendClient>
import { PrismaClient } from '@prisma/client'
import { withOptimize } from '@prisma/extension-optimize'
import { withPulse } from '@prisma/extension-pulse'

function extendClient(
base: PrismaClient,
): PrismaClient &
ReturnType<typeof withPulse> &
ReturnType<typeof withOptimize> {
const extended = base
.$extends(
withOptimize({
apiKey: process.env.OPTIMIZE_API_KEY as string,
}),
)
.$extends(
withPulse({
apiKey: process.env.PULSE_API_KEY as string,
}),
) as unknown as PrismaClient &
ReturnType<typeof withPulse> &
ReturnType<typeof withOptimize>
return extended
}

export class UntypedExtendedClient extends PrismaClient {
constructor(options?: ConstructorParameters<typeof PrismaClient>[0]) {
super(options)

// biome-ignore lint/correctness/noConstructorReturn: Extended client is returned
return extendClient(this) as PrismaClient &
ReturnType<typeof withPulse> &
ReturnType<typeof withOptimize>
}
}

export const ExtendedPrismaClient = UntypedExtendedClient as unknown as new (
options?: ConstructorParameters<typeof PrismaClient>[0],
) => ReturnType<typeof extendClient>

export type ExtendedPrismaClientType = ReturnType<typeof extendClient>
This is used to create a typed Extended Instance of the prisma client Then we use the prisma.service exported by the prisma.module :
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { ExtendedPrismaClient } from './prisma.extension'

@Injectable()
export class PrismaService
extends ExtendedPrismaClient
implements OnModuleInit, OnModuleDestroy
{
private static instance: PrismaService | null = null

constructor() {
super({
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
log: ['error', 'warn'],
})

if (PrismaService.instance) {
// biome-ignore lint/correctness/noConstructorReturn: <We don't want to create a new instance>
return PrismaService.instance
}

PrismaService.instance = this
}

async onModuleInit() {
await this.$connect()
}

async onModuleDestroy() {
await this.$disconnect()
}
}
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { ExtendedPrismaClient } from './prisma.extension'

@Injectable()
export class PrismaService
extends ExtendedPrismaClient
implements OnModuleInit, OnModuleDestroy
{
private static instance: PrismaService | null = null

constructor() {
super({
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
log: ['error', 'warn'],
})

if (PrismaService.instance) {
// biome-ignore lint/correctness/noConstructorReturn: <We don't want to create a new instance>
return PrismaService.instance
}

PrismaService.instance = this
}

async onModuleInit() {
await this.$connect()
}

async onModuleDestroy() {
await this.$disconnect()
}
}
The super must create an instance of extended client with extension (it work for pulse not for optimize by the way) and the return if instance exist is too prevent opening too much connection Happy to provide more code if it can help let me know, feel free to ping me
GitHub
GitHub - nurul3101/Optimize-NestJS
Contribute to nurul3101/Optimize-NestJS development by creating an account on GitHub.
4 Replies
SoxZz5
SoxZz5OP3w ago
While optimize doesn't work on nestjs, I did a rapid tool to parse a full nestjs API and with CLI command line run prisma queries to use optimize outside my API let me know if you want to have this as an example
SoxZz5
SoxZz5OP3w ago
It parse every controller and do smth like this :
No description
Nurul
Nurul2w ago
@SoxZz5 To confirm, you cloned the code from this example and it didn't work for you? https://github.com/nurul3101/Optimize-NestJS/tree/main One other user reported the same intially, but it worked for them eventually. https://github.com/nurul3101/Optimize-NestJS/issues/2
SoxZz5
SoxZz5OP2w ago
Didn't cloned it, but I checked the package json, the .prisma, and how you register the service it was really near the way we do it (we just type it a bit more) So then what I did sicne it's not working with our NestJS, I did a little tool to get all endpoints -> service -> prisma call And recreate it (my second screenshot) so now optimize work in the context of my little tool it's a simple nodeJS, but we still don't get prisma with optimize working on NestJS (pulse seems to work and we will try accelerate) @kipit ( I just tag one other dev on the project so he can follow too)
Want results from more Discord servers?
Add your server