nestjs / terminus

Terminus module for Nest framework (node.js) :robot:

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Prisma ORM

TasinIshmam opened this issue · comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Terminus is quite useful for checking the health of your database and it would be great if it would support Prisma ORM.

Describe the solution you'd like

Support for Prisma ORM, similar to TypeOrmHealthIndicator , SequelizeHealthIndicator and MongooseHealthIndicator.

Teachability, documentation, adoption, migration strategy

The terminus recipe in the NestJS docs would need to be updated with a PrismaHealthIndicator.

To the best of my understanding, the change to the documentation would be fairly minimal.

What is the motivation / use case for changing the behavior?

I work at Prisma and we have seen a lot of NestJS users adopting Prisma for the ORM layer in their application. So having better support for the ORM would be great for developers using NestJS with Prisma!

It looks like it could be as easy as duplicating the typeorm one here and adapting it (removing things) https://github.com/nestjs/terminus/blob/master/lib/health-indicator/database/typeorm.health.ts#L110
With Prisma we could do prisma.$queryRawSELECT 1` for SQL databases, for MongoDB (in Preview) I'm not sure how it can be done, maybe that would need a feature request here

Thanks for the suggestion! I am open to review any PRs around this!

@BrunnerLivio

I was actually looking into this, but it seems all the database connectors have a corresponding nestjs library (ex: TypeOrm has @nestjs/typeorm). From that sense, it's much more than a PR.

I'm happy to throw together a health indicator for this repo, but creating a new @nestjs/prisma module is an area I'm sure I'm missing a lot of context and frankly, necessary time to complete.

Thoughts?

Btw, in case anyone comes here and just wants a simple health indicator, I have a Stackoverflow answer for it.

https://stackoverflow.com/a/71445270/317951

import { Injectable } from "@nestjs/common";
import { HealthCheckError, HealthIndicator, HealthIndicatorResult } from "@nestjs/terminus";
import { PrismaService } from "./prisma.service";

@Injectable()
export class PrismaHealthIndicator extends HealthIndicator {
  constructor(private readonly prismaService: PrismaService) {
    super();
  }

  async isHealthy(key: string): Promise<HealthIndicatorResult> {
    try {
      await this.prismaService.$queryRaw`SELECT 1`;
      return this.getStatus(key, true);
    } catch (e) {
      throw new HealthCheckError("Prisma check failed", e);
    }
  }
}

@jfairley
this is not working for me it shows the error " Prisma check failed"

@ThallesP has created an inital PR for this and we've merged it into our next-branch which targets Terminus v10. Prisma client seems to have dropped support for Node v12 hence why I'd need to drop it as well which I can't do with Terminus v9 (it's considered a breaking change).
At the moment there are still some things to figure out though since Prisma seems to be quite tedious because of its bindings hence why the Pipeline is failing at the moment. Once that is figured out I'll try to release a beta release of v10 with the Prisma health indicator included

This has been released with @nestjs/terminus@10.0.0-beta.0 (unstable). 🎉
If you wanna try it out, follow the instructions here

@BrunnerLivio When should we expect for v10 to be out?

It’s released 🎉