From 585da88b0d1c662724207999d09a93bf91af910e Mon Sep 17 00:00:00 2001 From: Anton SHEPILOV Date: Fri, 12 Jul 2024 00:22:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BFixed=20pagination=20for=20PostgreS?= =?UTF-8?q?QL=20connector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postgres/postgres-query-builder.ts | 2 +- .../orm/connectors/postgres/postgres.ts | 13 +++++++---- .../node/test/e2e/documents/documents.spec.ts | 2 +- .../orm/connectors/postgres/postgres.test.ts | 23 +++++++++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts index bae3b0445..33ed35cf9 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts @@ -103,7 +103,7 @@ export class PostgresQueryBuilder { limit = Number.parseInt(options.pagination.limitStr); } if (options.pagination.page_token) { - offset = (Number.parseInt(options.pagination.page_token) - 1) * limit; + offset = Number.parseInt(options.pagination.page_token) * limit; } } return [query(whereClause, orderByClause, limit, offset), values]; diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts index 16aa26989..aa29d4dd1 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts @@ -230,10 +230,7 @@ export class PostgresConnector extends AbstractConnector(entityDefinition.type, entities, nextPage); } + nextPage(pagination: Pagination, entitiesLength: number) { + const nextPageToken = pagination?.page_token || "0"; + const limit = parseInt(pagination?.limitStr); + const nextToken = entitiesLength === limit && (parseInt(nextPageToken) + 1).toString(10); + const nextPage: Paginable = new Pagination(nextToken, pagination?.limitStr || "100"); + return nextPage; + } + // eslint-disable-next-line @typescript-eslint/no-unused-vars remove(entities: Entity[]): Promise { return Promise.all(entities.map(entity => this.removeOne(entity))); diff --git a/tdrive/backend/node/test/e2e/documents/documents.spec.ts b/tdrive/backend/node/test/e2e/documents/documents.spec.ts index 6a59213d1..56c106f5b 100644 --- a/tdrive/backend/node/test/e2e/documents/documents.spec.ts +++ b/tdrive/backend/node/test/e2e/documents/documents.spec.ts @@ -44,7 +44,7 @@ describe("the Drive feature", () => { currentUser = await UserApi.getInstance(platform); }); - afterAll(async () => { + afterEach(async () => { await platform?.tearDown(); platform = null; }); diff --git a/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres.test.ts b/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres.test.ts index 03496665f..c7117e93b 100644 --- a/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres.test.ts +++ b/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres.test.ts @@ -7,6 +7,7 @@ import { } from "../../../../../../../../../src/core/platform/services/database/services/orm/connectors/postgres/postgres"; import { getEntityDefinition } from '../../../../../../../../../src/core/platform/services/database/services/orm/utils'; import { TestDbEntity, normalizeWhitespace, newTestDbEntity } from "./utils"; +import { Pagination } from "../../../../../../../../../src/core/platform/framework/api/crud-service"; describe('The Postgres Connector module', () => { @@ -28,6 +29,28 @@ describe('The Postgres Connector module', () => { jest.clearAllMocks(); }); + test('nextPage successfully iterate starting from null value', () => { + //given + const pagination = new Pagination(null, "5"); + + //when + let nextPage = subj.nextPage(pagination, 5); + + //then + expect(nextPage.page_token).toEqual("1") + expect(nextPage.limitStr).toEqual("5"); + + nextPage = subj.nextPage(Pagination.fromPaginable(nextPage), 5); + //then + expect(nextPage.page_token).toEqual("2") + expect(nextPage.limitStr).toEqual("5"); + + nextPage = subj.nextPage(Pagination.fromPaginable(nextPage), 1); + //then + expect(nextPage.page_token).toEqual(false) + expect(nextPage.limitStr).toEqual("5"); + }) + test('createTable generates table structure queries', async () => { // given const definition = getEntityDefinition(new TestDbEntity());