From a709ef4da8293ba7b8e7ee056f7dee8009ef36d6 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Thu, 31 Oct 2024 23:54:35 +0700 Subject: [PATCH] feat: full working basic crud and customizable process --- libs/prisma/src/index.ts | 8 +++---- scripts/unix/cleaner.sh | 2 ++ src/app.controller.spec.ts | 22 ------------------- src/app.controller.ts | 5 ----- src/app.module.ts | 16 +++++--------- src/app.service.ts | 8 ------- src/example/custom/custom.controller.ts | 5 +++++ src/example/custom/custom.module.ts | 19 ++++++++++++++++ .../custom/domain/custom.list.process.ts | 12 ++++++++++ .../custom/domain/custom.read.process.ts | 19 ++++++++++++++++ src/example/simple/simple.controller.ts | 5 +++++ src/example/simple/simple.module.ts | 15 +++++++++++++ src/processes/list.process.ts | 12 ---------- 13 files changed, 86 insertions(+), 62 deletions(-) create mode 100755 scripts/unix/cleaner.sh delete mode 100644 src/app.controller.spec.ts delete mode 100644 src/app.controller.ts delete mode 100644 src/app.service.ts create mode 100644 src/example/custom/custom.controller.ts create mode 100644 src/example/custom/custom.module.ts create mode 100644 src/example/custom/domain/custom.list.process.ts create mode 100644 src/example/custom/domain/custom.read.process.ts create mode 100644 src/example/simple/simple.controller.ts create mode 100644 src/example/simple/simple.module.ts delete mode 100644 src/processes/list.process.ts diff --git a/libs/prisma/src/index.ts b/libs/prisma/src/index.ts index e284e32..3d026c4 100644 --- a/libs/prisma/src/index.ts +++ b/libs/prisma/src/index.ts @@ -1,6 +1,6 @@ -export * from './prisma.module'; -export * from './prisma.service'; +export * from './config.module-definition'; export * from './constants'; export * from './interfaces'; -export * from './config.module-definition'; -export * from './processes/list.process'; +export * from './prisma.module'; +export * from './prisma.service'; +export * from './processes'; diff --git a/scripts/unix/cleaner.sh b/scripts/unix/cleaner.sh new file mode 100755 index 0000000..65ca9ef --- /dev/null +++ b/scripts/unix/cleaner.sh @@ -0,0 +1,2 @@ +rm -rf ./plugins +rm -rf ./dist \ No newline at end of file diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f389..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/app.controller.ts b/src/app.controller.ts deleted file mode 100644 index df32091..0000000 --- a/src/app.controller.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { SkeletonCRUDController } from '@aditama-labs/nest-autocrud/skeleton'; -import { Controller } from '@nestjs/common'; - -@Controller('example') -export class AppController extends SkeletonCRUDController {} diff --git a/src/app.module.ts b/src/app.module.ts index fd14c7b..42240fc 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,16 +1,10 @@ -import { PrismaModule } from '@aditama-labs/nest-autocrud/prisma'; import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; -import { PrismaClient } from '@prisma/client'; +import { SimpleModule } from './example/simple/simple.module'; +import { CustomModule } from './example/custom/custom.module'; @Module({ - imports: [ - PrismaModule.forRoot({ - delegate: (prisma: PrismaClient) => prisma.user, - }), - ], - controllers: [AppController], - providers: [AppService], + imports: [SimpleModule, CustomModule], + controllers: [], + providers: [], }) export class AppModule {} diff --git a/src/app.service.ts b/src/app.service.ts deleted file mode 100644 index 927d7cc..0000000 --- a/src/app.service.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class AppService { - getHello(): string { - return 'Hello World!'; - } -} diff --git a/src/example/custom/custom.controller.ts b/src/example/custom/custom.controller.ts new file mode 100644 index 0000000..1f3bdc1 --- /dev/null +++ b/src/example/custom/custom.controller.ts @@ -0,0 +1,5 @@ +import { Controller } from '@nestjs/common'; +import { SkeletonCRUDController } from 'libs'; + +@Controller('example/custom') +export class CustomController extends SkeletonCRUDController {} diff --git a/src/example/custom/custom.module.ts b/src/example/custom/custom.module.ts new file mode 100644 index 0000000..a9d59d4 --- /dev/null +++ b/src/example/custom/custom.module.ts @@ -0,0 +1,19 @@ +import { Module } from '@nestjs/common'; +import { PrismaClient } from '@prisma/client'; +import { PrismaModule } from 'libs'; +import { CustomController } from './custom.controller'; +import { CustomListProcess } from './domain/custom.list.process'; +import { CustomReadProcess } from './domain/custom.read.process'; + +@Module({ + imports: [ + PrismaModule.forRoot({ + delegate: (prisma: PrismaClient) => prisma.user, + processList: CustomListProcess, + processRead: CustomReadProcess, + }), + ], + controllers: [CustomController], + providers: [], +}) +export class CustomModule {} diff --git a/src/example/custom/domain/custom.list.process.ts b/src/example/custom/domain/custom.list.process.ts new file mode 100644 index 0000000..0ce4fff --- /dev/null +++ b/src/example/custom/domain/custom.list.process.ts @@ -0,0 +1,12 @@ +import { PrismaListProcess } from '@aditama-labs/nest-autocrud/prisma'; + +export class CustomListProcess extends PrismaListProcess { + 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/custom/domain/custom.read.process.ts b/src/example/custom/domain/custom.read.process.ts new file mode 100644 index 0000000..7d21243 --- /dev/null +++ b/src/example/custom/domain/custom.read.process.ts @@ -0,0 +1,19 @@ +import { PrismaReadProcess } from '@aditama-labs/nest-autocrud/prisma'; + +export class CustomReadProcess extends PrismaReadProcess { + customResult; + + async before(): Promise { + console.log('The ID requested in path parameter', this.id); + } + async after(): Promise { + this.customResult = { + ...super.output(), + custom_code: 'XXXX', + }; + } + + output() { + return this.customResult; + } +} diff --git a/src/example/simple/simple.controller.ts b/src/example/simple/simple.controller.ts new file mode 100644 index 0000000..1da6add --- /dev/null +++ b/src/example/simple/simple.controller.ts @@ -0,0 +1,5 @@ +import { Controller } from '@nestjs/common'; +import { SkeletonCRUDController } from 'libs'; + +@Controller('example/simple') +export class SimpleController extends SkeletonCRUDController {} diff --git a/src/example/simple/simple.module.ts b/src/example/simple/simple.module.ts new file mode 100644 index 0000000..ff74a72 --- /dev/null +++ b/src/example/simple/simple.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { PrismaClient } from '@prisma/client'; +import { PrismaModule } from 'libs'; +import { SimpleController } from './simple.controller'; + +@Module({ + imports: [ + PrismaModule.forRoot({ + delegate: (prisma: PrismaClient) => prisma.user, + }), + ], + controllers: [SimpleController], + providers: [], +}) +export class SimpleModule {} diff --git a/src/processes/list.process.ts b/src/processes/list.process.ts deleted file mode 100644 index 739eced..0000000 --- a/src/processes/list.process.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { PrismaListProcess } from '@aditama-labs/nest-autocrud/prisma'; - -export class AppListProcess extends PrismaListProcess { - async process(): Promise { - console.log('Hello World'); - super.process(); - } - - output() { - return []; - } -}