perspectives - remove mongo hardcodes

This commit is contained in:
Jan Prochazka
2024-08-20 10:28:02 +02:00
parent 30e3bc6eeb
commit 80a4d3f238
10 changed files with 133 additions and 149 deletions

View File

@@ -105,7 +105,6 @@ export class PerspectiveCache {
'databaseConfig',
'orderBy',
'sqlCondition',
'mongoCondition',
])
);
let res = this.tables[tableKey];

View File

@@ -5,6 +5,7 @@ import _zipObject from 'lodash/zipObject';
import _mapValues from 'lodash/mapValues';
import _isArray from 'lodash/isArray';
import { safeJsonParse } from 'dbgate-tools';
import { CollectionAggregateDefinition } from 'dbgate-types';
function normalizeLoadedRow(row) {
return _mapValues(row, v => safeJsonParse(v) || v);
@@ -59,24 +60,6 @@ export class PerspectiveDataLoader {
: null;
}
buildMongoCondition(props: PerspectiveDataLoadProps): {} {
const { schemaName, pureName, bindingColumns, bindingValues, dataColumns, orderBy, mongoCondition } = props;
const conditions = [];
if (mongoCondition) {
conditions.push(mongoCondition);
}
if (bindingColumns?.length == 1) {
conditions.push({
[bindingColumns[0]]: { $in: bindingValues.map(x => x[0]) },
});
}
return conditions.length == 1 ? conditions[0] : conditions.length > 0 ? { $and: conditions } : null;
}
async loadGroupingSqlDb(props: PerspectiveDataLoadProps) {
const { schemaName, pureName, bindingColumns } = props;
@@ -135,18 +118,28 @@ export class PerspectiveDataLoader {
async loadGroupingDocDb(props: PerspectiveDataLoadProps) {
const { schemaName, pureName, bindingColumns } = props;
const aggregate = [
{ $match: this.buildMongoCondition(props) },
{
$group: {
_id: _zipObject(
bindingColumns,
bindingColumns.map(col => '$' + col)
),
count: { $sum: 1 },
const aggregate: CollectionAggregateDefinition = {
condition: this.buildSqlCondition(props),
groupByColumns: bindingColumns,
aggregateColumns: [
{
alias: 'acount',
aggregateFunction: 'count',
},
},
];
],
};
// const aggregate = [
// { $match: this.buildMongoCondition(props) },
// {
// $group: {
// _id: _zipObject(
// bindingColumns,
// bindingColumns.map(col => '$' + col)
// ),
// count: { $sum: 1 },
// },
// },
// ];
if (dbg?.enabled) {
dbg(`LOAD COUNTS, table=${props.pureName}, columns=${bindingColumns?.join(',')}`);
@@ -244,7 +237,7 @@ export class PerspectiveDataLoader {
const { pureName } = props;
const res: any = {
pureName,
condition: this.buildMongoCondition(props),
condition: this.buildSqlCondition(props),
skip: props.range?.offset,
limit: props.range?.limit,
};

View File

@@ -25,7 +25,6 @@ export interface PerspectiveDataLoadProps {
range?: RangeDefinition;
topCount?: number;
sqlCondition?: Condition;
mongoCondition?: any;
engineType: PerspectiveDatabaseEngineType;
}

View File

@@ -349,8 +349,23 @@ export abstract class PerspectiveTreeNode {
);
}
getMutliColumnCondition(source): Condition {
if (!this.nodeConfig?.multiColumnFilter) return null;
const base = this.getBaseTableFromThis() as TableInfo | ViewInfo | CollectionInfo;
if (!base) return null;
const isDocDb = isCollectionInfo(base);
if (isDocDb) {
return this.getMutliColumnNoSqlCondition();
} else {
return this.getMutliColumnSqlCondition(source);
}
}
getMutliColumnSqlCondition(source): Condition {
if (!this.nodeConfig?.multiColumnFilter) return null;
const base = this.getBaseTableFromThis() as TableInfo | ViewInfo;
if (!base) return null;
try {
@@ -383,32 +398,40 @@ export abstract class PerspectiveTreeNode {
return null;
}
getMutliColumnMongoCondition(): {} {
getMutliColumnNoSqlCondition(): Condition {
if (!this.nodeConfig?.multiColumnFilter) return null;
const pattern = this.dataProvider?.dataPatterns?.[this.designerId];
if (!pattern) return null;
const condition = parseFilter(this.nodeConfig?.multiColumnFilter, mongoFilterBehaviour);
if (!condition) return null;
const res = pattern.columns.map(col => {
return _cloneDeepWith(condition, expr => {
if (expr.__placeholder__) {
return {
[col.name]: expr.__placeholder__,
};
}
});
});
return {
$or: res,
const orCondition: CompoudCondition = {
conditionType: 'or',
conditions: [],
};
for (const column of pattern.columns || []) {
orCondition.conditions.push(
_cloneDeepWith(condition, (expr: Expression) => {
if (expr.exprType == 'placeholder') {
return {
exprType: 'column',
columnName: column.name,
};
}
})
);
}
if (orCondition.conditions.length > 0) {
return orCondition;
}
}
getChildrenSqlCondition(source = null): Condition {
const conditions = _compact([
...this.childNodes.map(x => x.parseFilterCondition(source)),
...this.buildParentFilterConditions(),
this.getMutliColumnSqlCondition(source),
this.getMutliColumnCondition(source),
]);
if (conditions.length == 0) {
return null;
@@ -422,20 +445,6 @@ export abstract class PerspectiveTreeNode {
};
}
getChildrenMongoCondition(source = null): {} {
const conditions = _compact([
...this.childNodes.map(x => x.parseFilterCondition(source)),
this.getMutliColumnMongoCondition(),
]);
if (conditions.length == 0) {
return null;
}
if (conditions.length == 1) {
return conditions[0];
}
return { $and: conditions };
}
getOrderBy(table: TableInfo | ViewInfo | CollectionInfo): PerspectiveDataLoadProps['orderBy'] {
const res = _compact(
this.childNodes.map(node => {
@@ -1158,17 +1167,16 @@ export class PerspectiveTableNode extends PerspectiveTreeNode {
}
getNodeLoadProps(parentRows: any[]): PerspectiveDataLoadProps {
const isMongo = isCollectionInfo(this.table);
const isDocDb = isCollectionInfo(this.table);
return {
schemaName: this.table.schemaName,
pureName: this.table.pureName,
dataColumns: this.getDataLoadColumns(),
allColumns: isMongo,
allColumns: isDocDb,
databaseConfig: this.databaseConfig,
orderBy: this.getOrderBy(this.table),
sqlCondition: isMongo ? null : this.getChildrenSqlCondition(),
mongoCondition: isMongo ? this.getChildrenMongoCondition() : null,
engineType: isMongo ? 'docdb' : 'sqldb',
sqlCondition: this.getChildrenSqlCondition(),
engineType: isDocDb ? 'docdb' : 'sqldb',
};
}
@@ -1372,7 +1380,7 @@ export class PerspectiveCustomJoinTreeNode extends PerspectiveTableNode {
// console.log('PARENT ROWS', parentRows);
// console.log('this.getDataLoadColumns()', this.getDataLoadColumns());
const isMongo = isCollectionInfo(this.table);
const isDocDb = isCollectionInfo(this.table);
// const bindingValues = [];
@@ -1432,12 +1440,12 @@ export class PerspectiveCustomJoinTreeNode extends PerspectiveTableNode {
bindingColumns: this.getParentMatchColumns(),
bindingValues: _uniqBy(bindingValues, x => JSON.stringify(x)),
dataColumns: this.getDataLoadColumns(),
allColumns: isMongo,
allColumns: isDocDb,
databaseConfig: this.databaseConfig,
orderBy: this.getOrderBy(this.table),
sqlCondition: isMongo ? null : this.getChildrenSqlCondition(),
mongoCondition: isMongo ? this.getChildrenMongoCondition() : null,
engineType: isMongo ? 'docdb' : 'sqldb',
sqlCondition: this.getChildrenSqlCondition(),
// mongoCondition: isMongo ? this.getChildrenMongoCondition() : null,
engineType: isDocDb ? 'docdb' : 'sqldb',
};
}