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

✨ Added offset pagination to db connector #648

Merged
merged 2 commits into from
Oct 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ export class CrudException extends Error {
}

export interface Paginable {
/**
* In page token can be different type of data depending of the source.
* For ES it will be exactly page token, identifier of the next page.
* For PostgreSQL it will be number of the next page.
* For MongoDB it will be offset.
* This information is relevant for the Service/Repository level, in the controller
* we have only offset there for every type of source because in the "browse" controller
* we do not pass "next_page_token" to frontend and hence we are calculating
* offset on the frontend side and then passing offset to DocumentService.
*
*/
page_token?: string;
limitStr?: string;
reversed?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Connector, UpsertOptions } from ".";
import { ConnectionOptions, DatabaseType } from "../..";
import { FindOptions } from "../repository/repository";
import { ColumnDefinition, EntityDefinition } from "../types";
import { ListResult } from "../../../../../framework/api/crud-service";
import { ListResult, Paginable, Pagination } from "../../../../../framework/api/crud-service";

export abstract class AbstractConnector<T extends ConnectionOptions> implements Connector {
constructor(protected type: DatabaseType, protected options: T, protected secret: string) {}
Expand Down Expand Up @@ -38,6 +38,8 @@ export abstract class AbstractConnector<T extends ConnectionOptions> implements
options: FindOptions,
): Promise<ListResult<EntityType>>;

abstract getOffsetPagination(options: Paginable): Pagination;

getOptions(): T {
return this.options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DatabaseType } from "../..";
import { MongoConnectionOptions } from "./mongodb/mongodb";
import { ColumnDefinition, EntityDefinition } from "../types";
import { FindOptions } from "../repository/repository";
import { ListResult } from "../../../../../framework/api/crud-service";
import { ListResult, Paginable, Pagination } from "../../../../../framework/api/crud-service";
import { PostgresConnectionOptions } from "./postgres/postgres";

export * from "./mongodb/mongodb";
Expand Down Expand Up @@ -92,6 +92,13 @@ export interface Connector extends Initializable {
filters: any,
options: FindOptions,
): Promise<ListResult<EntityType>>;

/**
* Get the pagination for the given options where the pagination is offset based
* @param options Paginable
* @returns Pagination
*/
getOffsetPagination(options: Paginable): Pagination;
}

export declare type ConnectionOptions = MongoConnectionOptions | PostgresConnectionOptions;
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,9 @@ export class MongoConnector extends AbstractConnector<MongoConnectionOptions> {
const nextPage: Paginable = new Pagination(nextToken, options.pagination.limitStr || "100");
return new ListResult<Table>(entityDefinition.type, entities, nextPage);
}

getOffsetPagination(options: Paginable): Pagination {
const { page_token, limitStr } = options;
return new Pagination(`${page_token}`, `${limitStr}`, options.reversed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
throw err;
}
}

getOffsetPagination(options: Paginable): Pagination {
const { page_token, limitStr } = options;
const pageNumber = parseInt(page_token) / parseInt(limitStr);
return new Pagination(`${pageNumber}`, `${limitStr}`, options.reversed);
}
}

export type TableRowInfo = {
Expand Down
10 changes: 2 additions & 8 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,12 @@ export class DocumentsService {
if (options?.sort) {
sortField = this.getSortFieldMapping(options.sort);
}
const dbType = await globalResolver.database.getConnector().getType();

// Initialize pagination
let pagination;

if (options?.pagination) {
const { page_token, limitStr } = options.pagination;
const pageNumber =
dbType === "mongodb" ? parseInt(page_token) : parseInt(page_token) / parseInt(limitStr);

pagination = new Pagination(`${pageNumber}`, `${limitStr}`, false);
}
if (options?.pagination)
pagination = globalResolver.database.getConnector().getOffsetPagination(options.pagination);

let children = isDirectory
? (
Expand Down