feat: full working of typeorm

This commit is contained in:
Supan Adit Pratama 2024-11-10 11:42:27 +07:00
parent 7424b38798
commit c1555b520e
17 changed files with 120 additions and 26 deletions

View File

@ -9,6 +9,8 @@ export class TypeORMDeleteProcess<T>
public identityKey: string = 'id'; public identityKey: string = 'id';
async process() { async process() {
this.result = await this.service.getRepository().delete(this.identityData); this.result = await this.service.getRepository().delete(<any>{
[this.identityKey]: this.identityData,
});
} }
} }

View File

@ -1,7 +1,10 @@
import { ListProcess } from '@aditama-labs/nest-autocrud/skeleton'; import { ListProcess } from '@aditama-labs/nest-autocrud/skeleton';
import { TypeORMProcess } from './typeorm.process'; import { TypeORMProcess } from './typeorm.process';
export class TypeORMListProcess<T> extends TypeORMProcess<T> implements ListProcess { export class TypeORMListProcess<T>
extends TypeORMProcess<T>
implements ListProcess
{
async process() { async process() {
this.result = await this.service.getRepository().find(); this.result = await this.service.getRepository().find();
} }

View File

@ -1,11 +1,18 @@
import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton'; import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton';
import { TypeORMProcess } from './typeorm.process'; import { TypeORMProcess } from './typeorm.process';
export class TypeORMReadProcess<T> extends TypeORMProcess<T> implements ReadProcess { export class TypeORMReadProcess<T>
extends TypeORMProcess<T>
implements ReadProcess
{
public identityData; public identityData;
public identityKey: string = 'id'; public identityKey: string = 'id';
async process() { async process() {
this.result = await this.service.getRepository().findOne(this.identityData); this.result = await this.service.getRepository().findOne({
where: <any>{
[this.identityKey]: this.identityData,
},
});
} }
} }

View File

@ -10,8 +10,11 @@ export class TypeORMUpdateProcess<T>
public payload; public payload;
async process() { async process() {
this.result = await this.service this.result = await this.service.getRepository().update(
.getRepository() <any>{
.update(this.identityData, this.payload); [this.identityKey]: this.identityData,
},
this.payload,
);
} }
} }

View File

@ -1,10 +1,16 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { CustomModule } from './example/prisma/custom/custom.module'; import { PrismaCustomModule } from './example/prisma/custom/custom.module';
import { SimpleModule } from './example/prisma/simple/simple.module'; import { PrismaSimpleModule } from './example/prisma/simple/simple.module';
import { SimpleTypeORMModule } from './example/typeorm/simple.module'; import { TypeORMSimpleModule } from './example/typeorm/simple/simple.module';
import { TypeORMCustomModule } from './example/typeorm/custom/custom.module';
@Module({ @Module({
imports: [SimpleTypeORMModule], imports: [
PrismaCustomModule,
PrismaSimpleModule,
TypeORMSimpleModule,
TypeORMCustomModule,
],
controllers: [], controllers: [],
providers: [], providers: [],
}) })

View File

@ -1,8 +1,8 @@
import { CustomCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; import { CustomCRUDController } from '@aditama-labs/nest-autocrud/skeleton';
import { Controller, Get } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
@Controller('example/custom') @Controller('example/prisma/custom')
export class CustomController extends CustomCRUDController({ export class PrismaCustomController extends CustomCRUDController({
uniqueIdentifier: 'username', uniqueIdentifier: 'username',
}) { }) {
@Get('list') @Get('list')

View File

@ -1,7 +1,7 @@
import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma'; import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { CustomController } from './custom.controller'; import { PrismaCustomController } from './custom.controller';
import { CustomListProcess } from './domain/custom.list.process'; import { CustomListProcess } from './domain/custom.list.process';
import { CustomReadProcess } from './domain/custom.read.process'; import { CustomReadProcess } from './domain/custom.read.process';
@ -13,7 +13,7 @@ import { CustomReadProcess } from './domain/custom.read.process';
processRead: CustomReadProcess, processRead: CustomReadProcess,
}), }),
], ],
controllers: [CustomController], controllers: [PrismaCustomController],
providers: [], providers: [],
}) })
export class CustomModule {} export class PrismaCustomModule {}

View File

@ -1,5 +1,5 @@
import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton';
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
@Controller('example/simple') @Controller('example/prisma/simple')
export class SimpleController extends SkeletonCRUDController {} export class PrismaSimpleController extends SkeletonCRUDController {}

View File

@ -1,7 +1,7 @@
import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma'; import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { SimpleController } from './simple.controller'; import { PrismaSimpleController } from './simple.controller';
@Module({ @Module({
imports: [ imports: [
@ -9,7 +9,7 @@ import { SimpleController } from './simple.controller';
delegate: (prisma: PrismaClient) => prisma.user, delegate: (prisma: PrismaClient) => prisma.user,
}), }),
], ],
controllers: [SimpleController], controllers: [PrismaSimpleController],
providers: [], providers: [],
}) })
export class SimpleModule {} export class PrismaSimpleModule {}

View File

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

View File

@ -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<UserEntity>({
entity: UserEntity,
processRead: CustomReadProcess,
processList: CustomListProcess,
}),
],
controllers: [TypeORMCustomController],
providers: [],
})
export class TypeORMCustomModule {}

View File

@ -0,0 +1,13 @@
import { TypeORMListProcess } from '@aditama-labs/nest-autocrud/typeorm/processes';
import { UserEntity } from '../entities/user.entity';
export class CustomListProcess extends TypeORMListProcess<UserEntity> {
async before(): Promise<any> {
console.log('This is custom logic before query to database');
}
output() {
console.log('You can modify the output here');
return super.output();
}
}

View File

@ -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<UserEntity> {
customResult;
async before(): Promise<any> {
console.log('The ID requested in path parameter', this.identityData);
}
async after(): Promise<any> {
this.customResult = {
...super.output(),
custom_code: 'XXXX',
};
}
output() {
return this.customResult;
}
}

View File

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

View File

@ -1,5 +1,5 @@
import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton';
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
@Controller('example/simple/typeorm') @Controller('example/typeorm/simple')
export class SimpleTypeORMController extends SkeletonCRUDController {} export class TypeORMSimpleController extends SkeletonCRUDController {}

View File

@ -1,7 +1,7 @@
import { TypeORMModule } from '@aditama-labs/nest-autocrud/typeorm'; import { TypeORMModule } from '@aditama-labs/nest-autocrud/typeorm';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { SimpleTypeORMController } from './simple.controller';
import { UserEntity } from './entities/user.entity'; import { UserEntity } from './entities/user.entity';
import { TypeORMSimpleController } from './simple.controller';
@Module({ @Module({
imports: [ imports: [
@ -9,7 +9,7 @@ import { UserEntity } from './entities/user.entity';
entity: UserEntity, entity: UserEntity,
}), }),
], ],
controllers: [SimpleTypeORMController], controllers: [TypeORMSimpleController],
providers: [], providers: [],
}) })
export class SimpleTypeORMModule {} export class TypeORMSimpleModule {}