mirror of
https://github.com/aditama-labs/nest-autocrud.git
synced 2024-11-22 03:16:21 +00:00
wip: advanced dynamic module
This commit is contained in:
parent
9187f02a23
commit
d393cb53f6
7
libs/prisma/src/config.module-definition.ts
Normal file
7
libs/prisma/src/config.module-definition.ts
Normal 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();
|
@ -0,0 +1,6 @@
|
||||
import { PrismaService } from '../prisma.service';
|
||||
|
||||
export interface PrismaModuleOptions {
|
||||
delegate: (prisma: PrismaService) => any;
|
||||
processList?: any;
|
||||
}
|
@ -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 = {
|
||||
@Module({
|
||||
exports: [PrismaService, PRISMA_DELEGATE, READ_ENTIRE_PROCESS],
|
||||
})
|
||||
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,
|
||||
};
|
||||
|
||||
@Module({
|
||||
providers: [PrismaService],
|
||||
exports: [PrismaService],
|
||||
})
|
||||
export class PrismaModule {}
|
||||
}
|
||||
}
|
||||
|
@ -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 {}
|
||||
|
15
src/processes/list.process.ts
Normal file
15
src/processes/list.process.ts
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user