From d393cb53f6f75d7892082e8a1a2dec52617d4e74 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Tue, 29 Oct 2024 00:49:10 +0700 Subject: [PATCH] wip: advanced dynamic module --- libs/prisma/src/config.module-definition.ts | 7 +++ .../config-module-options.interface.ts | 6 +++ libs/prisma/src/prisma.module.ts | 49 +++++++++++++++---- src/app.module.ts | 25 ++++------ src/processes/list.process.ts | 15 ++++++ 5 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 libs/prisma/src/config.module-definition.ts create mode 100644 libs/prisma/src/interfaces/config-module-options.interface.ts create mode 100644 src/processes/list.process.ts diff --git a/libs/prisma/src/config.module-definition.ts b/libs/prisma/src/config.module-definition.ts new file mode 100644 index 0000000..77f1f7c --- /dev/null +++ b/libs/prisma/src/config.module-definition.ts @@ -0,0 +1,7 @@ +import { ConfigurableModuleBuilder } from '@nestjs/common'; +import { PrismaModuleOptions } from './interfaces/config-module-options.interface'; + +export const { ConfigurableModuleClass } = + new ConfigurableModuleBuilder() + .setClassMethodName('forRoot') + .build(); diff --git a/libs/prisma/src/interfaces/config-module-options.interface.ts b/libs/prisma/src/interfaces/config-module-options.interface.ts new file mode 100644 index 0000000..5492091 --- /dev/null +++ b/libs/prisma/src/interfaces/config-module-options.interface.ts @@ -0,0 +1,6 @@ +import { PrismaService } from '../prisma.service'; + +export interface PrismaModuleOptions { + delegate: (prisma: PrismaService) => any; + processList?: any; +} diff --git a/libs/prisma/src/prisma.module.ts b/libs/prisma/src/prisma.module.ts index be9471f..cc1e597 100644 --- a/libs/prisma/src/prisma.module.ts +++ b/libs/prisma/src/prisma.module.ts @@ -1,15 +1,46 @@ import { READ_ENTIRE_PROCESS } from '@autocrud/skeleton'; -import { Module } from '@nestjs/common'; +import { DynamicModule, Module } from '@nestjs/common'; +import { ConfigurableModuleClass } from './config.module-definition'; +import { PRISMA_DELEGATE } from './constants'; +import { PrismaModuleOptions } from './interfaces/config-module-options.interface'; import { PrismaService } from './prisma.service'; import { PrismaListProcess } from './processes/list.process'; -export const configServiceProvider = { - provide: READ_ENTIRE_PROCESS, - useClass: PrismaListProcess, -}; - @Module({ - providers: [PrismaService], - exports: [PrismaService], + exports: [PrismaService, PRISMA_DELEGATE, READ_ENTIRE_PROCESS], }) -export class PrismaModule {} +export class PrismaModule extends ConfigurableModuleClass { + static forRoot(options: PrismaModuleOptions): DynamicModule { + let providers = []; + providers = [ + ...providers, + PrismaService, + { + provide: PRISMA_DELEGATE, + useFactory: options.delegate, + inject: [PrismaService], + }, + ]; + if (options.processList) { + providers = [ + ...providers, + { + provide: READ_ENTIRE_PROCESS, + useClass: options.processList, + }, + ]; + } else { + providers = [ + ...providers, + { + provide: READ_ENTIRE_PROCESS, + useClass: PrismaListProcess, + }, + ]; + } + return { + ...super.forRoot(options), + providers, + }; + } +} diff --git a/src/app.module.ts b/src/app.module.ts index 2ea893f..9e95035 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,24 +1,17 @@ -import { - configServiceProvider, - PrismaModule, - PrismaService, -} from '@autocrud/prisma'; -import { PRISMA_DELEGATE } from '@autocrud/prisma/constants'; +import { PrismaModule } from '@autocrud/prisma'; import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; - -const customFactory = { - provide: PRISMA_DELEGATE, - useFactory: (prisma: PrismaService) => { - return prisma.user; - }, - inject: [PrismaService], -}; +import { AppListProcess } from './processes/list.process'; @Module({ - imports: [PrismaModule], + imports: [ + PrismaModule.forRoot({ + delegate: (prisma) => prisma.user, + processList: AppListProcess, + }), + ], controllers: [AppController], - providers: [AppService, customFactory, configServiceProvider], + providers: [AppService], }) export class AppModule {} diff --git a/src/processes/list.process.ts b/src/processes/list.process.ts new file mode 100644 index 0000000..cde2359 --- /dev/null +++ b/src/processes/list.process.ts @@ -0,0 +1,15 @@ +import { PrismaProcess } from '@autocrud/prisma/processes/prisma.process'; +import { ListProcess } from '@autocrud/skeleton/processes/list.process'; + +export class AppListProcess extends PrismaProcess implements ListProcess { + private data: any; + + async process(): Promise { + console.log('Hello World'); + super.process(); + } + + result() { + return this.data; + } +}