fix: pagination not a number format by dto

This commit is contained in:
Supan Adit Pratama 2024-11-01 17:16:36 +07:00
parent aa6a655984
commit ee54146615
14 changed files with 99 additions and 23 deletions

View File

@ -1,11 +1,12 @@
import { PaginationProcess } from '@aditama-labs/nest-autocrud/skeleton'; import { PaginationProcess } from '@aditama-labs/nest-autocrud/skeleton';
import { PrismaProcess } from './prisma.process'; import { PrismaProcess } from './prisma.process';
import { IPaginationParam } from '@aditama-labs/nest-autocrud/skeleton/src/interfaces/pagination-param.interface';
export class PrismaPaginationProcess export class PrismaPaginationProcess
extends PrismaProcess extends PrismaProcess
implements PaginationProcess implements PaginationProcess
{ {
public params: { page: number; limit: number }; params: IPaginationParam;
async process(): Promise<any> { async process(): Promise<any> {
const { page, limit } = this.params; const { page, limit } = this.params;
@ -13,8 +14,7 @@ export class PrismaPaginationProcess
this.result = await this.getDelegate().findMany({ this.result = await this.getDelegate().findMany({
skip, skip,
// @TODO: I don't know why this actually string even the type is number, without parseInt it will throw error take: limit,
take: parseInt(limit.toString(), 10),
}); });
} }
} }

View File

@ -0,0 +1 @@
export * from './skeleton-controller.decorator';

View File

@ -0,0 +1,5 @@
export function SkeletonController(): ClassDecorator {
return (target: object) => {
console.log('STARTED', target);
};
}

View File

@ -0,0 +1 @@
export * from './pagination.dto';

View File

@ -0,0 +1,15 @@
import { Transform } from 'class-transformer';
import { IsNotEmpty, IsNumber, IsNumberString } from 'class-validator';
import { IPaginationParam } from '../interfaces/pagination-param.interface';
export class PaginationParamDTO implements IPaginationParam {
@IsNotEmpty()
@IsNumber()
@Transform((data) => parseInt(data.value))
page: number;
@IsNotEmpty()
@IsNumber()
@Transform((data) => parseInt(data.value))
limit: number;
}

View File

