Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefines the total of cursor pagination. #342

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs-library/crud",
"version": "0.8.5",
"version": "0.8.6",
"description": "Automatically generate CRUD Rest API based on NestJS and TypeOrm",
"homepage": "https://github.com/woowabros/nestjs-library-crud",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions spec/pagination/pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Pagination', () => {
expect(nextResponseBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
total: totalCount - defaultLimit,
total: totalCount,
});

const lastOneOfFirstResponse = firstResponseBody.data.pop();
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Pagination', () => {
expect(nextResponseBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
total: totalCount - defaultLimit,
total: totalCount,
});
expect(nextResponseBody.metadata.nextCursor).not.toEqual(responseBody.metadata.nextCursor);

Expand Down
4 changes: 2 additions & 2 deletions spec/read-many/read-many.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ describe('ReadMany - Options', () => {
expect(secondNextResponse.metadata.nextCursor).toEqual(expect.any(String));
expect(secondNextResponse.metadata.nextCursor).not.toEqual(firstResponse.metadata.nextCursor);

expect(nextResponse.metadata.total).toEqual(firstResponse.metadata.total - nextResponse.metadata.limit);
expect(secondNextResponse.metadata.total).toEqual(nextResponse.metadata.total - secondNextResponse.metadata.limit);
expect(nextResponse.metadata.total).toEqual(firstResponse.metadata.total);
expect(secondNextResponse.metadata.total).toEqual(nextResponse.metadata.total);

expect(nextResponse.data).toHaveLength(defaultLimit);
expect(nextResponse.metadata.nextCursor).toBeDefined();
Expand Down
2 changes: 1 addition & 1 deletion spec/search/search-cursor-pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('Search Cursor Pagination', () => {
],
metadata: {
limit: 5,
total: 20,
total: 25,
nextCursor: expect.any(String),
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/abstract/abstract.pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PaginationResponse } from '../interface';

describe('AbstractPaginationRequest', () => {
class PaginationRequest extends AbstractPaginationRequest {
nextTotal(_dataLength?: number | undefined): number {
nextTotal(): number {
throw new Error('Method not implemented.');
}
metadata<T>(_take: number, _dataLength: number, _total: number, _nextCursor: string): PaginationResponse<T>['metadata'] {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/abstract/abstract.pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ export abstract class AbstractPaginationRequest {
return this._nextCursor;
}

abstract nextTotal(dataLength?: number): number;
abstract nextTotal(): number;
abstract metadata<T>(take: number, dataLength: number, total: number, nextCursor: string): PaginationResponse<T>['metadata'];
}
2 changes: 1 addition & 1 deletion src/lib/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class CrudService<T extends BaseEntity> {

if (crudReadManyRequest.pagination.isNext) {
const entities = await findEntities;
return { entities, total: crudReadManyRequest.pagination.nextTotal(entities.length) };
return { entities, total: crudReadManyRequest.pagination.nextTotal() };
}
const [entities, total] = await Promise.all([
findEntities,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dto/pagination-cursor.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CursorPaginationResponse, PaginationType } from '../interface';
export class PaginationCursorDto extends AbstractPaginationRequest {
type: PaginationType.CURSOR = PaginationType.CURSOR;

nextTotal(dataLength: number): number {
return this.total - dataLength;
nextTotal(): number {
return this.total;
}

metadata<T>(take: number, _dataLength: number, total: number, nextCursor: string): CursorPaginationResponse<T>['metadata'] {
Expand Down