From aaa3e29ef3926074f826734198fa885da57ae366 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Wed, 30 Oct 2024 20:19:41 +0700 Subject: [PATCH] wip: working auto preset injection --- libs/prisma/src/prisma.module.ts | 109 +++++++++++++++++++++++++------ src/app.module.ts | 2 - 2 files changed, 90 insertions(+), 21 deletions(-) diff --git a/libs/prisma/src/prisma.module.ts b/libs/prisma/src/prisma.module.ts index ac1705d..cdf20f3 100644 --- a/libs/prisma/src/prisma.module.ts +++ b/libs/prisma/src/prisma.module.ts @@ -1,15 +1,60 @@ -import { LIST_PROCESS } from '@aditama-labs/nest-autocrud/skeleton'; +import { + CREATE_PROCESS, + DELETE_PROCESS, + LIST_PROCESS, + PAGINATION_PROCESS, + READ_PROCESS, + UPDATE_PROCESS, +} from '@aditama-labs/nest-autocrud/skeleton'; 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 { + PrismaCreateProcess, + PrismaDeleteProcess, + PrismaPaginationProcess, + PrismaReadProcess, + PrismaUpdateProcess, +} from './processes'; import { PrismaListProcess } from './processes/list.process'; @Module({ - exports: [PrismaService, PRISMA_DELEGATE, LIST_PROCESS], + exports: [ + PrismaService, + PRISMA_DELEGATE, + // List of Process + CREATE_PROCESS, + DELETE_PROCESS, + LIST_PROCESS, + PAGINATION_PROCESS, + READ_PROCESS, + UPDATE_PROCESS, + ], }) export class PrismaModule extends ConfigurableModuleClass { + private static autoPresetProvider(providers, option, key, preset): any { + if (option) { + providers = [ + ...providers, + { + provide: key, + useClass: option, + }, + ]; + } else { + providers = [ + ...providers, + { + provide: key, + useClass: preset, + }, + ]; + } + return providers; + } + static forRoot(options: PrismaModuleOptions): DynamicModule { let providers = []; providers = [ @@ -21,23 +66,49 @@ export class PrismaModule extends ConfigurableModuleClass { inject: [PrismaService], }, ]; - if (options.processList) { - providers = [ - ...providers, - { - provide: LIST_PROCESS, - useClass: options.processList, - }, - ]; - } else { - providers = [ - ...providers, - { - provide: LIST_PROCESS, - useClass: PrismaListProcess, - }, - ]; - } + + providers = PrismaModule.autoPresetProvider( + providers, + options.processCreate, + CREATE_PROCESS, + PrismaCreateProcess, + ); + + providers = PrismaModule.autoPresetProvider( + providers, + options.processDelete, + DELETE_PROCESS, + PrismaDeleteProcess, + ); + + providers = PrismaModule.autoPresetProvider( + providers, + options.processList, + LIST_PROCESS, + PrismaListProcess, + ); + + providers = PrismaModule.autoPresetProvider( + providers, + options.processPagination, + PAGINATION_PROCESS, + PrismaPaginationProcess, + ); + + providers = PrismaModule.autoPresetProvider( + providers, + options.processRead, + READ_PROCESS, + PrismaReadProcess, + ); + + providers = PrismaModule.autoPresetProvider( + providers, + options.processUpdate, + UPDATE_PROCESS, + PrismaUpdateProcess, + ); + return { ...super.forRoot(options), providers, diff --git a/src/app.module.ts b/src/app.module.ts index 3720d65..873bd60 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,13 +2,11 @@ import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma'; import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { AppListProcess } from './processes/list.process'; @Module({ imports: [ PrismaModule.forRoot({ delegate: (prisma) => prisma.user, - processList: AppListProcess, }), ], controllers: [AppController],