wip: working auto preset injection

This commit is contained in:
Supan Adit Pratama 2024-10-30 20:19:41 +07:00
parent 64b7119347
commit aaa3e29ef3
2 changed files with 90 additions and 21 deletions

View File

@ -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,

View File

@ -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],