Skip to content

Commit

Permalink
🐛Fixed pagination for PostgreSQL connector
Browse files Browse the repository at this point in the history
  • Loading branch information
shepilov committed Jul 12, 2024
1 parent 8457816 commit 585da88
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,22 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
entities.push(entity);
});

const nextPageToken = options?.pagination?.page_token || "0";
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");
const nextPage = this.nextPage(options.pagination, entities.length);
logger.debug(
`services.database.orm.postgres.find - Query Result (items=${entities.length}): ${query}`,
);

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
remove<Entity>(entities: Entity[]): Promise<boolean[]> {
return Promise.all(entities.map(entity => this.removeOne(entity)));
Expand Down
2 changes: 1 addition & 1 deletion tdrive/backend/node/test/e2e/documents/documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("the Drive feature", () => {
currentUser = await UserApi.getInstance(platform);
});

afterAll(async () => {
afterEach(async () => {
await platform?.tearDown();
platform = null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {

Expand All @@ -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());
Expand Down

0 comments on commit 585da88

Please sign in to comment.