From c1555b520eefe04de34c6a62eb7572da67c6ec7f Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Sun, 10 Nov 2024 11:42:27 +0700 Subject: [PATCH] feat: full working of typeorm --- libs/typeorm/src/processes/delete.process.ts | 4 +++- libs/typeorm/src/processes/list.process.ts | 5 +++- libs/typeorm/src/processes/read.process.ts | 11 +++++++-- libs/typeorm/src/processes/update.process.ts | 9 +++++--- src/app.module.ts | 14 +++++++---- .../prisma/custom/custom.controller.ts | 4 ++-- src/example/prisma/custom/custom.module.ts | 6 ++--- .../prisma/simple/simple.controller.ts | 4 ++-- src/example/prisma/simple/simple.module.ts | 6 ++--- .../typeorm/custom/custom.controller.ts | 5 ++++ src/example/typeorm/custom/custom.module.ts | 19 +++++++++++++++ .../custom/domain/custom.list.process.ts | 13 +++++++++++ .../custom/domain/custom.read.process.ts | 23 +++++++++++++++++++ .../{ => custom}/entities/user.entity.ts | 0 .../typeorm/simple/entities/user.entity.ts | 13 +++++++++++ .../typeorm/{ => simple}/simple.controller.ts | 4 ++-- .../typeorm/{ => simple}/simple.module.ts | 6 ++--- 17 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 src/example/typeorm/custom/custom.controller.ts create mode 100644 src/example/typeorm/custom/custom.module.ts create mode 100644 src/example/typeorm/custom/domain/custom.list.process.ts create mode 100644 src/example/typeorm/custom/domain/custom.read.process.ts rename src/example/typeorm/{ => custom}/entities/user.entity.ts (100%) create mode 100644 src/example/typeorm/simple/entities/user.entity.ts rename src/example/typeorm/{ => simple}/simple.controller.ts (56%) rename src/example/typeorm/{ => simple}/simple.module.ts (66%) diff --git a/libs/typeorm/src/processes/delete.process.ts b/libs/typeorm/src/processes/delete.process.ts index 6549913..70f4c94 100644 --- a/libs/typeorm/src/processes/delete.process.ts +++ b/libs/typeorm/src/processes/delete.process.ts @@ -9,6 +9,8 @@ export class TypeORMDeleteProcess public identityKey: string = 'id'; async process() { - this.result = await this.service.getRepository().delete(this.identityData); + this.result = await this.service.getRepository().delete({ + [this.identityKey]: this.identityData, + }); } } diff --git a/libs/typeorm/src/processes/list.process.ts b/libs/typeorm/src/processes/list.process.ts index f446c28..368ffa2 100644 --- a/libs/typeorm/src/processes/list.process.ts +++ b/libs/typeorm/src/processes/list.process.ts @@ -1,7 +1,10 @@ import { ListProcess } from '@aditama-labs/nest-autocrud/skeleton'; import { TypeORMProcess } from './typeorm.process'; -export class TypeORMListProcess extends TypeORMProcess implements ListProcess { +export class TypeORMListProcess + extends TypeORMProcess + implements ListProcess +{ async process() { this.result = await this.service.getRepository().find(); } diff --git a/libs/typeorm/src/processes/read.process.ts b/libs/typeorm/src/processes/read.process.ts index 51b7d41..cfaaba0 100644 --- a/libs/typeorm/src/processes/read.process.ts +++ b/libs/typeorm/src/processes/read.process.ts @@ -1,11 +1,18 @@ import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton'; import { TypeORMProcess } from './typeorm.process'; -export class TypeORMReadProcess extends TypeORMProcess implements ReadProcess { +export class TypeORMReadProcess + extends TypeORMProcess + implements ReadProcess +{ public identityData; public identityKey: string = 'id'; async process() { - this.result = await this.service.getRepository().findOne(this.identityData); + this.result = await this.service.getRepository().findOne({ + where: { + [this.identityKey]: this.identityData, + }, + }); } } diff --git a/libs/typeorm/src/processes/update.process.ts b/libs/typeorm/src/processes/update.process.ts index ebea74e..9536595 100644 --- a/libs/typeorm/src/processes/update.process.ts +++ b/libs/typeorm/src/processes/update.process.ts @@ -10,8 +10,11 @@ export class TypeORMUpdateProcess public payload; async process() { - this.result = await this.service - .getRepository() - .update(this.identityData, this.payload); + this.result = await this.service.getRepository().update( + { + [this.identityKey]: this.identityData, + }, + this.payload, + ); } } diff --git a/src/app.module.ts b/src/app.module.ts index ed7018c..f65863e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,16 @@ import { Module } from '@nestjs/common'; -import { CustomModule } from './example/prisma/custom/custom.module'; -import { SimpleModule } from './example/prisma/simple/simple.module'; -import { SimpleTypeORMModule } from './example/typeorm/simple.module'; +import { PrismaCustomModule } from './example/prisma/custom/custom.module'; +import { PrismaSimpleModule } from './example/prisma/simple/simple.module'; +import { TypeORMSimpleModule } from './example/typeorm/simple/simple.module'; +import { TypeORMCustomModule } from './example/typeorm/custom/custom.module'; @Module({ - imports: [SimpleTypeORMModule], + imports: [ + PrismaCustomModule, + PrismaSimpleModule, + TypeORMSimpleModule, + TypeORMCustomModule, + ], controllers: [], providers: [], }) diff --git a/src/example/prisma/custom/custom.controller.ts b/src/example/prisma/custom/custom.controller.ts index beb6dee..27dd0a4 100644 --- a/src/example/prisma/custom/custom.controller.ts +++ b/src/example/prisma/custom/custom.controller.ts @@ -1,8 +1,8 @@ import { CustomCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; import { Controller, Get } from '@nestjs/common'; -@Controller('example/custom') -export class CustomController extends CustomCRUDController({ +@Controller('example/prisma/custom') +export class PrismaCustomController extends CustomCRUDController({ uniqueIdentifier: 'username', }) { @Get('list') diff --git a/src/example/prisma/custom/custom.module.ts b/src/example/prisma/custom/custom.module.ts index c049dec..1ee7224 100644 --- a/src/example/prisma/custom/custom.module.ts +++ b/src/example/prisma/custom/custom.module.ts @@ -1,7 +1,7 @@ import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma'; import { Module } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; -import { CustomController } from './custom.controller'; +import { PrismaCustomController } from './custom.controller'; import { CustomListProcess } from './domain/custom.list.process'; import { CustomReadProcess } from './domain/custom.read.process'; @@ -13,7 +13,7 @@ import { CustomReadProcess } from './domain/custom.read.process'; processRead: CustomReadProcess, }), ], - controllers: [CustomController], + controllers: [PrismaCustomController], providers: [], }) -export class CustomModule {} +export class PrismaCustomModule {} diff --git a/src/example/prisma/simple/simple.controller.ts b/src/example/prisma/simple/simple.controller.ts index 9a8812e..0cb2713 100644 --- a/src/example/prisma/simple/simple.controller.ts +++ b/src/example/prisma/simple/simple.controller.ts @@ -1,5 +1,5 @@ import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; import { Controller } from '@nestjs/common'; -@Controller('example/simple') -export class SimpleController extends SkeletonCRUDController {} +@Controller('example/prisma/simple') +export class PrismaSimpleController extends SkeletonCRUDController {} diff --git a/src/example/prisma/simple/simple.module.ts b/src/example/prisma/simple/simple.module.ts index 6026d09..95e8d03 100644 --- a/src/example/prisma/simple/simple.module.ts +++ b/src/example/prisma/simple/simple.module.ts @@ -1,7 +1,7 @@ import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma'; import { Module } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; -import { SimpleController } from './simple.controller'; +import { PrismaSimpleController } from './simple.controller'; @Module({ imports: [ @@ -9,7 +9,7 @@ import { SimpleController } from './simple.controller'; delegate: (prisma: PrismaClient) => prisma.user, }), ], - controllers: [SimpleController], + controllers: [PrismaSimpleController], providers: [], }) -export class SimpleModule {} +export class PrismaSimpleModule {} diff --git a/src/example/typeorm/custom/custom.controller.ts b/src/example/typeorm/custom/custom.controller.ts new file mode 100644 index 0000000..d6039d0 --- /dev/null +++ b/src/example/typeorm/custom/custom.controller.ts @@ -0,0 +1,5 @@ +import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; +import { Controller } from '@nestjs/common'; + +@Controller('example/typeorm/custom') +export class TypeORMCustomController extends SkeletonCRUDController {} diff --git a/src/example/typeorm/custom/custom.module.ts b/src/example/typeorm/custom/custom.module.ts new file mode 100644 index 0000000..01c88fe --- /dev/null +++ b/src/example/typeorm/custom/custom.module.ts @@ -0,0 +1,19 @@ +import { TypeORMModule } from '@aditama-labs/nest-autocrud/typeorm'; +import { Module } from '@nestjs/common'; +import { TypeORMCustomController } from './custom.controller'; +import { UserEntity } from './entities/user.entity'; +import { CustomListProcess } from './domain/custom.list.process'; +import { CustomReadProcess } from './domain/custom.read.process'; + +@Module({ + imports: [ + TypeORMModule.forRoot({ + entity: UserEntity, + processRead: CustomReadProcess, + processList: CustomListProcess, + }), + ], + controllers: [TypeORMCustomController], + providers: [], +}) +export class TypeORMCustomModule {} diff --git a/src/example/typeorm/custom/domain/custom.list.process.ts b/src/example/typeorm/custom/domain/custom.list.process.ts new file mode 100644 index 0000000..ed6e4bb --- /dev/null +++ b/src/example/typeorm/custom/domain/custom.list.process.ts @@ -0,0 +1,13 @@ +import { TypeORMListProcess } from '@aditama-labs/nest-autocrud/typeorm/processes'; +import { UserEntity } from '../entities/user.entity'; + +export class CustomListProcess extends TypeORMListProcess { + async before(): Promise { + console.log('This is custom logic before query to database'); + } + + output() { + console.log('You can modify the output here'); + return super.output(); + } +} diff --git a/src/example/typeorm/custom/domain/custom.read.process.ts b/src/example/typeorm/custom/domain/custom.read.process.ts new file mode 100644 index 0000000..07a6292 --- /dev/null +++ b/src/example/typeorm/custom/domain/custom.read.process.ts @@ -0,0 +1,23 @@ +import { TypeORMReadProcess } from '@aditama-labs/nest-autocrud/typeorm/processes'; +import { Injectable } from '@nestjs/common'; +import { UserEntity } from '../entities/user.entity'; + +@Injectable() +export class CustomReadProcess extends TypeORMReadProcess { + customResult; + + async before(): Promise { + console.log('The ID requested in path parameter', this.identityData); + } + + async after(): Promise { + this.customResult = { + ...super.output(), + custom_code: 'XXXX', + }; + } + + output() { + return this.customResult; + } +} diff --git a/src/example/typeorm/entities/user.entity.ts b/src/example/typeorm/custom/entities/user.entity.ts similarity index 100% rename from src/example/typeorm/entities/user.entity.ts rename to src/example/typeorm/custom/entities/user.entity.ts diff --git a/src/example/typeorm/simple/entities/user.entity.ts b/src/example/typeorm/simple/entities/user.entity.ts new file mode 100644 index 0000000..77cc119 --- /dev/null +++ b/src/example/typeorm/simple/entities/user.entity.ts @@ -0,0 +1,13 @@ +import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity('account') +export class UserEntity { + @PrimaryGeneratedColumn('uuid') + id: string; + + @Column({ type: String }) + username: string; + + @Column({ type: String }) + name: string; +} diff --git a/src/example/typeorm/simple.controller.ts b/src/example/typeorm/simple/simple.controller.ts similarity index 56% rename from src/example/typeorm/simple.controller.ts rename to src/example/typeorm/simple/simple.controller.ts index 4b401b2..911e903 100644 --- a/src/example/typeorm/simple.controller.ts +++ b/src/example/typeorm/simple/simple.controller.ts @@ -1,5 +1,5 @@ import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; import { Controller } from '@nestjs/common'; -@Controller('example/simple/typeorm') -export class SimpleTypeORMController extends SkeletonCRUDController {} +@Controller('example/typeorm/simple') +export class TypeORMSimpleController extends SkeletonCRUDController {} diff --git a/src/example/typeorm/simple.module.ts b/src/example/typeorm/simple/simple.module.ts similarity index 66% rename from src/example/typeorm/simple.module.ts rename to src/example/typeorm/simple/simple.module.ts index 27c3568..aa19415 100644 --- a/src/example/typeorm/simple.module.ts +++ b/src/example/typeorm/simple/simple.module.ts @@ -1,7 +1,7 @@ import { TypeORMModule } from '@aditama-labs/nest-autocrud/typeorm'; import { Module } from '@nestjs/common'; -import { SimpleTypeORMController } from './simple.controller'; import { UserEntity } from './entities/user.entity'; +import { TypeORMSimpleController } from './simple.controller'; @Module({ imports: [ @@ -9,7 +9,7 @@ import { UserEntity } from './entities/user.entity'; entity: UserEntity, }), ], - controllers: [SimpleTypeORMController], + controllers: [TypeORMSimpleController], providers: [], }) -export class SimpleTypeORMModule {} +export class TypeORMSimpleModule {}