wip: advanced dynamic module

This commit is contained in:
Supan Adit Pratama 2024-10-29 00:49:10 +07:00
parent 9187f02a23
commit d393cb53f6
5 changed files with 77 additions and 25 deletions

View File

@ -0,0 +1,7 @@
import { ConfigurableModuleBuilder } from '@nestjs/common';
import { PrismaModuleOptions } from './interfaces/config-module-options.interface';
export const { ConfigurableModuleClass } =
new ConfigurableModuleBuilder<PrismaModuleOptions>()
.setClassMethodName('forRoot')
.build();

View File

@ -0,0 +1,6 @@
import { PrismaService } from '../prisma.service';
export interface PrismaModuleOptions {
delegate: (prisma: PrismaService) => any;
processList?: any;
}

View File

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

View File

@ -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 {}

View File

@ -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<any> {
console.log('Hello World');
super.process();
}
result() {
return this.data;
}
}