Boku no Tema
Boku no Tema
PPrisma
Created by Boku no Tema on 3/12/2025 in #help-and-questions
NestJS + prisma optimize
I'm trying to add prisma optimize to project with nest.js framework. As I can see in logs - it initializes, but there is no data recorded
import {
FactoryProvider,
Global,
Logger,
Module,
OnModuleDestroy,
} from '@nestjs/common';
import { PrismaClient, Prisma } from '@prisma/client';
import { ConfigService } from '@nestjs/config';
import { EventEmitter2 } from '@nestjs/event-emitter';

import { getPrismaWithEventEmitterExtension } from './extensions';
import { withOptimize } from '@prisma/extension-optimize';

const logger = new Logger('PrismaClient');

const prismaClientProvider: FactoryProvider<PrismaClient> = {
provide: PrismaClient,
inject: [ConfigService, EventEmitter2],
useFactory: (configService: ConfigService, eventEmitter: EventEmitter2) => {
const logLevels: Prisma.LogLevel[] = configService.get<boolean>(
'isPrismaDebugEnabled',
)
? ['info', 'query', 'warn', 'error']
: ['error'];

const prismaClient = new PrismaClient({
log: logLevels.map((level) => ({
emit: 'event',
level,
})),
datasources: {
db: {
url: configService.get('DB_URL'),
},
},
});

prismaClient.$on('info', logger.debug.bind(logger));
prismaClient.$on('query', logger.debug.bind(logger));
prismaClient.$on('warn', logger.warn.bind(logger));
prismaClient.$on('error', logger.error.bind(logger));

return prismaClient
.$extends(getPrismaWithEventEmitterExtension(eventEmitter))
.$extends(
withOptimize({ apiKey: process.env.PRISMA_OPTIMIZE_API_KEY }),
) as unknown as PrismaClient;
},
};

@Global()
@Module({
providers: [prismaClientProvider],
exports: [PrismaClient],
})
export class DatabaseModule implements OnModuleDestroy {
constructor(private readonly prisma: PrismaClient) {}

async onModuleDestroy() {
await this.prisma.$disconnect();
logger.log('PrismaClient disconnected on module destroy');
}
}
import {
FactoryProvider,
Global,
Logger,
Module,
OnModuleDestroy,
} from '@nestjs/common';
import { PrismaClient, Prisma } from '@prisma/client';
import { ConfigService } from '@nestjs/config';
import { EventEmitter2 } from '@nestjs/event-emitter';

import { getPrismaWithEventEmitterExtension } from './extensions';
import { withOptimize } from '@prisma/extension-optimize';

const logger = new Logger('PrismaClient');

const prismaClientProvider: FactoryProvider<PrismaClient> = {
provide: PrismaClient,
inject: [ConfigService, EventEmitter2],
useFactory: (configService: ConfigService, eventEmitter: EventEmitter2) => {
const logLevels: Prisma.LogLevel[] = configService.get<boolean>(
'isPrismaDebugEnabled',
)
? ['info', 'query', 'warn', 'error']
: ['error'];

const prismaClient = new PrismaClient({
log: logLevels.map((level) => ({
emit: 'event',
level,
})),
datasources: {
db: {
url: configService.get('DB_URL'),
},
},
});

prismaClient.$on('info', logger.debug.bind(logger));
prismaClient.$on('query', logger.debug.bind(logger));
prismaClient.$on('warn', logger.warn.bind(logger));
prismaClient.$on('error', logger.error.bind(logger));

return prismaClient
.$extends(getPrismaWithEventEmitterExtension(eventEmitter))
.$extends(
withOptimize({ apiKey: process.env.PRISMA_OPTIMIZE_API_KEY }),
) as unknown as PrismaClient;
},
};

@Global()
@Module({
providers: [prismaClientProvider],
exports: [PrismaClient],
})
export class DatabaseModule implements OnModuleDestroy {
constructor(private readonly prisma: PrismaClient) {}

async onModuleDestroy() {
await this.prisma.$disconnect();
logger.log('PrismaClient disconnected on module destroy');
}
}
8 replies