From 3755f491813f2b88aaac615e2a79d55027729143 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Sun, 27 Oct 2024 00:15:22 +0700 Subject: [PATCH] wip: test simple delegate --- libs/prisma/src/constants.ts | 1 + libs/prisma/src/prisma.module.ts | 28 +------ libs/prisma/src/processes/prisma.process.ts | 11 ++- libs/skeleton/src/constants.ts | 10 --- .../skeleton-crud.controller.interface.ts | 10 --- libs/skeleton/src/skeleton-crud.controller.ts | 80 +------------------ src/app.module.ts | 13 ++- 7 files changed, 24 insertions(+), 129 deletions(-) create mode 100644 libs/prisma/src/constants.ts diff --git a/libs/prisma/src/constants.ts b/libs/prisma/src/constants.ts new file mode 100644 index 0000000..eff4044 --- /dev/null +++ b/libs/prisma/src/constants.ts @@ -0,0 +1 @@ +export declare const PRISMA_DELEGATE = 'PRISMA_DELEGATE'; diff --git a/libs/prisma/src/prisma.module.ts b/libs/prisma/src/prisma.module.ts index d63df3f..4da3cab 100644 --- a/libs/prisma/src/prisma.module.ts +++ b/libs/prisma/src/prisma.module.ts @@ -1,37 +1,13 @@ -import { - CREATE_PROCESS, - READ_ENTIRE_PROCESS, - READ_PAGINATION_PROCESS, - READ_PROCESS, - UPDATE_BATCH_PROCESS, - UPDATE_ENTIRE_BATCH_PROCESS, - UPDATE_ENTIRE_PROCESS, - UPDATE_PARTIAL_PROCESS, -} from '@autocrud/skeleton'; +import { READ_ENTIRE_PROCESS } from '@autocrud/skeleton'; import { Module } from '@nestjs/common'; import { PrismaService } from './prisma.service'; -import { PrismaBatchUpdateProcess } from './processes/batch/update.process'; -import { PrismaCreateProcess } from './processes/create.process'; import { PrismaListProcess } from './processes/list.process'; -import { PrismaPaginationProcess } from './processes/pagination.process'; -import { PrismaPartialUpdateProcess } from './processes/partial/update.process'; -import { PrismaReadProcess } from './processes/read.process'; -import { PrismaUpdateProcess } from './processes/update.process'; @Module({ providers: [ PrismaService, - { provide: CREATE_PROCESS, useClass: PrismaCreateProcess }, - { provide: READ_PROCESS, useClass: PrismaReadProcess }, - { provide: READ_PAGINATION_PROCESS, useClass: PrismaPaginationProcess }, { provide: READ_ENTIRE_PROCESS, useClass: PrismaListProcess }, - { provide: UPDATE_PARTIAL_PROCESS, useClass: PrismaPartialUpdateProcess }, - { provide: UPDATE_BATCH_PROCESS, useClass: PrismaPartialUpdateProcess }, - { provide: UPDATE_ENTIRE_PROCESS, useClass: PrismaUpdateProcess }, - { - provide: UPDATE_ENTIRE_BATCH_PROCESS, - useClass: PrismaBatchUpdateProcess, - }, + , ], exports: [PrismaService], }) diff --git a/libs/prisma/src/processes/prisma.process.ts b/libs/prisma/src/processes/prisma.process.ts index 2ecd528..ba1175a 100644 --- a/libs/prisma/src/processes/prisma.process.ts +++ b/libs/prisma/src/processes/prisma.process.ts @@ -1,10 +1,13 @@ import { DefaultProcess } from '@autocrud/skeleton/processes/default.process'; +import { Inject } from '@nestjs/common'; +import { PRISMA_DELEGATE } from '../constants'; export class PrismaProcess extends DefaultProcess { - protected delegate; - - setDelegate(delegate) { - this.delegate = delegate; + constructor( + @Inject(PRISMA_DELEGATE) + private delegate: any, + ) { + super(); } get getDelegate() { diff --git a/libs/skeleton/src/constants.ts b/libs/skeleton/src/constants.ts index 7af786f..b637760 100644 --- a/libs/skeleton/src/constants.ts +++ b/libs/skeleton/src/constants.ts @@ -1,11 +1 @@ -export declare const CREATE_PROCESS = 'CREATE_PROCESS'; -export declare const READ_PROCESS = 'READ_PROCESS'; -export declare const READ_PAGINATION_PROCESS = 'READ_PAGINATION_PROCESS'; export declare const READ_ENTIRE_PROCESS = 'READ_ENTIRE_PROCESS'; -export declare const UPDATE_PARTIAL_PROCESS = 'UPDATE_PARTIAL_PROCESS'; -export declare const UPDATE_BATCH_PROCESS = 'UPDATE_BATCH_PROCESS'; -export declare const UPDATE_ENTIRE_PROCESS = 'UPDATE_ENTIRE_PROCESS'; -export declare const UPDATE_ENTIRE_BATCH_PROCESS = - 'UPDATE_ENTIRE_BATCH_PROCESS'; -export declare const DELETE_PROCESS = 'DELETE_PROCESS'; -export declare const DELETE_BATCH_PROCESS = 'DELETE_BATCH_PROCESS'; diff --git a/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts b/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts index 03da976..c7a43d4 100644 --- a/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts +++ b/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts @@ -1,13 +1,3 @@ - export interface ISkeletonCRUDController { - create(); - readSelected(id); - readPagination(); readEntire(); - updatePartial(); - updatePartialBatch(); - updateEntire(); - updateEntireBatch(); - deleteSelected(); - deleteBatch(); } diff --git a/libs/skeleton/src/skeleton-crud.controller.ts b/libs/skeleton/src/skeleton-crud.controller.ts index 53df221..cf17fbb 100644 --- a/libs/skeleton/src/skeleton-crud.controller.ts +++ b/libs/skeleton/src/skeleton-crud.controller.ts @@ -1,90 +1,16 @@ -import { Delete, Get, Inject, Param, Patch, Post, Put } from '@nestjs/common'; -import { - CREATE_PROCESS, - DELETE_BATCH_PROCESS, - DELETE_PROCESS, - READ_ENTIRE_PROCESS, - READ_PAGINATION_PROCESS, - READ_PROCESS, - UPDATE_BATCH_PROCESS, - UPDATE_ENTIRE_BATCH_PROCESS, - UPDATE_ENTIRE_PROCESS, - UPDATE_PARTIAL_PROCESS, -} from './constants'; +import { Get, Inject } from '@nestjs/common'; +import { READ_ENTIRE_PROCESS } from './constants'; import { DefaultExecutor } from './executors/default.executor'; import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface'; export class SkeletonCRUDController implements ISkeletonCRUDController { constructor( - @Inject(CREATE_PROCESS) - public readonly createProcess, - @Inject(READ_PROCESS) - public readonly readProcess, - @Inject(READ_PAGINATION_PROCESS) - public readonly readPaginationProcess, @Inject(READ_ENTIRE_PROCESS) - public readonly readEntireProcess, - @Inject(UPDATE_PARTIAL_PROCESS) - public readonly updatePartialProcess, - @Inject(UPDATE_BATCH_PROCESS) - public readonly updateBatchProcess, - @Inject(UPDATE_ENTIRE_PROCESS) - public readonly updateEntireProcess, - @Inject(UPDATE_ENTIRE_BATCH_PROCESS) - public readonly updateEntireBatchProcess, - @Inject(DELETE_PROCESS) - public readonly deleteProcess, - @Inject(DELETE_BATCH_PROCESS) - public readonly deleteBatchProcess, + public readonly readEntireProcess: any, ) {} - @Post() - async create(): Promise { - return await DefaultExecutor.bootstrap(this.createProcess); - } - - @Get(':id') - async readSelected(@Param('id') id) { - return await DefaultExecutor.bootstrap(this.readProcess); - } - - @Get() - async readPagination() { - return await DefaultExecutor.bootstrap(this.readPaginationProcess); - } - @Get('list') async readEntire() { return await DefaultExecutor.bootstrap(this.readEntireProcess); } - - @Patch() - async updatePartial() { - return await DefaultExecutor.bootstrap(this.updatePartialProcess); - } - - @Patch('batch') - async updatePartialBatch() { - return await DefaultExecutor.bootstrap(this.updateBatchProcess); - } - - @Put() - async updateEntire() { - return await DefaultExecutor.bootstrap(this.updateEntireProcess); - } - - @Put('batch') - async updateEntireBatch() { - return await DefaultExecutor.bootstrap(this.updateEntireBatchProcess); - } - - @Delete() - async deleteSelected() { - return await DefaultExecutor.bootstrap(this.deleteProcess); - } - - @Delete('batch') - async deleteBatch() { - return await DefaultExecutor.bootstrap(this.deleteBatchProcess); - } } diff --git a/src/app.module.ts b/src/app.module.ts index 19e4b4a..c8ced0a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,11 +1,20 @@ -import { PrismaModule } from '@autocrud/prisma'; +import { PrismaModule, PrismaService } from '@autocrud/prisma'; +import { PRISMA_DELEGATE } from '@autocrud/prisma/constants'; import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +const delegateFactory = { + provide: PRISMA_DELEGATE, + useFactory: (prisma: PrismaService) => { + return prisma.user; + }, + inject: [PrismaService], +}; + @Module({ imports: [PrismaModule], controllers: [AppController], - providers: [AppService], + providers: [AppService, delegateFactory], }) export class AppModule {}