mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 09:05:59 +00:00
perspectives - remove mongo hardcodes
This commit is contained in:
@@ -7,7 +7,7 @@ const MongoClient = require('mongodb').MongoClient;
|
||||
const ObjectId = require('mongodb').ObjectId;
|
||||
const AbstractCursor = require('mongodb').AbstractCursor;
|
||||
const createBulkInsertStream = require('./createBulkInsertStream');
|
||||
const { convertToMongoCondition } = require('../frontend/convertToMongoCondition');
|
||||
const { convertToMongoCondition, convertToMongoAggregate } = require('../frontend/convertToMongoCondition');
|
||||
|
||||
function transformMongoData(row) {
|
||||
return _.cloneDeepWith(row, (x) => {
|
||||
@@ -280,7 +280,7 @@ const driver = {
|
||||
const count = await collection.countDocuments(convertObjectId(mongoCondition) || {});
|
||||
return { count };
|
||||
} else if (options.aggregate) {
|
||||
let cursor = await collection.aggregate(convertObjectId(options.aggregate));
|
||||
let cursor = await collection.aggregate(convertObjectId(convertToMongoAggregate(options.aggregate)));
|
||||
const rows = await cursor.toArray();
|
||||
return { rows: rows.map(transformMongoData) };
|
||||
} else {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
const _zipObject = require('lodash/zipObject');
|
||||
|
||||
function convertLeftOperandToMongoColumn(left) {
|
||||
if (left.exprType == 'placeholder') return '__placeholder__';
|
||||
if (left.exprType == 'column') return left.columnName;
|
||||
@@ -121,11 +123,55 @@ function convertToMongoCondition(filter) {
|
||||
};
|
||||
}
|
||||
|
||||
case 'in':
|
||||
return {
|
||||
[convertLeftOperandToMongoColumn(filter.expr)]: {
|
||||
$in: filter.values,
|
||||
},
|
||||
};
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown condition type ${filter.conditionType}`);
|
||||
}
|
||||
}
|
||||
|
||||
function convertToMongoAggregateFunction(aggregate) {
|
||||
switch (aggregate.aggregateFunction) {
|
||||
case 'count':
|
||||
return { $sum: 1 };
|
||||
case 'sum':
|
||||
return { $sum: `$${aggregate.columnArgument}` };
|
||||
case 'avg':
|
||||
return { $avg: `$${aggregate.columnArgument}` };
|
||||
case 'min':
|
||||
return { $min: `$${aggregate.columnArgument}` };
|
||||
case 'max':
|
||||
return { $max: `$${aggregate.columnArgument}` };
|
||||
default:
|
||||
throw new Error(`Unknown aggregate function ${aggregate.aggregateFunction}`);
|
||||
}
|
||||
}
|
||||
|
||||
function convertToMongoAggregate(collectionAggregate) {
|
||||
return [
|
||||
{ $match: convertToMongoCondition(collectionAggregate.condition) },
|
||||
{
|
||||
$group: {
|
||||
_id: _zipObject(
|
||||
collectionAggregate.groupByColumns,
|
||||
collectionAggregate.groupByColumns.map((col) => '$' + col)
|
||||
),
|
||||
..._zipObject(
|
||||
collectionAggregate.aggregateColumns.map((col) => col.alias),
|
||||
collectionAggregate.aggregateColumns.map((col) => convertToMongoAggregateFunction(col))
|
||||
),
|
||||
count: { $sum: 1 },
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
convertToMongoCondition,
|
||||
convertToMongoAggregate,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user