Prisma Module
This module registers a global database client with NestJS. Once registered, this client can be injected using the PRISMA_CLIENT
symbol exported from this module.
Quickstart
To use library you need to:
- Import the PrismaModule into your top-level app module using the
registerAsync
method - Inject the prisma client using the
PRISMA_CLIENT
symbol into any services where you want access to a prisma client
// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PrismaModule } from '@consumer-health-platform/prisma';
import { PrismaClient } from '.prisma/my-app';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
PrismaModule.registerAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => {
return {
client: PrismaClient,
connString: config.get('DATABASE_URL', { infer: true }),
};
},
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
// my.repository.ts
import { Injectable, Inject } from '@nestjs/common';
import { PRISMA_CLIENT } from '@consumer-health-platform/prisma';
import { PrismaClient } from '.prisma/my-app';
@Injectable()
export class MyRepo {
constructor(@Inject(PRISMA_CLIENT) private readonly client: PrismaClient) {}
findAll() {
return this.client.myEntity.findMany();
}
}
Module
The module is designed to be passed configuration (the prisma client and connection string) asynchronously.The reasons for this are to:
- allow for different prisma clients between apps
- allow configuration to interact with other NestJS services (e.g.
ConfigService
)
Because of this, you must import the module using the registerAsync
method. This method accepts a configuration object for resolving client configuration:
- useFactory: Defines a factory function which returns a
client
andconnString
to use when providing a prisma client instance. - inject: Defines an array of dependencies to passed to the
useFactory
function. These must be globally available services.
Client
This module exports a PRISMA_CLIENT
symbol. This symbol is an injection token for resolving an instance of the prisma client given when registering the module.