📝Prisma

This service is used to connect to the database using Prisma.

Imports

import { PrismaService } from 'niro-health';

Environment Variables

Do you create variables an .env file.

name
description

DATABASE_URL

Url of the database.

DB_PORT

Port of the database.

DB_USERNAME

Username of the database.

DB_PASSWORD

Password of the database.

DB_DATABASE_NAME

Name of the database.

Check the example file.

.env
DATABASE_URL=postgresql://postgres:postgres@localhost/project_development
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE_NAME=project_development

Method of Use

To use this module, you need to inject it into the desired service.

import { Module } from '@nestjs/common';
import { AppHostService, PrismaService } from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IAppHostService',
      useClass: AppHostService,
    },
    {
      provide: 'IPrismaService',
      useClass: PrismaService,
    },
  ],
})
export class AppModule {}

Injecting the module into our service.

import { Inject, Injectable } from '@nestjs/common';
import type { IAppHostService, IPrismaService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IAppHostService')
    private readonly appHostService: IAppHostService,
    @Inject('IPrismaService')
    private readonly prismaService: IPrismaService,
  ) {}

  async enableShutdownHooks() {
    await this.prismaService.enableShutdownHooks(this.appHostService.app);
  }
}

Modifying our main.ts

main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from '@/app.module';
import { Logger } from '@nestjs/common';
import { AppHostModule, AppHostService } from 'niro-health';
import { AppModule } from './app.module';
import { AppService } from './app.service';

(async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new Logger('NestApplication', {
      timestamp: true,
    }),
    cors: true,
  });
  app.select(AppHostModule).get(AppHostService).setApp(app);
  app.select(AppModule).get(AppService).enableShutdownHooks();
})();

With dependencies

You will need to inject the following interfaces.

Interface
Service

AppHostService

Properties

Methods

Method
Scope
Description

onModuleInit

public

This method is used to connect to the database.

enableShutdownHooks

public

This method is used to close the connection to the database.

Last updated