wip: test simple delegate

This commit is contained in:
Supan Adit Pratama 2024-10-27 00:15:22 +07:00
parent 10f1e644b2
commit 3755f49181
7 changed files with 24 additions and 129 deletions

View File

@ -0,0 +1 @@
export declare const PRISMA_DELEGATE = 'PRISMA_DELEGATE';

View File

@ -1,37 +1,13 @@
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 { READ_ENTIRE_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,
{ 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],
})

View File

@ -1,10 +1,13 @@
import { DefaultProcess } from '@autocrud/skeleton/processes/default.process';
import { Inject } from '@nestjs/common';
import { PRISMA_DELEGATE } from '../constants';
export class PrismaProcess extends DefaultProcess {
protected delegate;
setDelegate(delegate) {
this.delegate = delegate;
constructor(
@Inject(PRISMA_DELEGATE)
private delegate: any,
) {
super();
}
get getDelegate() {

View File

@ -1,11 +1 @@
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';

View File

@ -1,13 +1,3 @@
export interface ISkeletonCRUDController {
create();
readSelected(id);
readPagination();
readEntire();
updatePartial();
updatePartialBatch();
updateEntire();
updateEntireBatch();
deleteSelected();
deleteBatch();
}

View File

@ -1,90 +1,16 @@
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 { Get, Inject } from '@nestjs/common';
import { READ_ENTIRE_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,
public readonly readEntireProcess: any,
) {}
@Post()
async create(): Promise<any> {
return await DefaultExecutor.bootstrap(this.createProcess);
}
@Get(':id')
async readSelected(@Param('id') id) {
return await DefaultExecutor.bootstrap(this.readProcess);
}
@Get()
async readPagination() {
return await DefaultExecutor.bootstrap(this.readPaginationProcess);
}
@Get('list')
async readEntire() {
return await DefaultExecutor.bootstrap(this.readEntireProcess);
}
@Patch()
async updatePartial() {
return await DefaultExecutor.bootstrap(this.updatePartialProcess);
}
@Patch('batch')
async updatePartialBatch() {
return await DefaultExecutor.bootstrap(this.updateBatchProcess);
}
@Put()
async updateEntire() {
return await DefaultExecutor.bootstrap(this.updateEntireProcess);
}
@Put('batch')
async updateEntireBatch() {
return await DefaultExecutor.bootstrap(this.updateEntireBatchProcess);
}
@Delete()
async deleteSelected() {
return await DefaultExecutor.bootstrap(this.deleteProcess);
}
@Delete('batch')
async deleteBatch() {
return await DefaultExecutor.bootstrap(this.deleteBatchProcess);
}
}

View File

@ -1,11 +1,20 @@
import { PrismaModule } from '@autocrud/prisma';
import { PrismaModule, PrismaService } from '@autocrud/prisma';
import { PRISMA_DELEGATE } from '@autocrud/prisma/constants';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
const delegateFactory = {
provide: PRISMA_DELEGATE,
useFactory: (prisma: PrismaService) => {
return prisma.user;
},
inject: [PrismaService],
};
@Module({
imports: [PrismaModule],
controllers: [AppController],
providers: [AppService],
providers: [AppService, delegateFactory],
})
export class AppModule {}