@ -1,11 +1,9 @@
import { DefaultExecutor } from '.'; import { DefaultExecutor } from '.';
import { PaginationParamDTO } from '../dto';
import { PaginationProcess } from '../processes'; import { PaginationProcess } from '../processes';
export class PaginationExecutor extends DefaultExecutor { export class PaginationExecutor extends DefaultExecutor {
constructor( constructor(process: PaginationProcess, params: PaginationParamDTO) {
process: PaginationProcess,
params: { page: number; limit: number },
) {
super(process); super(process);
// Set params to process // Set params to process
process.params = params; process.params = params;
@ -13,7 +11,7 @@ export class PaginationExecutor extends DefaultExecutor {
static async bootstrap( static async bootstrap(
process: PaginationProcess, process: PaginationProcess,
params: { page: number; limit: number }, params: PaginationParamDTO,
): Promise<any> { ): Promise<any> {
const executor = new PaginationExecutor(process, params); const executor = new PaginationExecutor(process, params);
await executor.execute(); await executor.execute();

View File

@ -1,6 +1,9 @@
export * from './constants'; export * from './constants';
export * from './processes'; export * from './decoratos';
export * from './interfaces'; export * from './dto';
export * from './executors';
export * from './skeleton-crud.controller';
export * from './entities'; export * from './entities';
export * from './executors';
export * from './interfaces';
export * from './processes';
export * from './skeleton-crud.controller';

View File

@ -1,8 +1,10 @@
import { IPaginationParam } from '../pagination-param.interface';
export interface ISkeletonCRUDController { export interface ISkeletonCRUDController {
create(body); create(body);
delete(id); delete(id);
list(); list();
pagination(params: { page?: number; limit?: number }); pagination(params: IPaginationParam);
read(id); read(id);
update(id, body); update(id, body);
} }

View File

@ -0,0 +1,4 @@
export interface IPaginationParam {
page: number;
limit: number;
}

View File

@ -1,5 +1,6 @@
import { IPaginationParam } from '../interfaces/pagination-param.interface';
import { DefaultProcess } from './default.process'; import { DefaultProcess } from './default.process';
export class PaginationProcess extends DefaultProcess { export class PaginationProcess extends DefaultProcess {
params: { page: number; limit: number }; params: IPaginationParam;
} }

View File

@ -1,5 +1,6 @@
import { import {
Body, Body,
ClassSerializerInterceptor,
Delete, Delete,
Get, Get,
Inject, Inject,
@ -7,6 +8,9 @@ import {
Patch, Patch,
Post, Post,
Query, Query,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common'; } from '@nestjs/common';
import { import {
CREATE_PROCESS, CREATE_PROCESS,
@ -16,14 +20,14 @@ import {
READ_PROCESS, READ_PROCESS,
UPDATE_PROCESS, UPDATE_PROCESS,
} from './constants'; } from './constants';
import { PaginationParamDTO } from './dto';
import { PaginationExecutor } from './executors';
import { CreateExcutor } from './executors/create.executor'; import { CreateExcutor } from './executors/create.executor';
import { DefaultExecutor } from './executors/default.executor';
import { DeleteExecutor } from './executors/delete.executor'; import { DeleteExecutor } from './executors/delete.executor';
import { ListExecutor } from './executors/list.executor'; import { ListExecutor } from './executors/list.executor';
import { ReadExecutor } from './executors/read.executor'; import { ReadExecutor } from './executors/read.executor';
import { UpdateExecutor } from './executors/update.executor'; import { UpdateExecutor } from './executors/update.executor';
import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface'; import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface';
import { PaginationExecutor } from './executors';
export class SkeletonCRUDController implements ISkeletonCRUDController { export class SkeletonCRUDController implements ISkeletonCRUDController {
constructor( constructor(
@ -57,9 +61,8 @@ export class SkeletonCRUDController implements ISkeletonCRUDController {
} }
@Get() @Get()
async pagination( @UsePipes(new ValidationPipe({ transform: true }))
@Query() params: { page: number; limit: number } = { page: 1, limit: 10 }, async pagination(@Query() params: PaginationParamDTO) {
) {
return await PaginationExecutor.bootstrap(this.paginationProcess, params); return await PaginationExecutor.bootstrap(this.paginationProcess, params);
} }

44
package-lock.json generated
View File

@ -1,18 +1,20 @@
{ {
"name": "@aditama-labs/nest-autocrud", "name": "@aditama-labs/nest-autocrud",
"version": "0.0.13", "version": "0.1.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@aditama-labs/nest-autocrud", "name": "@aditama-labs/nest-autocrud",
"version": "0.0.13", "version": "0.1.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@prisma/client": "^5.21.1", "@prisma/client": "^5.21.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"esm": "^3.2.25", "esm": "^3.2.25",
"reflect-metadata": "^0.2.0", "reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1" "rxjs": "^7.8.1"
@ -2671,6 +2673,12 @@
"@types/superagent": "^8.1.0" "@types/superagent": "^8.1.0"
} }
}, },
"node_modules/@types/validator": {
"version": "13.12.2",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.12.2.tgz",
"integrity": "sha512-6SlHBzUW8Jhf3liqrGGXyTJSIFe4nqlJ5A5KaMZ2l/vbM3Wh3KSybots/wfWVzNLK4D1NZluDlSQIbIEPx6oyA==",
"license": "MIT"
},
"node_modules/@types/yargs": { "node_modules/@types/yargs": {
"version": "17.0.33", "version": "17.0.33",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz",
@ -4029,6 +4037,23 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/class-transformer": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==",
"license": "MIT"
},
"node_modules/class-validator": {
"version": "0.14.1",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.1.tgz",
"integrity": "sha512-2VEG9JICxIqTpoK1eMzZqaV+u/EiwEJkMGzTrZf6sU/fwsnOITVgYJ8yojSy6CaXtO9V0Cc6ZQZ8h8m4UBuLwQ==",
"license": "MIT",
"dependencies": {
"@types/validator": "^13.11.8",
"libphonenumber-js": "^1.10.53",
"validator": "^13.9.0"
}
},
"node_modules/cli-boxes": { "node_modules/cli-boxes": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/cli-boxes/-/cli-boxes-3.0.0.tgz", "resolved": "https://registry.npmjs.org/cli-boxes/-/cli-boxes-3.0.0.tgz",
@ -7648,6 +7673,12 @@
"node": ">= 0.8.0" "node": ">= 0.8.0"
} }
}, },
"node_modules/libphonenumber-js": {
"version": "1.11.12",
"resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.11.12.tgz",
"integrity": "sha512-QkJn9/D7zZ1ucvT++TQSvZuSA2xAWeUytU+DiEQwbPKLyrDpvbul2AFs1CGbRAPpSCCk47aRAb5DX5mmcayp4g==",
"license": "MIT"
},
"node_modules/lines-and-columns": { "node_modules/lines-and-columns": {
"version": "1.2.4", "version": "1.2.4",
"resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz",
@ -11136,6 +11167,15 @@
"node": ">=10.12.0" "node": ">=10.12.0"
} }
}, },
"node_modules/validator": {
"version": "13.12.0",
"resolved": "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz",
"integrity": "sha512-c1Q0mCiPlgdTVVVIJIrBuxNicYE+t/7oKeI9MWLj3fh/uq2Pxh/3eeWbVZ4OcGW1TUf53At0njHw5SMdA3tmMg==",
"license": "MIT",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/vary": { "node_modules/vary": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",

View File

@ -36,6 +36,8 @@
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@prisma/client": "^5.21.1", "@prisma/client": "^5.21.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"esm": "^3.2.25", "esm": "^3.2.25",
"reflect-metadata": "^0.2.0", "reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1" "rxjs": "^7.8.1"
@ -90,4 +92,4 @@
"^@aditama-labs/nest-autocrud/prisma(|/.*)$": "<rootDir>/libs/prisma/$1" "^@aditama-labs/nest-autocrud/prisma(|/.*)$": "<rootDir>/libs/prisma/$1"
} }
} }
} }

View File

@ -1,5 +1,6 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { SkeletonCRUDController } from 'libs'; import { SkeletonController, SkeletonCRUDController } from 'libs';
@Controller('example/simple') @Controller('example/simple')
@SkeletonController()
export class SimpleController extends SkeletonCRUDController {} export class SimpleController extends SkeletonCRUDController {}