From 99a6b1bb311dae1fbc31d2b2a5bcd57a15a7fc0b Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Wed, 13 May 2020 07:08:34 +0200 Subject: [PATCH] fixed show master-detail --- packages/web/src/utility/metadataLoaders.js | 20 +++++++++++++++++--- packages/web/src/utility/useFetch.js | 3 ++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/web/src/utility/metadataLoaders.js b/packages/web/src/utility/metadataLoaders.js index 25d2353a..25811138 100644 --- a/packages/web/src/utility/metadataLoaders.js +++ b/packages/web/src/utility/metadataLoaders.js @@ -1,5 +1,6 @@ import useFetch from './useFetch'; import axios from './axios'; +import _ from 'lodash'; import { cacheGet, cacheSet, getCachedPromise } from './cache'; import stableStringify from 'json-stable-stringify'; @@ -7,6 +8,18 @@ const databaseInfoLoader = ({ conid, database }) => ({ url: 'database-connections/structure', params: { conid, database }, reloadTrigger: `database-structure-changed-${conid}-${database}`, + transform: (db) => { + const allForeignKeys = _.flatten(db.tables.map((x) => x.foreignKeys)); + return { + ...db, + tables: db.tables.map((table) => ({ + ...table, + dependencies: allForeignKeys.filter( + (x) => x.refSchemaName == table.schemaName && x.refTableName == table.pureName + ), + })), + }; + }, }); // const tableInfoLoader = ({ conid, database, schemaName, pureName }) => ({ @@ -58,7 +71,7 @@ const connectionListLoader = () => ({ }); async function getCore(loader, args) { - const { url, params, reloadTrigger } = loader(args); + const { url, params, reloadTrigger, transform } = loader(args); const key = stableStringify({ url, ...params }); async function doLoad() { @@ -67,7 +80,7 @@ async function getCore(loader, args) { url, params, }); - return resp.data; + return (transform || ((x) => x))(resp.data); } const fromCache = cacheGet(key); @@ -79,7 +92,7 @@ async function getCore(loader, args) { } function useCore(loader, args) { - const { url, params, reloadTrigger } = loader(args); + const { url, params, reloadTrigger, transform } = loader(args); const cacheKey = stableStringify({ url, ...params }); const res = useFetch({ @@ -87,6 +100,7 @@ function useCore(loader, args) { params, reloadTrigger, cacheKey, + transform, }); return res; diff --git a/packages/web/src/utility/useFetch.js b/packages/web/src/utility/useFetch.js index fcf6fa85..afb84fff 100644 --- a/packages/web/src/utility/useFetch.js +++ b/packages/web/src/utility/useFetch.js @@ -13,6 +13,7 @@ export default function useFetch({ defaultValue = undefined, reloadTrigger = undefined, cacheKey = undefined, + transform = (x) => x, ...config }) { const [value, setValue] = React.useState([defaultValue, []]); @@ -34,7 +35,7 @@ export default function useFetch({ data, ...config, }); - return resp.data; + return transform(resp.data); } if (cacheKey) {