metadata loaders refactor

This commit is contained in:
Jan Prochazka 2020-04-12 19:50:29 +02:00
parent 99a9b003bc
commit 648e3a921e
15 changed files with 116 additions and 89 deletions

View File

@ -2,7 +2,7 @@ import React from 'react';
import { TableIcon } from '../icons';
import { DropDownMenuItem } from '../modals/DropDownMenu';
import { openNewTab } from '../utility/common';
import getConnectionInfo from '../utility/getConnectionInfo';
import { getConnectionInfo } from '../utility/metadataLoaders';
import fullDisplayName from '../utility/fullDisplayName';
import { filterName } from '@dbgate/datalib';

View File

@ -2,7 +2,7 @@ import React from 'react';
import { ViewIcon } from '../icons';
import { DropDownMenuItem } from '../modals/DropDownMenu';
import { openNewTab } from '../utility/common';
import getConnectionInfo from '../utility/getConnectionInfo';
import { getConnectionInfo } from '../utility/metadataLoaders';
import fullDisplayName from '../utility/fullDisplayName';
import { filterName } from '@dbgate/datalib';

View File

@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import _ from 'lodash';
import axios from '../utility/axios';
import useConnectionInfo from '../utility/useConnectionInfo';
import { useConnectionInfo } from '../utility/metadataLoaders';
import SqlEditor from '../sqleditor/SqlEditor';
import { useUpdateDatabaseForTab } from '../utility/globalState';
import QueryToolbar from '../query/QueryToolbar';

View File

