diff --git a/libs/skeleton/src/entities/index.ts b/libs/skeleton/src/entities/index.ts deleted file mode 100644 index ab68122..0000000 --- a/libs/skeleton/src/entities/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './pagination.entity'; diff --git a/libs/skeleton/src/entities/pagination.entity.ts b/libs/skeleton/src/entities/pagination.entity.ts deleted file mode 100644 index 78d3500..0000000 --- a/libs/skeleton/src/entities/pagination.entity.ts +++ /dev/null @@ -1,13 +0,0 @@ -export interface IPaginationEntity { - // Data related - data: T[]; - totalData: number; - - // Page related - totalPage: number; - currentPage: number; - lastPage: number; - perPage: number; - nextPage: number | null; - previousPage: number | null; -} diff --git a/libs/skeleton/src/index.ts b/libs/skeleton/src/index.ts index 2152dfe..4511b5e 100644 --- a/libs/skeleton/src/index.ts +++ b/libs/skeleton/src/index.ts @@ -1,8 +1,6 @@ export * from './constants'; export * from './dto'; -export * from './entities'; export * from './executors'; export * from './interfaces'; export * from './processes'; export * from './skeleton-crud.controller'; - diff --git a/libs/typeorm/src/interfaces/config-module-options.interface.ts b/libs/typeorm/src/interfaces/config-module-options.interface.ts index 596fc01..0ecdb5d 100644 --- a/libs/typeorm/src/interfaces/config-module-options.interface.ts +++ b/libs/typeorm/src/interfaces/config-module-options.interface.ts @@ -1,8 +1,13 @@ -import { EntityTarget, FindOptionsWhere, ObjectLiteral } from "typeorm"; +import { EntityTarget, ObjectLiteral } from 'typeorm'; export interface TypeORMModuleOptions { entity: EntityTarget; + processCreate?; + processDelete?; + processList?; + processPagination?; + processRead?; + processUpdate?; synchronize?: boolean; - entities: string[]; - uniqueWhereClause: FindOptionsWhere; + entities?: string[]; } diff --git a/libs/typeorm/src/processes/delete.process.ts b/libs/typeorm/src/processes/delete.process.ts index 9efb4a9..6549913 100644 --- a/libs/typeorm/src/processes/delete.process.ts +++ b/libs/typeorm/src/processes/delete.process.ts @@ -9,6 +9,6 @@ export class TypeORMDeleteProcess public identityKey: string = 'id'; async process() { - this.result = await this.service.getRepository().delete(this.uniqueWhereClause); + this.result = await this.service.getRepository().delete(this.identityData); } } diff --git a/libs/typeorm/src/processes/read.process.ts b/libs/typeorm/src/processes/read.process.ts index aae49cf..51b7d41 100644 --- a/libs/typeorm/src/processes/read.process.ts +++ b/libs/typeorm/src/processes/read.process.ts @@ -6,8 +6,6 @@ export class TypeORMReadProcess extends TypeORMProcess implements ReadProc public identityKey: string = 'id'; async process() { - this.result = await this.service.getRepository().findOne({ - where: this.uniqueWhereClause, - }); + this.result = await this.service.getRepository().findOne(this.identityData); } } diff --git a/libs/typeorm/src/processes/typeorm.process.ts b/libs/typeorm/src/processes/typeorm.process.ts index 1536697..16d39e5 100644 --- a/libs/typeorm/src/processes/typeorm.process.ts +++ b/libs/typeorm/src/processes/typeorm.process.ts @@ -1,16 +1,10 @@ import { DefaultProcess } from '@aditama-labs/nest-autocrud/skeleton'; -import { Inject, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { TypeORMService } from '../typeorm.service'; -import { TYPEORM_WHERE_CLAUSE } from '../constants'; -import { FindOptionsWhere } from 'typeorm'; @Injectable() export class TypeORMProcess extends DefaultProcess { - constructor( - public service: TypeORMService, - @Inject(TYPEORM_WHERE_CLAUSE) - public uniqueWhereClause: FindOptionsWhere, - ) { + constructor(public service: TypeORMService) { super(); } } diff --git a/libs/typeorm/src/processes/update.process.ts b/libs/typeorm/src/processes/update.process.ts index 4eff20c..ebea74e 100644 --- a/libs/typeorm/src/processes/update.process.ts +++ b/libs/typeorm/src/processes/update.process.ts @@ -12,6 +12,6 @@ export class TypeORMUpdateProcess async process() { this.result = await this.service .getRepository() - .update(this.uniqueWhereClause, this.payload); + .update(this.identityData, this.payload); } } diff --git a/libs/typeorm/src/typeorm.module.ts b/libs/typeorm/src/typeorm.module.ts index 71b292d..3d3f224 100644 --- a/libs/typeorm/src/typeorm.module.ts +++ b/libs/typeorm/src/typeorm.module.ts @@ -1,12 +1,25 @@ import { DynamicModule, Module } from '@nestjs/common'; import { DataSource, ObjectLiteral } from 'typeorm'; -import { - TYPEORM_DATASOURCE, - TYPEORM_REPOSITORY, - TYPEORM_WHERE_CLAUSE, -} from './constants'; +import { TYPEORM_DATASOURCE, TYPEORM_REPOSITORY } from './constants'; import { TypeORMModuleOptions } from './interfaces'; import { TypeORMService } from './typeorm.service'; +import { + CREATE_PROCESS, + DELETE_PROCESS, + LIST_PROCESS, + PAGINATION_PROCESS, + READ_PROCESS, + UPDATE_PROCESS, +} from '@aditama-labs/nest-autocrud/skeleton'; +import { + TypeORMCreateProcess, + TypeORMDeleteProcess, + TypeORMListProcess, + TypeORMPaginationProcess, + TypeORMReadProcess, + TypeORMUpdateProcess, +} from './processes'; +import { TypeOrmModule } from '@nestjs/typeorm'; const getDatabaseCredential = ( synchronize?: boolean, @@ -19,8 +32,9 @@ const getDatabaseCredential = ( entityList = entities; } - let protocol; - switch (url.protocol) { + const defaultProtocol = url.protocol.substring(0, url.protocol.length - 1); + let protocol = defaultProtocol; + switch (defaultProtocol) { case 'postgresql': protocol = 'postgres'; break; @@ -49,6 +63,27 @@ const getDatabaseCredential = ( @Module({}) export class TypeORMModule { + private static autoPresetProvider(providers, option, key, preset) { + if (option) { + providers = [ + ...providers, + { + provide: key, + useClass: option, + }, + ]; + } else { + providers = [ + ...providers, + { + provide: key, + useClass: preset, + }, + ]; + } + return providers; + } + static forRoot( options: TypeORMModuleOptions, ): DynamicModule { @@ -65,22 +100,69 @@ export class TypeORMModule { }, { provide: TYPEORM_REPOSITORY, - useFactory: (dataSource: DataSource) => - dataSource.getRepository(options.entity), + useFactory: (dataSource: DataSource) => { + return dataSource.getRepository(options.entity); + }, inject: [TYPEORM_DATASOURCE], }, ]; + providers = TypeORMModule.autoPresetProvider( + providers, + options.processCreate, + CREATE_PROCESS, + TypeORMCreateProcess, + ); + + providers = TypeORMModule.autoPresetProvider( + providers, + options.processDelete, + DELETE_PROCESS, + TypeORMDeleteProcess, + ); + + providers = TypeORMModule.autoPresetProvider( + providers, + options.processList, + LIST_PROCESS, + TypeORMListProcess, + ); + + providers = TypeORMModule.autoPresetProvider( + providers, + options.processPagination, + PAGINATION_PROCESS, + TypeORMPaginationProcess, + ); + + providers = TypeORMModule.autoPresetProvider( + providers, + options.processRead, + READ_PROCESS, + TypeORMReadProcess, + ); + + providers = TypeORMModule.autoPresetProvider( + providers, + options.processUpdate, + UPDATE_PROCESS, + TypeORMUpdateProcess, + ); + return { module: TypeORMModule, - providers: [ - ...providers, - { - provide: TYPEORM_WHERE_CLAUSE, - useValue: options.uniqueWhereClause, - }, + providers: [...providers], + exports: [ + TYPEORM_DATASOURCE, + TYPEORM_REPOSITORY, + // List of Process + CREATE_PROCESS, + DELETE_PROCESS, + LIST_PROCESS, + PAGINATION_PROCESS, + READ_PROCESS, + UPDATE_PROCESS, ], - exports: providers, }; } } diff --git a/src/app.module.ts b/src/app.module.ts index d3bf012..ed7018c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,12 +1,10 @@ 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'; @Module({ - imports: [ - SimpleModule, - CustomModule, - ], + imports: [SimpleTypeORMModule], controllers: [], providers: [], }) diff --git a/src/example/typeorm/entities/user.entity.ts b/src/example/typeorm/entities/user.entity.ts new file mode 100644 index 0000000..b9d0e1f --- /dev/null +++ b/src/example/typeorm/entities/user.entity.ts @@ -0,0 +1,13 @@ +import { Column, Entity, 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.controller.ts new file mode 100644 index 0000000..4b401b2 --- /dev/null +++ b/src/example/typeorm/simple.controller.ts @@ -0,0 +1,5 @@ +import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; +import { Controller } from '@nestjs/common'; + +@Controller('example/simple/typeorm') +export class SimpleTypeORMController extends SkeletonCRUDController {} diff --git a/src/example/typeorm/simple.module.ts b/src/example/typeorm/simple.module.ts new file mode 100644 index 0000000..27c3568 --- /dev/null +++ b/src/example/typeorm/simple.module.ts @@ -0,0 +1,15 @@ +import { TypeORMModule } from '@aditama-labs/nest-autocrud/typeorm'; +import { Module } from '@nestjs/common'; +import { SimpleTypeORMController } from './simple.controller'; +import { UserEntity } from './entities/user.entity'; + +@Module({ + imports: [ + TypeORMModule.forRoot({ + entity: UserEntity, + }), + ], + controllers: [SimpleTypeORMController], + providers: [], +}) +export class SimpleTypeORMModule {}