perspectives support views

This commit is contained in:
Jan Prochazka
2022-07-31 16:56:09 +02:00
parent c8d031e2c4
commit 2b4120435b
4 changed files with 75 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
import { ColumnInfo, DatabaseInfo, ForeignKeyInfo, RangeDefinition, TableInfo } from 'dbgate-types'; import { ColumnInfo, DatabaseInfo, ForeignKeyInfo, RangeDefinition, TableInfo, ViewInfo } from 'dbgate-types';
import { clearConfigCache } from 'prettier'; import { clearConfigCache } from 'prettier';
import { ChangePerspectiveConfigFunc, PerspectiveConfig, PerspectiveConfigColumns } from './PerspectiveConfig'; import { ChangePerspectiveConfigFunc, PerspectiveConfig, PerspectiveConfigColumns } from './PerspectiveConfig';
import _isEqual from 'lodash/isEqual'; import _isEqual from 'lodash/isEqual';
@@ -185,7 +185,7 @@ export abstract class PerspectiveTreeNode {
}; };
} }
getOrderBy(table: TableInfo): PerspectiveDataLoadProps['orderBy'] { getOrderBy(table: TableInfo | ViewInfo): PerspectiveDataLoadProps['orderBy'] {
const res = _compact( const res = _compact(
this.childNodes.map(node => { this.childNodes.map(node => {
const sort = this.config?.sort?.[node?.parentNode?.uniqueName]?.find(x => x.uniqueName == node.uniqueName); const sort = this.config?.sort?.[node?.parentNode?.uniqueName]?.find(x => x.uniqueName == node.uniqueName);
@@ -199,7 +199,7 @@ export abstract class PerspectiveTreeNode {
); );
return res.length > 0 return res.length > 0
? res ? res
: table?.primaryKey?.columns.map(x => ({ columnName: x.columnName, order: 'ASC' })) || [ : (table as TableInfo)?.primaryKey?.columns.map(x => ({ columnName: x.columnName, order: 'ASC' })) || [
{ columnName: table?.columns[0].columnName, order: 'ASC' }, { columnName: table?.columns[0].columnName, order: 'ASC' },
]; ];
} }
@@ -210,7 +210,7 @@ export class PerspectiveTableColumnNode extends PerspectiveTreeNode {
refTable: TableInfo; refTable: TableInfo;
constructor( constructor(
public column: ColumnInfo, public column: ColumnInfo,
public table: TableInfo, public table: TableInfo | ViewInfo,
public db: DatabaseInfo, public db: DatabaseInfo,
config: PerspectiveConfig, config: PerspectiveConfig,
setConfig: ChangePerspectiveConfigFunc, setConfig: ChangePerspectiveConfigFunc,
@@ -223,9 +223,9 @@ export class PerspectiveTableColumnNode extends PerspectiveTreeNode {
this.defaultChecked = defaultChecked; this.defaultChecked = defaultChecked;
this.foreignKey = this.foreignKey = (table as TableInfo)?.foreignKeys?.find(
table.foreignKeys && fk => fk.columns.length == 1 && fk.columns[0].columnName == column.columnName
table.foreignKeys.find(fk => fk.columns.length == 1 && fk.columns[0].columnName == column.columnName); );
this.refTable = db.tables.find( this.refTable = db.tables.find(
x => x.pureName == this.foreignKey?.refTableName && x.schemaName == this.foreignKey?.refSchemaName x => x.pureName == this.foreignKey?.refTableName && x.schemaName == this.foreignKey?.refSchemaName
@@ -382,6 +382,59 @@ export class PerspectiveTableNode extends PerspectiveTreeNode {
} }
} }
export class PerspectiveViewNode extends PerspectiveTreeNode {
constructor(
public view: ViewInfo,
public db: DatabaseInfo,
config: PerspectiveConfig,
setConfig: ChangePerspectiveConfigFunc,
public dataProvider: PerspectiveDataProvider,
databaseConfig: PerspectiveDatabaseConfig,
parentNode: PerspectiveTreeNode
) {
super(config, setConfig, parentNode, dataProvider, databaseConfig);
}
getNodeLoadProps(parentRows: any[]): PerspectiveDataLoadProps {
return {
schemaName: this.view.schemaName,
pureName: this.view.pureName,
dataColumns: this.getDataLoadColumns(),
databaseConfig: this.databaseConfig,
orderBy: this.getOrderBy(this.view),
condition: this.getChildrenCondition(),
};
}
get codeName() {
return this.view.schemaName ? `${this.view.schemaName}:${this.view.pureName}` : this.view.pureName;
}
get title() {
return this.view.pureName;
}
get isExpandable() {
return true;
}
get childNodes(): PerspectiveTreeNode[] {
return getTableChildPerspectiveNodes(
this.view,
this.db,
this.config,
this.setConfig,
this.dataProvider,
this.databaseConfig,
this
);
}
get icon() {
return 'img table';
}
}
export class PerspectiveTableReferenceNode extends PerspectiveTableNode { export class PerspectiveTableReferenceNode extends PerspectiveTableNode {
constructor( constructor(
public foreignKey: ForeignKeyInfo, public foreignKey: ForeignKeyInfo,
@@ -434,7 +487,7 @@ export class PerspectiveTableReferenceNode extends PerspectiveTableNode {
} }
export function getTableChildPerspectiveNodes( export function getTableChildPerspectiveNodes(
table: TableInfo, table: TableInfo | ViewInfo,
db: DatabaseInfo, db: DatabaseInfo,
config: PerspectiveConfig, config: PerspectiveConfig,
setConfig: ChangePerspectiveConfigFunc, setConfig: ChangePerspectiveConfigFunc,
@@ -463,8 +516,8 @@ export function getTableChildPerspectiveNodes(
) )
) )
); );
if (db && table.dependencies) { if (db && (table as TableInfo)?.dependencies) {
for (const fk of table.dependencies) { for (const fk of (table as TableInfo)?.dependencies) {
const tbl = db.tables.find(x => x.pureName == fk.pureName && x.schemaName == fk.schemaName); const tbl = db.tables.find(x => x.pureName == fk.pureName && x.schemaName == fk.schemaName);
if (tbl) if (tbl)
res.push( res.push(

View File

@@ -1,7 +1,7 @@
import { findForeignKeyForColumn } from 'dbgate-tools'; import { findForeignKeyForColumn } from 'dbgate-tools';
import { DatabaseInfo, TableInfo } from 'dbgate-types'; import { DatabaseInfo, TableInfo, ViewInfo } from 'dbgate-types';
export function getPerspectiveDefaultColumns(table: TableInfo, db: DatabaseInfo): string[] { export function getPerspectiveDefaultColumns(table: TableInfo | ViewInfo, db: DatabaseInfo): string[] {
const columns = table.columns.map(x => x.columnName); const columns = table.columns.map(x => x.columnName);
const predicates = [ const predicates = [
x => x.toLowerCase() == 'name', x => x.toLowerCase() == 'name',
@@ -9,7 +9,7 @@ export function getPerspectiveDefaultColumns(table: TableInfo, db: DatabaseInfo)
x => x.toLowerCase().includes('name'), x => x.toLowerCase().includes('name'),
x => x.toLowerCase().includes('title'), x => x.toLowerCase().includes('title'),
x => x.dataType?.toLowerCase()?.includes('char'), x => x.dataType?.toLowerCase()?.includes('char'),
x => findForeignKeyForColumn(table, x)?.columns?.length == 1, x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1,
]; ];
for (const predicate of predicates) { for (const predicate of predicates) {

View File

@@ -142,6 +142,12 @@
tab: 'TableStructureTab', tab: 'TableStructureTab',
icon: 'img view-structure', icon: 'img view-structure',
}, },
{
label: 'Open perspective',
tab: 'PerspectiveTab',
forceNewTab: true,
icon: 'img perspective',
},
{ {
label: 'Drop view', label: 'Drop view',
isDrop: true, isDrop: true,
@@ -193,12 +199,6 @@
dropViews: true, dropViews: true,
}, },
}, },
{
label: 'Create perspective',
tab: 'PerspectiveTab',
forceNewTab: true,
icon: 'img perspective',
},
], ],
matviews: [ matviews: [
{ {

View File

@@ -5,6 +5,7 @@
PerspectiveDataProvider, PerspectiveDataProvider,
PerspectiveTableColumnNode, PerspectiveTableColumnNode,
PerspectiveTableNode, PerspectiveTableNode,
PerspectiveViewNode,
} from 'dbgate-datalib'; } from 'dbgate-datalib';
import _ from 'lodash'; import _ from 'lodash';
@@ -60,6 +61,8 @@
$: loader = new PerspectiveDataLoader(apiCall); $: loader = new PerspectiveDataLoader(apiCall);
$: root = $tableInfo $: root = $tableInfo
? new PerspectiveTableNode($tableInfo, $dbInfo, config, setConfig, dataProvider, { conid, database }, null) ? new PerspectiveTableNode($tableInfo, $dbInfo, config, setConfig, dataProvider, { conid, database }, null)
: $viewInfo
? new PerspectiveViewNode($viewInfo, $dbInfo, config, setConfig, dataProvider, { conid, database }, null)
: null; : null;
// $: console.log('CONFIG', config); // $: console.log('CONFIG', config);