@ -1,7 +1,6 @@
import React from 'react';
import engines from '@dbgate/engines';
import useTableInfo from '../utility/useTableInfo';
import useConnectionInfo from '../utility/useConnectionInfo';
import { useTableInfo, useConnectionInfo } from '../utility/metadataLoaders';
import SqlEditor from '../sqleditor/SqlEditor';
export default function TableCreateScriptTab({ conid, database, schemaName, pureName }) {

View File

@ -4,10 +4,8 @@ import styled from 'styled-components';
import theme from '../theme';
import DataGrid from '../datagrid/DataGrid';
import { TableGridDisplay, createGridConfig, createGridCache, createChangeSet } from '@dbgate/datalib';
import useTableInfo from '../utility/useTableInfo';
import useConnectionInfo from '../utility/useConnectionInfo';
import { useTableInfo, useConnectionInfo, getTableInfo } from '../utility/metadataLoaders';
import engines from '@dbgate/engines';
import getTableInfo from '../utility/getTableInfo';
import useUndoReducer from '../utility/useUndoReducer';
import usePropsCompare from '../utility/usePropsCompare';
import { useUpdateDatabaseForTab } from '../utility/globalState';
@ -26,7 +24,7 @@ export default function TableDataTab({ conid, database, schemaName, pureName, ta
const display = React.useMemo(
() =>
tableInfo && connection
? new TableGridDisplay(tableInfo, engines(connection), config, setConfig, cache, setCache, name =>
? new TableGridDisplay(tableInfo, engines(connection), config, setConfig, cache, setCache, (name) =>
getTableInfo({ conid, database, ...name })
)
: null,

View File

@ -1,13 +1,11 @@
import React from 'react';
import styled from 'styled-components';
import _ from 'lodash';
import theme from '../theme';
import useFetch from '../utility/useFetch';
import ObjectListControl from '../utility/ObjectListControl';
import { TableColumn } from '../utility/TableControl';
import columnAppObject from '../appobj/columnAppObject';
import constraintAppObject from '../appobj/constraintAppObject';
import useTableInfo from '../utility/useTableInfo';
import { useTableInfo } from '../utility/metadataLoaders';
const WhitePage = styled.div`
position: absolute;
@ -33,7 +31,7 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
fieldName="notNull"
header="Not NULL"
sortable={true}
formatter={row => (row.notNull ? 'YES' : 'NO')}
formatter={(row) => (row.notNull ? 'YES' : 'NO')}
/>
<TableColumn fieldName="dataType" header="Data Type" sortable={true} />
<TableColumn fieldName="defaultValue" header="Default value" sortable={true} />
@ -41,14 +39,14 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
fieldName="isSparse"
header="Is Sparse"
sortable={true}
formatter={row => (row.isSparse ? 'YES' : 'NO')}
formatter={(row) => (row.isSparse ? 'YES' : 'NO')}
/>
<TableColumn fieldName="computedExpression" header="Computed Expression" sortable={true} />
<TableColumn
fieldName="isPersisted"
header="Is Persisted"
sortable={true}
formatter={row => (row.isPersisted ? 'YES' : 'NO')}
formatter={(row) => (row.isPersisted ? 'YES' : 'NO')}
/>
{/* {_.includes(dbCaps.columnListOptionalColumns, 'referencedTableNamesFormatted') && (
<TableColumn fieldName="referencedTableNamesFormatted" header="References" sortable={true} />
@ -80,7 +78,7 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
<TableColumn
fieldName="columns"
header="Columns"
formatter={row => row.columns.map(x => x.columnName).join(', ')}
formatter={(row) => row.columns.map((x) => x.columnName).join(', ')}
/>
</ObjectListControl>
@ -88,13 +86,13 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
<TableColumn
fieldName="baseColumns"
header="Base columns"
formatter={row => row.columns.map(x => x.columnName).join(', ')}
formatter={(row) => row.columns.map((x) => x.columnName).join(', ')}
/>
<TableColumn fieldName="refTable" header="Referenced table" formatter={row => row.refTableName} />
<TableColumn fieldName="refTable" header="Referenced table" formatter={(row) => row.refTableName} />
<TableColumn
fieldName="refColumns"
header="Referenced columns"
formatter={row => row.columns.map(x => x.refColumnName).join(', ')}
formatter={(row) => row.columns.map((x) => x.refColumnName).join(', ')}
/>
<TableColumn fieldName="updateAction" header="ON UPDATE" />
<TableColumn fieldName="deleteAction" header="ON DELETE" />
@ -104,13 +102,13 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
<TableColumn
fieldName="baseColumns"
header="Base columns"
formatter={row => row.columns.map(x => x.columnName).join(', ')}
formatter={(row) => row.columns.map((x) => x.columnName).join(', ')}
/>
<TableColumn fieldName="baseTable" header="Base table" formatter={row => row.pureName} />
<TableColumn fieldName="baseTable" header="Base table" formatter={(row) => row.pureName} />
<TableColumn
fieldName="refColumns"
header="Referenced columns"
formatter={row => row.columns.map(x => x.refColumnName).join(', ')}
formatter={(row) => row.columns.map((x) => x.refColumnName).join(', ')}
/>
<TableColumn fieldName="updateAction" header="ON UPDATE" />
<TableColumn fieldName="deleteAction" header="ON DELETE" />

View File

@ -1,7 +1,6 @@
import React from 'react';
import useConnectionInfo from '../utility/useConnectionInfo';
import { useConnectionInfo, useViewInfo } from '../utility/metadataLoaders';
import SqlEditor from '../sqleditor/SqlEditor';
import useViewInfo from '../utility/useViewInfo';
export default function ViewCreateScriptTab({ conid, database, schemaName, pureName }) {
const viewInfo = useViewInfo({ conid, database, schemaName, pureName });

View File

@ -4,14 +4,11 @@ import styled from 'styled-components';
import theme from '../theme';
import DataGrid from '../datagrid/DataGrid';
import { ViewGridDisplay, createGridConfig, createGridCache, createChangeSet } from '@dbgate/datalib';
import useTableInfo from '../utility/useTableInfo';
import useConnectionInfo from '../utility/useConnectionInfo';
import { useConnectionInfo, useViewInfo } from '../utility/metadataLoaders';
import engines from '@dbgate/engines';
import getTableInfo from '../utility/getTableInfo';
import useUndoReducer from '../utility/useUndoReducer';
import usePropsCompare from '../utility/usePropsCompare';
import { useUpdateDatabaseForTab } from '../utility/globalState';
import useViewInfo from '../utility/useViewInfo';
export default function ViewDataTab({ conid, database, schemaName, pureName, tabVisible, toolbarPortalRef }) {
const viewInfo = useViewInfo({ conid, database, schemaName, pureName });
@ -26,9 +23,8 @@ export default function ViewDataTab({ conid, database, schemaName, pureName, tab
const display = React.useMemo(
() =>
viewInfo && connection
? new ViewGridDisplay(viewInfo, engines(connection), config, setConfig, cache, setCache,
)
viewInfo && connection
? new ViewGridDisplay(viewInfo, engines(connection), config, setConfig, cache, setCache)
: null,
[viewInfo, connection, config, cache]
);

View File

@ -1,12 +0,0 @@
import axios from './axios';
export default async function getConnectionInfo(conid) {
const resp = await axios.request({
method: 'get',
params: { conid },
url: 'connections/get',
});
/** @type {import('@dbgate/types').StoredConnection} */
const res = resp.data;
return res;
}

View File

@ -1,12 +0,0 @@
import axios from './axios';
export default async function getTableInfo({ conid, database, schemaName, pureName }) {
const resp = await axios.request({
method: 'get',
url: 'metadata/table-info',
params: { conid, database, schemaName, pureName },
});
/** @type {import('@dbgate/types').TableInfo} */
const res = resp.data;
return res;
}

View File

@ -1,7 +1,7 @@
import _ from 'lodash';
import React from 'react';
import useStorage from './useStorage';
import useConnectionInfo from './useConnectionInfo';
import { useConnectionInfo } from './metadataLoaders';
import usePrevious from './usePrevious';
function createGlobalState(defaultValue) {
@ -50,7 +50,7 @@ const [CurrentDatabaseProvider, useCurrentDatabase, useSetCurrentDatabaseCore] =
function useSetCurrentDatabase() {
const setDb = useSetCurrentDatabaseCore();
const db = useCurrentDatabase();
return value => {
return (value) => {
if (_.get(db, 'name') !== _.get(value, 'name') || _.get(db, 'connection._id') != _.get(value, 'connection._id')) {
setDb(value);
}

View File

@ -0,0 +1,93 @@
import useFetch from './useFetch';
import axios from './axios';
// /** @returns {import('@dbgate/types').TableInfo} */
// function makeTableInfo(x) {
// return x;
// }
// const tableInfoLoader = ({ conid, database, schemaName, pureName }) => ({
// url: 'metadata/table-info',
// params: { conid, database, schemaName, pureName },
// reloadTrigger: `database-structure-changed-${conid}-${database}`,
// type: makeTableInfo,
// });
// function createGet(loader) {
// return async (args) => {
// const { url, params, reloadTrigger, type } = loader(args);
// const resp = await axios.request({
// method: 'get',
// url,
// params,
// });
// return type(resp.data);
// };
// }
// function createUse(loader) {
// return async (args) => {
// const { url, params, reloadTrigger, type } = loader(args);
// const res = useFetch({
// url,
// params,
// reloadTrigger,
// });
// return type(res);
// };
// }
// export const getTableInfo = createGet(tableInfoLoader);
// export const useTableInfo = createUse(tableInfoLoader);
export async function getTableInfo({ conid, database, schemaName, pureName }) {
const resp = await axios.request({
method: 'get',
url: 'metadata/table-info',
params: { conid, database, schemaName, pureName },
});
/** @type {import('@dbgate/types').TableInfo} */
const res = resp.data;
return res;
}
export function useTableInfo({ conid, database, schemaName, pureName }) {
/** @type {import('@dbgate/types').TableInfo} */
const tableInfo = useFetch({
url: 'metadata/table-info',
params: { conid, database, schemaName, pureName },
reloadTrigger: `database-structure-changed-${conid}-${database}`,
});
return tableInfo;
}
export function useViewInfo({ conid, database, schemaName, pureName }) {
/** @type {import('@dbgate/types').ViewInfo} */
const viewInfo = useFetch({
url: 'metadata/view-info',
params: { conid, database, schemaName, pureName },
reloadTrigger: `database-structure-changed-${conid}-${database}`,
});
return viewInfo;
}
export function useConnectionInfo(conid) {
/** @type {import('@dbgate/types').StoredConnection} */
const connection = useFetch({
params: { conid },
url: 'connections/get',
});
return connection;
}
export async function getConnectionInfo(conid) {
const resp = await axios.request({
method: 'get',
params: { conid },
url: 'connections/get',
});
/** @type {import('@dbgate/types').StoredConnection} */
const res = resp.data;
return res;
}

View File

@ -1,10 +0,0 @@
import useFetch from './useFetch';
export default function useConnectionInfo(conid) {
/** @type {import('@dbgate/types').StoredConnection} */
const connection = useFetch({
params: { conid },
url: 'connections/get',
});
return connection;
}

View File

@ -1,11 +0,0 @@
import useFetch from './useFetch';
export default function useTableInfo({ conid, database, schemaName, pureName }) {
/** @type {import('@dbgate/types').TableInfo} */
const tableInfo = useFetch({
url: 'metadata/table-info',
params: { conid, database, schemaName, pureName },
reloadTrigger: `database-structure-changed-${conid}-${database}`,
});
return tableInfo;
}

View File

@ -1,11 +0,0 @@
import useFetch from './useFetch';
export default function useViewInfo({ conid, database, schemaName, pureName }) {
/** @type {import('@dbgate/types').ViewInfo} */
const viewInfo = useFetch({
url: 'metadata/view-info',
params: { conid, database, schemaName, pureName },
reloadTrigger: `database-structure-changed-${conid}-${database}`,
});
return viewInfo;
}