SoxZz5
SoxZz5
PPrisma
Created by SoxZz5 on 11/2/2024 in #help-and-questions
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
7 replies