🐛Fixed pagination for PostgreSQL connector

This commit is contained in:
Anton SHEPILOV
2024-07-12 00:22:33 +02:00
committed by Anton Shepilov
parent 845781642f
commit 585da88b0d
4 changed files with 34 additions and 6 deletions
@@ -103,7 +103,7 @@ export class PostgresQueryBuilder {
limit = Number.parseInt(options.pagination.limitStr); limit = Number.parseInt(options.pagination.limitStr);
} }
if (options.pagination.page_token) { 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]; return [query(whereClause, orderByClause, limit, offset), values];
@@ -230,10 +230,7 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
entities.push(entity); entities.push(entity);
}); });
const nextPageToken = options?.pagination?.page_token || "0"; const nextPage = this.nextPage(options.pagination, entities.length);
const limit = parseInt(options?.pagination?.limitStr);
const nextToken = entities.length === limit && (parseInt(nextPageToken) + limit).toString(10);
const nextPage: Paginable = new Pagination(nextToken, options?.pagination?.limitStr || "100");
logger.debug( logger.debug(
`services.database.orm.postgres.find - Query Result (items=${entities.length}): ${query}`, `services.database.orm.postgres.find - Query Result (items=${entities.length}): ${query}`,
); );
@@ -241,6 +238,14 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
return new ListResult<EntityType>(entityDefinition.type, entities, nextPage); return new ListResult<EntityType>(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 // eslint-disable-next-line @typescript-eslint/no-unused-vars
remove<Entity>(entities: Entity[]): Promise<boolean[]> { remove<Entity>(entities: Entity[]): Promise<boolean[]> {
return Promise.all(entities.map(entity => this.removeOne(entity))); return Promise.all(entities.map(entity => this.removeOne(entity)));
@@ -44,7 +44,7 @@ describe("the Drive feature", () => {
currentUser = await UserApi.getInstance(platform); currentUser = await UserApi.getInstance(platform);
}); });
afterAll(async () => { afterEach(async () => {
await platform?.tearDown(); await platform?.tearDown();
platform = null; platform = null;
}); });
@@ -7,6 +7,7 @@ import {
} from "../../../../../../../../../src/core/platform/services/database/services/orm/connectors/postgres/postgres"; } from "../../../../../../../../../src/core/platform/services/database/services/orm/connectors/postgres/postgres";
import { getEntityDefinition } from '../../../../../../../../../src/core/platform/services/database/services/orm/utils'; import { getEntityDefinition } from '../../../../../../../../../src/core/platform/services/database/services/orm/utils';
import { TestDbEntity, normalizeWhitespace, newTestDbEntity } from "./utils"; import { TestDbEntity, normalizeWhitespace, newTestDbEntity } from "./utils";
import { Pagination } from "../../../../../../../../../src/core/platform/framework/api/crud-service";
describe('The Postgres Connector module', () => { describe('The Postgres Connector module', () => {
@@ -28,6 +29,28 @@ describe('The Postgres Connector module', () => {
jest.clearAllMocks(); 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 () => { test('createTable generates table structure queries', async () => {
// given // given
const definition = getEntityDefinition(new TestDbEntity()); const definition = getEntityDefinition(new TestDbEntity());