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 { DynamicModule, Module } from '@nestjs/common';
import { ConfigurableModuleClass } from './config.module-definition'; import { ConfigurableModuleClass } from './config.module-definition';
import { PRISMA_DELEGATE } from './constants'; import { PRISMA_DELEGATE } from './constants';
import { PrismaModuleOptions } from './interfaces/config-module-options.interface'; import { PrismaModuleOptions } from './interfaces/config-module-options.interface';
import { PrismaService } from './prisma.service'; import { PrismaService } from './prisma.service';
import {
PrismaCreateProcess,
PrismaDeleteProcess,
PrismaPaginationProcess,
PrismaReadProcess,
PrismaUpdateProcess,
} from './processes';
import { PrismaListProcess } from './processes/list.process'; import { PrismaListProcess } from './processes/list.process';
@Module({ @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 { 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 { static forRoot(options: PrismaModuleOptions): DynamicModule {
let providers = []; let providers = [];
providers = [ providers = [
@ -21,23 +66,49 @@ export class PrismaModule extends ConfigurableModuleClass {
inject: [PrismaService], inject: [PrismaService],
}, },
]; ];
if (options.processList) {
providers = [ providers = PrismaModule.autoPresetProvider(
...providers, providers,
{ options.processCreate,
provide: LIST_PROCESS, CREATE_PROCESS,
useClass: options.processList, PrismaCreateProcess,
}, );
];
} else { providers = PrismaModule.autoPresetProvider(
providers = [ providers,
...providers, options.processDelete,
{ DELETE_PROCESS,
provide: LIST_PROCESS, PrismaDeleteProcess,
useClass: PrismaListProcess, );
},
]; 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 { return {
...super.forRoot(options), ...super.forRoot(options),
providers, providers,

View File

@ -2,13 +2,11 @@ import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { AppListProcess } from './processes/list.process';
@Module({ @Module({
imports: [ imports: [
PrismaModule.forRoot({ PrismaModule.forRoot({
delegate: (prisma) => prisma.user, delegate: (prisma) => prisma.user,
processList: AppListProcess,
}), }),
], ],
controllers: [AppController], controllers: [AppController],