sabinadams / nestjs-prisma-module

A NestJS module that allows you use Prisma, set up multiple Prisma services, and use multi-tenancy in each Prisma service.
50 stars 3 forks source link

Feature Request: Support Async Configurations #15

Open ruslanguns opened 2 years ago

ruslanguns commented 2 years ago

I think it's essential for any NestJS libraries to be able to support dynamic configurations, as many of us want to inject loadable configurations based on certain business logic or simple because we want to load them from another service, so there's a conventional approach using Dependency Injection so that by exposing a static method called: registerAsync. So for instance, we can follow this guide, because doing it manually can be a bit overwhelming.

If you want I can organice a PR with this functionality, the register will always be supported for the dynamic module, but also it will support theregisterAsync method.

Proposal:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaClient } from '@prisma/client';
import { PrismaModule } from '@sabinthedev/nestjs-prisma';
import { ConfigService } from './config/config.service.ts';

@Module({
  imports: [
    PrismaModule.registerAsync({
       useFactory: (config: ConfigService) => ({
           client: PrismaClient,
           name: config.get('database.name'),
           multitenancy: config.get('database.allowMultitenancy'),
           datasource: config.get('database.url'),
       })
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }
sabinadams commented 2 years ago

This would be a great addition! Absolutely feel free to submit a PR for this.

Otherwise I'll pick it up at some point 👍

ruslanguns commented 2 years ago

Cool, I'll start soon with this enhancement!