Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query-logging not work in nestjs with prisma service #48

Open
tastafur opened this issue Feb 2, 2024 · 0 comments
Open

query-logging not work in nestjs with prisma service #48

tastafur opened this issue Feb 2, 2024 · 0 comments

Comments

@tastafur
Copy link

tastafur commented Feb 2, 2024

I have tried to use the extension in the service but it never logs information and I have tried to put it as your example and as the documentation but my loguer of the queries with the events on yes

const options = {
  log: [
    { emit: 'event', level: 'query' } as const,
    { emit: 'event', level: 'info' } as const,
    { emit: 'event', level: 'warn' } as const,
    { emit: 'event', level: 'error' } as const,
  ],
  errorFormat:
    process.env.NODE_ENV === 'production'
      ? ('minimal' as const)
      : ('pretty' as const),
};

@Injectable()
export class PrismaService
  extends PrismaClient<typeof options>
  implements OnModuleInit, OnModuleDestroy
{
  private readonly _logger = new Logger(PrismaService.name);

  public constructor() {
    super(options);
    this.$on('query', (e: Prisma.QueryEvent) => {
      console.log(e);
    });
    this.$on('info', (e: Prisma.LogEvent) => {
      this._logger.log(e.message);
    });
    this.$on('warn', (e: Prisma.LogEvent) => {
      this._logger.warn(e.message);
    });
    this.$on('error', (e: Prisma.LogEvent) => {
      this._logger.error(e.message);
    });

    this.$extends({
      query: {
        $allModels: {
          async $allOperations({ operation, model, args, query }) {
            const start = performance.now();
            const result = await query(args);
            const end = performance.now();
            const time = end - start;
            console.log(
              util.inspect(
                { model, operation, args, time },
                { showHidden: false, depth: null, colors: true },
              ),
            );
            return result;
          },
        },
      },
    });
  }
  async onModuleInit() {
    await this.$connect();
  }

  async onModuleDestroy() {
    await this.$disconnect();
  }

  async enableShutdownHooks(app: INestApplication) {
    process.on('beforeExit', async () => {
      await app.close();
    });
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant