From b3b80ede8407fdc103ffd2e8df316eed69eaf76a Mon Sep 17 00:00:00 2001 From: Montassar Ghanmy Date: Thu, 10 Oct 2024 11:18:22 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20offset=20pagination=20to=20?= =?UTF-8?q?db=20connector=20(#648)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added offset pagination to db connector * 📝Added more info about Paginable usage --------- Co-authored-by: Monta Co-authored-by: Anton SHEPILOV --- .../src/core/platform/framework/api/crud-service.ts | 11 +++++++++++ .../services/orm/connectors/abstract-connector.ts | 4 +++- .../database/services/orm/connectors/index.ts | 9 ++++++++- .../services/orm/connectors/mongodb/mongodb.ts | 5 +++++ .../services/orm/connectors/postgres/postgres.ts | 6 ++++++ .../node/src/services/documents/services/index.ts | 10 ++-------- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts b/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts index de716d129..d9b7bad15 100644 --- a/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts +++ b/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts @@ -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; diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/abstract-connector.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/abstract-connector.ts index 51d703be5..a8ba3555b 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/abstract-connector.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/abstract-connector.ts @@ -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 implements Connector { constructor(protected type: DatabaseType, protected options: T, protected secret: string) {} @@ -38,6 +38,8 @@ export abstract class AbstractConnector implements options: FindOptions, ): Promise>; + abstract getOffsetPagination(options: Paginable): Pagination; + getOptions(): T { return this.options; } diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/index.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/index.ts index 3ef487e2e..f1c6792e5 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/index.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/index.ts @@ -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>; + + /** + * 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; diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/mongodb/mongodb.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/mongodb/mongodb.ts index 598f419c5..7958110ac 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/mongodb/mongodb.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/mongodb/mongodb.ts @@ -316,4 +316,9 @@ export class MongoConnector extends AbstractConnector { const nextPage: Paginable = new Pagination(nextToken, options.pagination.limitStr || "100"); return new ListResult(entityDefinition.type, entities, nextPage); } + + getOffsetPagination(options: Paginable): Pagination { + const { page_token, limitStr } = options; + return new Pagination(`${page_token}`, `${limitStr}`, options.reversed); + } } diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts index aa29d4dd1..696e88998 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts @@ -358,6 +358,12 @@ export class PostgresConnector extends AbstractConnector