diff --git a/packages/datalib/src/PerspectiveDataProvider.ts b/packages/datalib/src/PerspectiveDataProvider.ts index e06b223e4..6ab36bd1a 100644 --- a/packages/datalib/src/PerspectiveDataProvider.ts +++ b/packages/datalib/src/PerspectiveDataProvider.ts @@ -17,9 +17,9 @@ export interface PerspectiveDatabaseConfig { export interface PerspectiveDataLoadProps { databaseConfig: PerspectiveDatabaseConfig; - schemaName: string; + schemaName?: string; pureName: string; - dataColumns: string[]; + dataColumns?: string[]; orderBy: { columnName: string; order: 'ASC' | 'DESC'; diff --git a/packages/datalib/src/processPerspectiveDefaultColunns.ts b/packages/datalib/src/processPerspectiveDefaultColunns.ts index 060c2890b..cd8e97479 100644 --- a/packages/datalib/src/processPerspectiveDefaultColunns.ts +++ b/packages/datalib/src/processPerspectiveDefaultColunns.ts @@ -1,6 +1,7 @@ import { findForeignKeyForColumn } from 'dbgate-tools'; import { DatabaseInfo, TableInfo, ViewInfo } from 'dbgate-types'; import { createPerspectiveNodeConfig, MultipleDatabaseInfo, PerspectiveConfig } from './PerspectiveConfig'; +import { PerspectiveDataPatternDict } from './PerspectiveDataPattern'; import { PerspectiveTableNode } from './PerspectiveTreeNode'; function getPerspectiveDefaultColumns( @@ -47,6 +48,7 @@ function getPerspectiveDefaultColumns( export function perspectiveNodesHaveStructure( config: PerspectiveConfig, dbInfos: MultipleDatabaseInfo, + dataPatterns: PerspectiveDataPatternDict, conid: string, database: string ) { @@ -56,8 +58,10 @@ export function perspectiveNodesHaveStructure( 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); + const collection = db.collections.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName); - if (!table && !view) return false; + if (!table && !view && !collection) return false; + if (collection && !dataPatterns?.[node.designerId]) return false; } return true; @@ -66,13 +70,14 @@ export function perspectiveNodesHaveStructure( export function shouldProcessPerspectiveDefaultColunns( config: PerspectiveConfig, dbInfos: MultipleDatabaseInfo, + dataPatterns: PerspectiveDataPatternDict, conid: string, database: string ) { const nodesNotProcessed = config.nodes.filter(x => !x.defaultColumnsProcessed); if (nodesNotProcessed.length == 0) return false; - return perspectiveNodesHaveStructure(config, dbInfos, conid, database); + return perspectiveNodesHaveStructure(config, dbInfos, dataPatterns, conid, database); } function processPerspectiveDefaultColunnsStep( diff --git a/packages/web/src/designer/Designer.svelte b/packages/web/src/designer/Designer.svelte index cc4dad1da..2dfabbda4 100644 --- a/packages/web/src/designer/Designer.svelte +++ b/packages/web/src/designer/Designer.svelte @@ -479,7 +479,7 @@ const rect = e.target.getBoundingClientRect(); var json = JSON.parse(data); const { objectTypeField } = json; - if (objectTypeField != 'tables' && objectTypeField != 'views') return; + if (objectTypeField != 'tables' && objectTypeField != 'views' && objectTypeField != 'collections') return; json.designerId = `${json.pureName}-${uuidv1()}`; json.left = e.clientX - rect.left; json.top = e.clientY - rect.top; @@ -941,6 +941,7 @@ .empty { margin: 50px; font-size: 20px; + position: absolute; } .canvas { position: relative; diff --git a/packages/web/src/designer/DesignerTable.svelte b/packages/web/src/designer/DesignerTable.svelte index ad8fa924e..ca3162057 100644 --- a/packages/web/src/designer/DesignerTable.svelte +++ b/packages/web/src/designer/DesignerTable.svelte @@ -213,6 +213,8 @@ !isMultipleTableSelection && [{ divider: true }, createDatabaseObjectMenu({ ...table, conid, database })], ]; } + + // $: console.log('COLUMNS', columns);
{ - const table = dbInfos?.[node.conid || conid]?.[node.database || database]?.tables?.find( + const db = dbInfos?.[node.conid || conid]?.[node.database || database]; + 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); + let collection: CollectionInfo & { columns?: any[] } = db?.collections?.find( x => x.pureName == node.pureName && x.schemaName == node.schemaName ); - const view = dbInfos?.[node.conid || conid]?.[node.database || database]?.views?.find( - x => x.pureName == node.pureName && x.schemaName == node.schemaName - ); - let collection: CollectionInfo & { columns?: any[] } = dbInfos?.[node.conid || conid]?.[ - node.database || database - ]?.collections?.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName); if (collection) { const pattern = dataPatterns?.[node.designerId]; if (!pattern) return null; collection = { ...collection, - columns: pattern.columns.map(x => ({ - columnName: x.name, - })), + columns: + pattern?.columns.map(x => ({ + columnName: x.name, + })) || [], }; } @@ -144,11 +142,11 @@ }); } - async function detectAutoArrange(config: PerspectiveConfig, dbInfos, root) { + async function detectAutoArrange(config: PerspectiveConfig, dbInfos, dataPatterns, root) { if ( root && config.nodes.find(x => !x.position) && - perspectiveNodesHaveStructure(config, dbInfos, conid, database) && + perspectiveNodesHaveStructure(config, dbInfos, dataPatterns, conid, database) && config.nodes.every(x => root?.findNodeByDesignerId(x.designerId)) ) { await tick(); @@ -156,7 +154,7 @@ } } - $: detectAutoArrange(config, dbInfos, root); + $: detectAutoArrange(config, dbInfos, dataPatterns, root); // $: console.log('DESIGNER ROOT', root); diff --git a/packages/web/src/perspectives/PerspectiveView.svelte b/packages/web/src/perspectives/PerspectiveView.svelte index 877c08ac7..2caa237ed 100644 --- a/packages/web/src/perspectives/PerspectiveView.svelte +++ b/packages/web/src/perspectives/PerspectiveView.svelte @@ -168,7 +168,7 @@ $: tempRoot = root?.findNodeByDesignerId(tempRootDesignerId); $: { - if (shouldProcessPerspectiveDefaultColunns(config, $dbInfos, conid, database)) { + if (shouldProcessPerspectiveDefaultColunns(config, $dbInfos, $dataPatterns, conid, database)) { setConfig(cfg => processPerspectiveDefaultColunns(cfg, $dbInfos, conid, database)); } } diff --git a/packages/web/src/utility/usePerspectiveDataPatterns.ts b/packages/web/src/utility/usePerspectiveDataPatterns.ts index fdabd9023..525a5dd67 100644 --- a/packages/web/src/utility/usePerspectiveDataPatterns.ts +++ b/packages/web/src/utility/usePerspectiveDataPatterns.ts @@ -11,6 +11,38 @@ import { import { PerspectiveDataLoader } from 'dbgate-datalib/lib/PerspectiveDataLoader'; import { writable, Readable } from 'svelte/store'; +export function getPerspectiveDataPatternsFromCache( + databaseConfig: PerspectiveDatabaseConfig, + config: PerspectiveConfig, + cache: PerspectiveCache, + dbInfos: MultipleDatabaseInfo +): PerspectiveDataPatternDict { + const res = {}; + + for (const node of config.nodes) { + const conid = node.conid || databaseConfig.conid; + const database = node.database || databaseConfig.database; + const { schemaName, pureName } = node; + + const cached = cache.dataPatterns.find( + x => x.conid == conid && x.database == database && x.schemaName == schemaName && x.pureName == pureName + ); + if (cached) { + res[node.designerId] = cached; + continue; + } + + const db = dbInfos?.[conid]?.[database]; + + if (!db) continue; + + const collection = db.collections?.find(x => x.pureName == pureName && x.schemaName == schemaName); + if (!collection) continue; + } + + return res; +} + export async function getPerspectiveDataPatterns( databaseConfig: PerspectiveDatabaseConfig, config: PerspectiveConfig, @@ -45,6 +77,10 @@ export async function getPerspectiveDataPatterns( engineType: 'docdb', pureName, orderBy: [], + range: { + offset: 0, + limit: 10, + }, }; const rows = await dataLoader.loadData(props); const pattern = analyseDataPattern( @@ -71,7 +107,9 @@ export function usePerspectiveDataPatterns( dbInfos: MultipleDatabaseInfo, dataLoader: PerspectiveDataLoader ): Readable { - const res = writable({}); - getPerspectiveDataPatterns(databaseConfig, config, cache, dbInfos, dataLoader).then(value => res.set(value)); + const cached = getPerspectiveDataPatternsFromCache(databaseConfig, config, cache, dbInfos); + const promise = getPerspectiveDataPatterns(databaseConfig, config, cache, dbInfos, dataLoader); + const res = writable(cached); + promise.then(value => res.set(value)); return res; }