Skip to content

Commit

Permalink
✨ Added offset pagination to db connector (#648)
Browse files Browse the repository at this point in the history
* added offset pagination to db connector

* 📝Added more info about Paginable usage

---------

Co-authored-by: Monta <[email protected]>
Co-authored-by: Anton SHEPILOV <[email protected]>
  • Loading branch information
3 people authored Oct 10, 2024
1 parent a669e81 commit b3b80ed
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
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 @@ -222,18 +222,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

0 comments on commit b3b80ed

Please sign in to comment.