mongo sorts - moved to plugin

This commit is contained in:
Jan Prochazka 2024-08-20 14:00:58 +02:00
parent 49e338bbbc
commit 76c8f8ef62
6 changed files with 45 additions and 23 deletions

View File

@ -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;
}

View File

@ -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<string>;
getAccessTokenFromAuth(connection, req): Promise<string | null>;
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;

View File

@ -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,

View File

@ -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();

View File

@ -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,
};

View File

@ -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) || {},
};
},
};