✨ Added offset pagination to db connector (#648)
* added offset pagination to db connector * 📝Added more info about Paginable usage --------- Co-authored-by: Monta <monta@HP-ProBook-445-14-inch-G9-Notebook-PC-505aadfc.localdomain> Co-authored-by: Anton SHEPILOV <ashepilov@linagora.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
+3
-1
@@ -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) {}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+8
-1
@@ -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";
|
||||
@@ -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;
|
||||
|
||||
+5
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
? (
|
||||
|
||||
Reference in New Issue
Block a user