diff --git a/packages/datalib/src/PerspectiveConfig.ts b/packages/datalib/src/PerspectiveConfig.ts index 9277c86da..cc1414f71 100644 --- a/packages/datalib/src/PerspectiveConfig.ts +++ b/packages/datalib/src/PerspectiveConfig.ts @@ -50,6 +50,7 @@ export interface PerspectiveNodeConfig { designerId: string; schemaName?: string; pureName: string; + defaultColumnsProcessed?: boolean; alias?: string; diff --git a/packages/datalib/src/PerspectiveTreeNode.ts b/packages/datalib/src/PerspectiveTreeNode.ts index 60c2d8f60..68e14aa5e 100644 --- a/packages/datalib/src/PerspectiveTreeNode.ts +++ b/packages/datalib/src/PerspectiveTreeNode.ts @@ -34,7 +34,7 @@ import stableStringify from 'json-stable-stringify'; import { getFilterType, parseFilter } from 'dbgate-filterparser'; import { FilterType } from 'dbgate-filterparser/lib/types'; import { Condition, Expression, Select } from 'dbgate-sqltree'; -import { getPerspectiveDefaultColumns } from './getPerspectiveDefaultColumns'; +// import { getPerspectiveDefaultColumns } from './getPerspectiveDefaultColumns'; import uuidv1 from 'uuid/v1'; export interface PerspectiveDataLoadPropsWithNode { diff --git a/packages/datalib/src/getPerspectiveDefaultColumns.ts b/packages/datalib/src/getPerspectiveDefaultColumns.ts deleted file mode 100644 index 113ecc286..000000000 --- a/packages/datalib/src/getPerspectiveDefaultColumns.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { findForeignKeyForColumn } from 'dbgate-tools'; -import { DatabaseInfo, TableInfo, ViewInfo } from 'dbgate-types'; - -export function getPerspectiveDefaultColumns( - table: TableInfo | ViewInfo, - db: DatabaseInfo, - circularColumns: string[] -): string[] { - const columns = table.columns.map(x => x.columnName); - const predicates = [ - x => x.toLowerCase() == 'name', - x => x.toLowerCase() == 'title', - x => x.toLowerCase().includes('name'), - x => x.toLowerCase().includes('title'), - x => x.toLowerCase().includes('subject'), - // x => x.toLowerCase().includes('text'), - // x => x.toLowerCase().includes('desc'), - x => - table.columns - .find(y => y.columnName == x) - ?.dataType?.toLowerCase() - ?.includes('char'), - x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1 && !circularColumns.includes(x), - x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1, - ]; - - for (const predicate of predicates) { - const col = columns.find(predicate); - if (col) return [col]; - } - - return [columns[0]]; -} diff --git a/packages/datalib/src/index.ts b/packages/datalib/src/index.ts index 589ad7929..dace2858f 100644 --- a/packages/datalib/src/index.ts +++ b/packages/datalib/src/index.ts @@ -18,3 +18,4 @@ export * from './PerspectiveDisplay'; export * from './PerspectiveDataProvider'; export * from './PerspectiveCache'; export * from './PerspectiveConfig'; +export * from './processPerspectiveDefaultColunns'; diff --git a/packages/datalib/src/processPerspectiveDefaultColunns.ts b/packages/datalib/src/processPerspectiveDefaultColunns.ts new file mode 100644 index 000000000..02388a130 --- /dev/null +++ b/packages/datalib/src/processPerspectiveDefaultColunns.ts @@ -0,0 +1,71 @@ +import { findForeignKeyForColumn } from 'dbgate-tools'; +import { DatabaseInfo, TableInfo, ViewInfo } from 'dbgate-types'; +import { MultipleDatabaseInfo, PerspectiveConfig } from './PerspectiveConfig'; + +function getPerspectiveDefaultColumns( + table: TableInfo | ViewInfo, + db: DatabaseInfo, + circularColumns: string[] +): string[] { + const columns = table.columns.map(x => x.columnName); + const predicates = [ + x => x.toLowerCase() == 'name', + x => x.toLowerCase() == 'title', + x => x.toLowerCase().includes('name'), + x => x.toLowerCase().includes('title'), + x => x.toLowerCase().includes('subject'), + // x => x.toLowerCase().includes('text'), + // x => x.toLowerCase().includes('desc'), + x => + table.columns + .find(y => y.columnName == x) + ?.dataType?.toLowerCase() + ?.includes('char'), + x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1 && !circularColumns.includes(x), + x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1, + ]; + + for (const predicate of predicates) { + const col = columns.find(predicate); + if (col) return [col]; + } + + return [columns[0]]; +} + +export function processPerspectiveDefaultColunns( + config: PerspectiveConfig, + dbInfos: MultipleDatabaseInfo, + conid: string, + database: string +) { + for (const node of config.nodes) { + if (node.defaultColumnsProcessed) continue; + + const db = dbInfos?.[conid]?.[database]; + if (!db) continue; + + const table = db.tables.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName); + const view = db.views.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName); + + if (table || view) { + const defaultColumns = getPerspectiveDefaultColumns(table || view, db, []); + const newConfig = { + ...config, + nodes: config.nodes.map(n => + n.designerId == node.designerId + ? { + ...n, + defaultColumnsProcessed: true, + checkedColumns: defaultColumns, + } + : n + ), + }; + + return newConfig; + } + } + + return null; +} diff --git a/packages/web/src/perspectives/PerspectiveView.svelte b/packages/web/src/perspectives/PerspectiveView.svelte index 2ea804269..89a469cf5 100644 --- a/packages/web/src/perspectives/PerspectiveView.svelte +++ b/packages/web/src/perspectives/PerspectiveView.svelte @@ -36,6 +36,7 @@ PerspectiveDataProvider, PerspectiveTableColumnNode, PerspectiveTableNode, + processPerspectiveDefaultColunns, } from 'dbgate-datalib'; import _ from 'lodash'; @@ -147,6 +148,13 @@ config.rootDesignerId ) : null; + + $: { + const newConfig = processPerspectiveDefaultColunns(config, $dbInfos, conid, database); + if (newConfig) { + setConfig(() => newConfig); + } + }