Skip to content

Commit

Permalink
✨ Feat: prisma query logging 시 formatting 및 params, duration 볼 수 있도록
Browse files Browse the repository at this point in the history
#
  • Loading branch information
ks1ksi committed Aug 4, 2023
1 parent a6d2547 commit 9f137d2
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
86 changes: 86 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"pgvector": "^0.1.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"sql-formatter": "^12.2.4",
"swagger-ui-express": "^5.0.0",
"uuid": "^9.0.0"
},
Expand Down
43 changes: 40 additions & 3 deletions src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PrismaClient } from '@prisma/client';
import { Prisma, PrismaClient } from '@prisma/client';
import { format } from 'sql-formatter';

@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
private readonly logger: Logger = new Logger(PrismaService.name);

constructor(private readonly configService: ConfigService) {
super({
datasources: {
db: {
url: configService.get<string>('DATABASE_URL'),
},
},
log: ['query', 'info', 'warn', 'error'],
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'stdout',
level: 'error',
},
{
emit: 'stdout',
level: 'info',
},
{
emit: 'stdout',
level: 'warn',
},
],
});
(
this.$on as (
eventType: 'query',
callback: (e: Prisma.QueryEvent) => void,
) => void
)('query', (e: Prisma.QueryEvent) => {
this.logger.log(
`Query: \n${format(e.query, { language: 'postgresql' })}`,
);
this.logger.verbose(`Params: ${e.params}`);
this.logger.debug(`Duration: ${e.duration}ms`);
});
}

Expand Down

0 comments on commit 9f137d2

Please sign in to comment.