From 76c8f8ef62107caa214104c6c9c54a494d2b39ea Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Tue, 20 Aug 2024 14:00:58 +0200 Subject: [PATCH] mongo sorts - moved to plugin --- packages/datalib/src/PerspectiveDataLoader.ts | 13 +++++++------ packages/types/engines.d.ts | 12 ++++++++++-- .../src/datagrid/CollectionDataGridCore.svelte | 16 ++++++++-------- .../dbgate-plugin-mongo/src/backend/driver.js | 8 ++++++-- .../src/frontend/convertToMongoCondition.js | 9 +++++++++ .../dbgate-plugin-mongo/src/frontend/driver.js | 10 +++++----- 6 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/datalib/src/PerspectiveDataLoader.ts b/packages/datalib/src/PerspectiveDataLoader.ts index ea94b452..538bc24d 100644 --- a/packages/datalib/src/PerspectiveDataLoader.ts +++ b/packages/datalib/src/PerspectiveDataLoader.ts @@ -238,15 +238,16 @@ export class PerspectiveDataLoader { const res: any = { pureName, condition: this.buildSqlCondition(props), + sort: useSort ? props.orderBy : undefined, skip: props.range?.offset, limit: props.range?.limit, }; - if (useSort && props.orderBy?.length > 0) { - res.sort = _zipObject( - props.orderBy.map(col => col.columnName), - props.orderBy.map(col => (col.order == 'DESC' ? -1 : 1)) - ); - } + // if (useSort && props.orderBy?.length > 0) { + // res.sort = _zipObject( + // props.orderBy.map(col => col.columnName), + // props.orderBy.map(col => (col.order == 'DESC' ? -1 : 1)) + // ); + // } return res; } diff --git a/packages/types/engines.d.ts b/packages/types/engines.d.ts index cf8c1d2b..ba595587 100644 --- a/packages/types/engines.d.ts +++ b/packages/types/engines.d.ts @@ -43,6 +43,7 @@ export interface ReadCollectionOptions { limit?: number; condition?: any; aggregate?: CollectionAggregateDefinition; + sort?: CollectionSortDefinition; } export interface NewObjectTemplate { @@ -85,6 +86,13 @@ export interface CollectionAggregateDefinition { }[]; } +export interface CollectionSortDefinitionItem { + columnName: string; + direction: 'ASC' | 'DESC'; +} + +export type CollectionSortDefinition = CollectionSortDefinitionItem[]; + export interface FilterBehaviourProvider { getFilterBehaviour(dataType: string, standardFilterBehaviours: { [id: string]: FilterBehaviour }): FilterBehaviour; } @@ -171,8 +179,8 @@ export interface EngineDriver extends FilterBehaviourProvider { getRedirectAuthUrl(connection, options): Promise<{ url: string; sid: string }>; getAuthTokenFromCode(connection, options): Promise; getAccessTokenFromAuth(connection, req): Promise; - getCollectionExportQueryScript(collection: string, condition: any, sort: any): string; - getCollectionExportQueryJson(collection: string, condition: any, sort: any): {}; + getCollectionExportQueryScript(collection: string, condition: any, sort?: CollectionSortDefinition): string; + getCollectionExportQueryJson(collection: string, condition: any, sort?: CollectionSortDefinition): {}; analyserClass?: any; dumperClass?: any; diff --git a/packages/web/src/datagrid/CollectionDataGridCore.svelte b/packages/web/src/datagrid/CollectionDataGridCore.svelte index 85ec10af..45f714ad 100644 --- a/packages/web/src/datagrid/CollectionDataGridCore.svelte +++ b/packages/web/src/datagrid/CollectionDataGridCore.svelte @@ -60,14 +60,14 @@ : undefined; } - function buildMongoSort(props) { + function buildSortForGrid(props) { const sort = props?.display?.config?.sort; if (sort?.length > 0) { - return _.zipObject( - sort.map(col => col.uniqueName), - sort.map(col => (col.order == 'DESC' ? -1 : 1)) - ); + return sort.map(col => ({ + columnName: col.uniqueName, + order: col.order, + })); } return null; @@ -84,7 +84,7 @@ limit, skip: offset, condition: buildConditionForGrid(props), - sort: buildMongoSort(props), + sort: buildSortForGrid(props), }, }); @@ -175,7 +175,7 @@ return display?.driver?.getCollectionExportQueryScript?.( pureName, buildConditionForGrid($$props), - buildMongoSort($$props) + buildSortForGrid($$props) ); // return `db.collection('${pureName}') // .find(${JSON.stringify(buildConditionForGrid($$props) || {})}) @@ -186,7 +186,7 @@ return display?.driver?.getCollectionExportQueryJson?.( pureName, buildConditionForGrid($$props), - buildMongoSort($$props) + buildSortForGrid($$props) ); // return { // collection: pureName, diff --git a/plugins/dbgate-plugin-mongo/src/backend/driver.js b/plugins/dbgate-plugin-mongo/src/backend/driver.js index baa963fe..82696951 100644 --- a/plugins/dbgate-plugin-mongo/src/backend/driver.js +++ b/plugins/dbgate-plugin-mongo/src/backend/driver.js @@ -7,7 +7,11 @@ const MongoClient = require('mongodb').MongoClient; const ObjectId = require('mongodb').ObjectId; const AbstractCursor = require('mongodb').AbstractCursor; const createBulkInsertStream = require('./createBulkInsertStream'); -const { convertToMongoCondition, convertToMongoAggregate } = require('../frontend/convertToMongoCondition'); +const { + convertToMongoCondition, + convertToMongoAggregate, + convertToMongoSort, +} = require('../frontend/convertToMongoCondition'); function transformMongoData(row) { return _.cloneDeepWith(row, (x) => { @@ -286,7 +290,7 @@ const driver = { } else { // console.log('options.condition', JSON.stringify(options.condition, undefined, 2)); let cursor = await collection.find(convertObjectId(mongoCondition) || {}); - if (options.sort) cursor = cursor.sort(options.sort); + if (options.sort) cursor = cursor.sort(convertToMongoSort(options.sort)); if (options.skip) cursor = cursor.skip(options.skip); if (options.limit) cursor = cursor.limit(options.limit); const rows = await cursor.toArray(); diff --git a/plugins/dbgate-plugin-mongo/src/frontend/convertToMongoCondition.js b/plugins/dbgate-plugin-mongo/src/frontend/convertToMongoCondition.js index 66fbe8e3..9c270e8a 100644 --- a/plugins/dbgate-plugin-mongo/src/frontend/convertToMongoCondition.js +++ b/plugins/dbgate-plugin-mongo/src/frontend/convertToMongoCondition.js @@ -171,7 +171,16 @@ function convertToMongoAggregate(collectionAggregate) { ]; } +function convertToMongoSort(sort) { + if (!sort) return null; + return _zipObject( + sort.map((col) => col.columnName), + sort.map((col) => (col.order == 'DESC' ? -1 : 1)) + ); +} + module.exports = { convertToMongoCondition, convertToMongoAggregate, + convertToMongoSort, }; diff --git a/plugins/dbgate-plugin-mongo/src/frontend/driver.js b/plugins/dbgate-plugin-mongo/src/frontend/driver.js index 4c8b9541..6b986950 100644 --- a/plugins/dbgate-plugin-mongo/src/frontend/driver.js +++ b/plugins/dbgate-plugin-mongo/src/frontend/driver.js @@ -1,5 +1,5 @@ const { driverBase } = global.DBGATE_PACKAGES['dbgate-tools']; -const { convertToMongoCondition } = require('./convertToMongoCondition'); +const { convertToMongoCondition, convertToMongoSort } = require('./convertToMongoCondition'); const Dumper = require('./Dumper'); const { mongoSplitterOptions } = require('dbgate-query-splitter/lib/options'); @@ -101,14 +101,14 @@ const driver = { getCollectionExportQueryScript(collection, condition, sort) { return `db.collection('${collection}') - .find(${JSON.stringify(convertToMongoCondition(condition || {}))}) - .sort(${JSON.stringify(sort || {})})`; + .find(${JSON.stringify(convertToMongoCondition(condition) || {})}) + .sort(${JSON.stringify(convertToMongoSort(sort) || {})})`; }, getCollectionExportQueryJson(collection, condition, sort) { return { collection, - condition: convertToMongoCondition(condition || {}), - sort: sort || {}, + condition: convertToMongoCondition(condition) || {}, + sort: convertToMongoSort(sort) || {}, }; }, };