From 10f1e644b2a7d5ede62227a670061cd910480c91 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Sun, 27 Oct 2024 00:07:59 +0700 Subject: [PATCH] wip: injection --- libs/prisma/src/prisma.module.ts | 32 ++++++++++++++++++- libs/prisma/src/processes/create.process.ts | 16 +++++++--- libs/prisma/src/processes/list.process.ts | 13 +++++++- libs/prisma/src/processes/prisma.process.ts | 13 ++++++++ libs/skeleton/src/constants.ts | 11 +++++++ libs/skeleton/src/index.ts | 2 ++ .../skeleton/src/processes/default.process.ts | 4 ++- libs/skeleton/src/skeleton-crud.controller.ts | 24 +++++++++++++- src/app.controller.ts | 10 ++---- 9 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 libs/prisma/src/processes/prisma.process.ts create mode 100644 libs/skeleton/src/constants.ts diff --git a/libs/prisma/src/prisma.module.ts b/libs/prisma/src/prisma.module.ts index ec0ce32..d63df3f 100644 --- a/libs/prisma/src/prisma.module.ts +++ b/libs/prisma/src/prisma.module.ts @@ -1,8 +1,38 @@ +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 { 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], + 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], }) export class PrismaModule {} diff --git a/libs/prisma/src/processes/create.process.ts b/libs/prisma/src/processes/create.process.ts index adf2efb..49018be 100644 --- a/libs/prisma/src/processes/create.process.ts +++ b/libs/prisma/src/processes/create.process.ts @@ -1,9 +1,17 @@ import { CreateProcess } from '@autocrud/skeleton/processes/create.process'; +import { PrismaProcess } from './prisma.process'; + +export class PrismaCreateProcess + extends PrismaProcess + implements CreateProcess +{ + private data; + + setData(data) { + this.data = data; + } -export class PrismaCreateProcess extends CreateProcess { async process(): Promise { - this.prisma['asd'].create({ - data, - }); + await this.getDelegate().create(this.data); } } diff --git a/libs/prisma/src/processes/list.process.ts b/libs/prisma/src/processes/list.process.ts index 71c8404..9243f5f 100644 --- a/libs/prisma/src/processes/list.process.ts +++ b/libs/prisma/src/processes/list.process.ts @@ -1,3 +1,14 @@ import { ListProcess } from '@autocrud/skeleton/processes/list.process'; +import { PrismaProcess } from './prisma.process'; -export class PrismaListProcess extends ListProcess {} +export class PrismaListProcess extends PrismaProcess implements ListProcess { + private data; + + async process(): Promise { + this.data = await this.getDelegate().findMany(); + } + + result() { + return this.data; + } +} diff --git a/libs/prisma/src/processes/prisma.process.ts b/libs/prisma/src/processes/prisma.process.ts new file mode 100644 index 0000000..2ecd528 --- /dev/null +++ b/libs/prisma/src/processes/prisma.process.ts @@ -0,0 +1,13 @@ +import { DefaultProcess } from '@autocrud/skeleton/processes/default.process'; + +export class PrismaProcess extends DefaultProcess { + protected delegate; + + setDelegate(delegate) { + this.delegate = delegate; + } + + get getDelegate() { + return this.delegate; + } +} diff --git a/libs/skeleton/src/constants.ts b/libs/skeleton/src/constants.ts new file mode 100644 index 0000000..7af786f --- /dev/null +++ b/libs/skeleton/src/constants.ts @@ -0,0 +1,11 @@ +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/index.ts b/libs/skeleton/src/index.ts index 79b7acf..c72e6fa 100644 --- a/libs/skeleton/src/index.ts +++ b/libs/skeleton/src/index.ts @@ -1 +1,3 @@ +export * from './constants'; export * from './skeleton-crud.controller'; + diff --git a/libs/skeleton/src/processes/default.process.ts b/libs/skeleton/src/processes/default.process.ts index 43593c1..b66b379 100644 --- a/libs/skeleton/src/processes/default.process.ts +++ b/libs/skeleton/src/processes/default.process.ts @@ -8,5 +8,7 @@ export class DefaultProcess implements ISkeletonProcess { async end(): Promise {} async after(): Promise {} - result(): any {} + result(): any { + return 'Hello World'; + } } diff --git a/libs/skeleton/src/skeleton-crud.controller.ts b/libs/skeleton/src/skeleton-crud.controller.ts index 51bfa77..53df221 100644 --- a/libs/skeleton/src/skeleton-crud.controller.ts +++ b/libs/skeleton/src/skeleton-crud.controller.ts @@ -1,18 +1,40 @@ -import { Delete, Get, Param, Patch, Post, Put } from '@nestjs/common'; +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 { 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, ) {} diff --git a/src/app.controller.ts b/src/app.controller.ts index 1b2aaa9..95abbd1 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,11 +1,5 @@ -import { PrismaCRUDController } from '@autocrud/prisma/prisma-crud.controller'; +import { SkeletonCRUDController } from '@autocrud/skeleton'; import { Controller } from '@nestjs/common'; @Controller('hello') -export class AppController extends PrismaCRUDController { - constructor() {} - - // Read(): void { - // console.log(this.appService.getHello()); - // } -} +export class AppController extends SkeletonCRUDController {}