wip: remove all generic caused by prisma doesn't support by default

This commit is contained in:
Supan Adit Pratama 2024-10-26 21:35:16 +07:00
parent a1576273bd
commit 4d5ac919be
27 changed files with 257 additions and 150 deletions

View File

@ -26,6 +26,10 @@
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
## Todo
- [] Create custom generic for Prisma ORM Support [#5273](https://github.com/prisma/prisma/issues/5273)
## Project setup ## Project setup
```bash ```bash

View File

@ -0,0 +1,3 @@
import { SkeletonCRUDController } from '@autocrud/skeleton';
export class PrismaCRUDController extends SkeletonCRUDController {}

View File

@ -0,0 +1,11 @@
import { BatchDeleteProcess } from '@autocrud/skeleton/processes/batch/delete.process';
export abstract class PrismaBatchDeleteProcess implements BatchDeleteProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,11 @@
import { BatchUpdateProcess } from '@autocrud/skeleton/processes/batch/update.process';
export abstract class PrismaBatchUpdateProcess implements BatchUpdateProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,18 @@
import { CreateProcess } from '@autocrud/skeleton/processes/create.process';
import { Prisma, PrismaClient } from '@prisma/client';
export abstract class PrismaCreateProcess implements CreateProcess {
constructor(private readonly prisma: PrismaClient) {}
abstract initialization(): T;
abstract before(): T;
abstract begin(): T;
process(): T {
this.prisma['asd'].create({
data,
});
}
abstract end(): T;
abstract after(): T;
abstract result(): R;
}

View File

@ -0,0 +1,12 @@
import { DeleteProcess } from '@autocrud/skeleton/processes/delete.process';
import { Prisma } from '@prisma/client';
export abstract class PrismaDeleteProcess implements DeleteProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,11 @@
import { ListProcess } from '@autocrud/skeleton/processes/list.process';
export abstract class PrismaListProcess implements ListProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,11 @@
import { PaginationProcess } from '@autocrud/skeleton/processes/pagination.process';
export abstract class PrismaPaginationProcess implements PaginationProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,13 @@
import { PartialBatchUpdateProcess } from '@autocrud/skeleton/processes/partial/batch/update.process';
export abstract class PrismaPartialBatchUpdateProcess
implements PartialBatchUpdateProcess
{
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,13 @@
import { PartialUpdateProcess } from '@autocrud/skeleton/processes/partial/update.process';
export abstract class PrismaPartialUpdateProcess
implements PartialUpdateProcess
{
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,11 @@
import { ReadProcess } from '@autocrud/skeleton/processes/read.process';
export abstract class PrismaReadProcess implements ReadProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -0,0 +1,11 @@
import { UpdateProcess } from '@autocrud/skeleton/processes/update.process';
export abstract class PrismaUpdateProcess implements UpdateProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -1,14 +1,13 @@
import { IPaginationEntity } from '@autocrud/skeleton/entities/pagination.entity';
export interface ISkeletonCRUDController<T, E> { export interface ISkeletonCRUDController {
create(): Promise<T>; create();
readSelected(id: E): Promise<T>; readSelected(id);
readPagination(): Promise<IPaginationEntity<T>>; readPagination();
readEntire(): Promise<T[]>; readEntire();
updatePartial(): Promise<T>; updatePartial();
updatePartialBatch(): Promise<T[]>; updatePartialBatch();
updateEntire(): Promise<T>; updateEntire();
updateEntireBatch(): Promise<T[]>; updateEntireBatch();
deleteSelected(): Promise<T>; deleteSelected();
deleteBatch(): Promise<T[]>; deleteBatch();
} }

View File

@ -1,10 +1,10 @@
export interface ISkeletonProcess<T, R> { export interface ISkeletonProcess {
initialization(): T; initialization();
before(): T; before();
begin(): T; begin();
process(): T; process();
end(): T; end();
after(): T; after();
result(): R; result();
} }

View File

@ -1,13 +1,11 @@
import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface';
export abstract class BatchDeleteProcess<T, R> export abstract class BatchDeleteProcess implements ISkeletonProcess {
implements ISkeletonProcess<T, R> abstract initialization();
{ abstract before();
abstract initialization(): T; abstract begin();
abstract before(): T; abstract process();
abstract begin(): T; abstract end();
abstract process(): T; abstract after();
abstract end(): T; abstract result();
abstract after(): T;
abstract result(): R;
} }

View File

@ -1,13 +1,11 @@
import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface';
export abstract class BatchUpdateProcess<T, R> export abstract class BatchUpdateProcess implements ISkeletonProcess {
implements ISkeletonProcess<T, R> abstract initialization();
{ abstract before();
abstract initialization(): T; abstract begin();
abstract before(): T; abstract process();
abstract begin(): T; abstract end();
abstract process(): T; abstract after();
abstract end(): T; abstract result();
abstract after(): T;
abstract result(): R;
} }

View File

@ -1,11 +1,11 @@
import { ISkeletonProcess } from '../interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '../interfaces/skeleton-process.interface';
export abstract class CreateProcess<T, R> implements ISkeletonProcess<T, R> { export abstract class CreateProcess implements ISkeletonProcess {
abstract initialization(): T; abstract initialization();
abstract before(): T; abstract before();
abstract begin(): T; abstract begin();
abstract process(): T; abstract process();
abstract end(): T; abstract end();
abstract after(): T; abstract after();
abstract result(): R; abstract result();
} }

View File

@ -1,11 +1,11 @@
import { ISkeletonProcess } from '../interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '../interfaces/skeleton-process.interface';
export abstract class DeleteProcess<T, R> implements ISkeletonProcess<T, R> { export abstract class DeleteProcess implements ISkeletonProcess {
abstract initialization(): T; abstract initialization();
abstract before(): T; abstract before();
abstract begin(): T; abstract begin();
abstract process(): T; abstract process();
abstract end(): T; abstract end();
abstract after(): T; abstract after();
abstract result(): R; abstract result();
} }

View File

@ -1,11 +1,11 @@
import { ISkeletonProcess } from '../interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '../interfaces/skeleton-process.interface';
export abstract class ListProcess<T, R> implements ISkeletonProcess<T, R> { export abstract class ListProcess implements ISkeletonProcess {
abstract initialization(): T; abstract initialization();
abstract before(): T; abstract before();
abstract begin(): T; abstract begin();
abstract process(): T; abstract process();
abstract end(): T; abstract end();
abstract after(): T; abstract after();
abstract result(): R; abstract result();
} }

View File

@ -1,11 +1,11 @@
import { ISkeletonProcess } from '../interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '../interfaces/skeleton-process.interface';
export abstract class PaginationProcess<T, R> implements ISkeletonProcess<T, R> { export abstract class PaginationProcess implements ISkeletonProcess {
abstract initialization(): T; abstract initialization();
abstract before(): T; abstract before();
abstract begin(): T; abstract begin();
abstract process(): T; abstract process();
abstract end(): T; abstract end();
abstract after(): T; abstract after();
abstract result(): R; abstract result();
} }

View File

@ -1,13 +1,11 @@
import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface';
export abstract class PartialBatchUpdateProcess<T, R> export abstract class PartialBatchUpdateProcess implements ISkeletonProcess {
implements ISkeletonProcess<T, R> abstract initialization();
{ abstract before();
abstract initialization(): T; abstract begin();
abstract before(): T; abstract process();
abstract begin(): T; abstract end();
abstract process(): T; abstract after();
abstract end(): T; abstract result();
abstract after(): T;
abstract result(): R;
} }

View File

@ -0,0 +1,11 @@
import { ISkeletonProcess } from '@autocrud/skeleton/interfaces/skeleton-process.interface';
export abstract class PartialUpdateProcess implements ISkeletonProcess {
abstract initialization();
abstract before();
abstract begin();
abstract process();
abstract end();
abstract after();
abstract result();
}

View File

@ -1,11 +1,11 @@
import { ISkeletonProcess } from '../interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '../interfaces/skeleton-process.interface';
export abstract class ReadProcess<T, R> implements ISkeletonProcess<T, R> { export abstract class ReadProcess implements ISkeletonProcess {
abstract initialization(): T; abstract initialization();
abstract before(): T; abstract before();
abstract begin(): T; abstract begin();
abstract process(): T; abstract process();
abstract end(): T; abstract end();
abstract after(): T; abstract after();
abstract result(): R; abstract result();
} }

View File

@ -1,11 +1,11 @@
import { ISkeletonProcess } from '../interfaces/skeleton-process.interface'; import { ISkeletonProcess } from '../interfaces/skeleton-process.interface';
export abstract class UpdateProcess<T, R> implements ISkeletonProcess<T, R> { export abstract class UpdateProcess implements ISkeletonProcess {
abstract initialization(): T; abstract initialization();
abstract before(): T; abstract before();
abstract begin(): T; abstract begin();
abstract process(): T; abstract process();
abstract end(): T; abstract end();
abstract after(): T; abstract after();
abstract result(): R; abstract result();
} }

View File

@ -1,91 +1,67 @@
import { Delete, Get, Param, Patch, Post, Put } from '@nestjs/common'; import { Delete, Get, Param, Patch, Post, Put } from '@nestjs/common';
import { IPaginationEntity } from './entities/pagination.entity';
import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface'; import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface';
import { BatchDeleteProcess } from './processes/batch/delete.process';
import { BatchUpdateProcess } from './processes/batch/update.process';
import { CreateProcess } from './processes/create.process';
import { DeleteProcess } from './processes/delete.process';
import { ListProcess } from './processes/list.process';
import { PaginationProcess } from './processes/pagination.process';
import { PartialBatchUpdateProcess } from './processes/partial/batch/update.process';
import { ReadProcess } from './processes/read.process';
import { UpdateProcess } from './processes/update.process';
export class SkeletonCRUDController< export class SkeletonCRUDController implements ISkeletonCRUDController {
T,
E,
CP extends CreateProcess<T, T>,
RSP extends ReadProcess<T, T>,
RPP extends PaginationProcess<T, IPaginationEntity<T>>,
REP extends ListProcess<T, T[]>,
UPP extends UpdateProcess<T, T>,
UBP extends PartialBatchUpdateProcess<T, T[]>,
UEP extends UpdateProcess<T, T>,
UEBP extends BatchUpdateProcess<T, T[]>,
DP extends DeleteProcess<T, T>,
DBP extends BatchDeleteProcess<T, T[]>,
> implements ISkeletonCRUDController<T, E>
{
constructor( constructor(
private readonly createProcess: CP, private readonly createProcess,
private readonly readProcess: RSP, private readonly readProcess,
private readonly readPaginationProcess: RPP, private readonly readPaginationProcess,
private readonly readEntireProcess: REP, private readonly readEntireProcess,
private readonly updatePartialProcess: UPP, private readonly updatePartialProcess,
private readonly updateBatchProcess: UBP, private readonly updateBatchProcess,
private readonly updateEntireProcess: UEP, private readonly updateEntireProcess,
private readonly updateEntireBatchProcess: UEBP, private readonly updateEntireBatchProcess,
private readonly deleteProcess: DP, private readonly deleteProcess,
private readonly deleteBatchProcess: DBP, private readonly deleteBatchProcess,
) {} ) {}
@Post() @Post()
async create(): Promise<T> { async create() {
return this.createProcess.result(); return this.createProcess.result();
} }
@Get(':id') @Get(':id')
async readSelected(@Param('id') id: E): Promise<T> { async readSelected(@Param('id') id) {
return this.readProcess.result(); return this.readProcess.result();
} }
@Get() @Get()
async readPagination(): Promise<IPaginationEntity<T>> { async readPagination() {
return this.readPaginationProcess.result(); return this.readPaginationProcess.result();
} }
@Get('list') @Get('list')
async readEntire(): Promise<T[]> { async readEntire() {
return this.readEntireProcess.result(); return this.readEntireProcess.result();
} }
@Patch() @Patch()
async updatePartial(): Promise<T> { async updatePartial() {
return this.updatePartialProcess.result(); return this.updatePartialProcess.result();
} }
@Patch('batch') @Patch('batch')
async updatePartialBatch(): Promise<T[]> { async updatePartialBatch() {
return this.updateBatchProcess.result(); return this.updateBatchProcess.result();
} }
@Put() @Put()
async updateEntire(): Promise<T> { async updateEntire() {
return this.updateEntireProcess.result(); return this.updateEntireProcess.result();
} }
@Put('batch') @Put('batch')
async updateEntireBatch(): Promise<T[]> { async updateEntireBatch() {
return this.updateEntireBatchProcess.result(); return this.updateEntireBatchProcess.result();
} }
@Delete() @Delete()
async deleteSelected(): Promise<T> { async deleteSelected() {
return this.deleteProcess.result(); return this.deleteProcess.result();
} }
@Delete('batch') @Delete('batch')
async deleteBatch(): Promise<T[]> { async deleteBatch() {
return this.deleteBatchProcess.result(); return this.deleteBatchProcess.result();
} }
} }

View File

@ -1,12 +1,9 @@
import { SkeletonCRUDController } from '@autocrud/skeleton'; import { PrismaCRUDController } from '@autocrud/prisma/prisma-crud.controller';
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { AppService } from './app.service';
@Controller('hello') @Controller('hello')
export class AppController extends SkeletonCRUDController { export class AppController extends PrismaCRUDController {
constructor(private readonly appService: AppService) { constructor() {}
super();
}
// Read(): void { // Read(): void {
// console.log(this.appService.getHello()); // console.log(this.appService.getHello());

View File

@ -1,9 +1,10 @@
import { PrismaModule } from '@autocrud/prisma';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
@Module({ @Module({
imports: [], imports: [PrismaModule],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],
}) })