Skip to content

Commit c01232e

Browse files
iykekingshasura-bot
authored andcommitted
console: create a hook for each loadShema sql queries
PR-URL: hasura/graphql-engine-mono#3016 GitOrigin-RevId: 030eef3b8e71281c419f8f7ec6dcbd40a085be74
1 parent 3d2dfc6 commit c01232e

39 files changed

Lines changed: 1879 additions & 220 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
window.__env = {
3+
dataApiUrl: 'http://localhost:8001',
4+
apiHost: 'http://localhost',
5+
apiPort: '8001',
6+
nodeEnv: 'development'
7+
}
8+
</script>

‎console/src/components/Services/Data/DataActions.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ const setUntrackedRelations = () => (dispatch, getState) => {
145145
* @param {{schemas: string[], tables: import('@/metadata/types').QualifiedTable[]}} [configOptions={}]
146146
*/
147147
// todo: it's called 4 times on start
148+
//! This has been solved by the hooks in src/hooks.
149+
//! They should be used to phase this out
148150
const loadSchema = (configOptions = {}) => {
149151
return (dispatch, getState) => {
150152
const url = Endpoints.query;

‎console/src/components/Services/Data/mergeData.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ import { FixMe } from '../../../types';
88

99
// TODO — each service should export `mergeLoadSchemaData` — results should be "merged" to Table object
1010

11-
type PermKeys = Pick<
11+
export type PermKeys = Pick<
1212
TableEntry,
1313
| 'update_permissions'
1414
| 'select_permissions'
1515
| 'delete_permissions'
1616
| 'insert_permissions'
1717
>;
1818

19-
const permKeys: Array<keyof PermKeys> = [
19+
export const permKeys: Array<keyof PermKeys> = [
2020
'insert_permissions',
2121
'update_permissions',
2222
'select_permissions',
2323
'delete_permissions',
2424
];
2525

26-
const keyToPermission = {
26+
export const keyToPermission = {
2727
insert_permissions: 'insert',
2828
update_permissions: 'update',
2929
select_permissions: 'select',
@@ -46,7 +46,7 @@ type MSSqlTable = {
4646
table_type: 'TABLE' | 'VIEW';
4747
};
4848

49-
type MSSqlFk = {
49+
export type MSSqlFk = {
5050
table_name: string;
5151
table_schema: string;
5252
constraint_name: string;
@@ -57,7 +57,7 @@ type MSSqlFk = {
5757
column_mapping: Array<{ column: string; referenced_column: string }>;
5858
};
5959

60-
type MSSqlConstraint = {
60+
export type MSSqlConstraint = {
6161
table_name: string;
6262
table_schema: string;
6363
constraints: {
@@ -73,7 +73,7 @@ type MSSqlCheckConstraint = {
7373
check_definition: string;
7474
};
7575

76-
const trimDefaultValue = (defaultValue?: string) => {
76+
export const trimDefaultValue = (defaultValue?: string) => {
7777
if (!defaultValue) {
7878
return '';
7979
}
@@ -92,7 +92,7 @@ const trimDefaultValue = (defaultValue?: string) => {
9292
);
9393
};
9494

95-
const modifyViolationType = (fkType: string) => {
95+
export const modifyViolationType = (fkType: string) => {
9696
switch (fkType) {
9797
case 'NO_ACTION':
9898
return 'no action';

‎console/src/features/MetadataAPI/hooks/metadataSelectors.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { TableEntry } from '@/metadata/types';
1+
import {
2+
keyToPermission,
3+
permKeys,
4+
} from '@/components/Services/Data/mergeData';
5+
import type { Permission, ComputedField } from '@/dataSources/types';
6+
import type { QualifiedTable, TableEntry } from '@/metadata/types';
27
import { MetadataResponse } from '..';
38

49
export namespace MetadataSelector {
@@ -34,4 +39,61 @@ export namespace MetadataSelector {
3439
const sources = getDataSourceMetadata(currentDataSource)(m);
3540
return sources?.tables ?? [];
3641
};
42+
43+
export const getTable = (
44+
currentDataSource: string,
45+
table: QualifiedTable
46+
) => (m: MetadataResponse) => {
47+
const tables = getTables(currentDataSource)(m);
48+
return tables.find(
49+
t => t.table.name === table.name && t.table.schema === table.schema
50+
);
51+
};
52+
53+
export const getTablePermissions = (
54+
currentDataSource: string,
55+
table: QualifiedTable
56+
) => (m: MetadataResponse) => {
57+
const metadataTable = getTable(currentDataSource, table)(m);
58+
const rolePermMap = permKeys.reduce((rpm: Record<string, any>, key) => {
59+
if (metadataTable) {
60+
metadataTable[key]?.forEach(
61+
(perm: { role: string; permission: Record<string, any> }) => {
62+
rpm[perm.role] = {
63+
permissions: {
64+
...(rpm[perm.role] && rpm[perm.role].permissions),
65+
[keyToPermission[key]]: perm.permission,
66+
},
67+
};
68+
}
69+
);
70+
}
71+
return rpm;
72+
}, {});
73+
const permissions: Permission[] = Object.keys(rolePermMap).map(role => ({
74+
role_name: role,
75+
permissions: rolePermMap[role].permissions,
76+
table_name: table.name,
77+
table_schema: table.schema,
78+
}));
79+
return permissions;
80+
};
81+
82+
export const getTableComputedFields = (
83+
currentDataSource: string,
84+
table: QualifiedTable
85+
) => (m: MetadataResponse) => {
86+
const metadataTable = getTable(currentDataSource, table)(m);
87+
const computed_fields: ComputedField[] = (
88+
metadataTable?.computed_fields || []
89+
).map(field => ({
90+
comment: field.comment || '',
91+
computed_field_name: field.name,
92+
name: field.name,
93+
table_name: table.name,
94+
table_schema: table.schema,
95+
definition: field.definition as ComputedField['definition'],
96+
}));
97+
return computed_fields;
98+
};
3799
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { QualifiedTable } from '@/metadata/types';
2+
import { useAppSelector } from '@/store';
3+
import { MetadataSelector } from './metadataSelectors';
4+
import { useMetadata } from './useMetadata';
5+
6+
export const useMetadataTableComputedFields = (table: QualifiedTable) => {
7+
const source: string = useAppSelector(
8+
state => state.tables.currentDataSource
9+
);
10+
return useMetadata(MetadataSelector.getTableComputedFields(source, table));
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { QualifiedTable } from '@/metadata/types';
2+
import { useAppSelector } from '@/store';
3+
import { MetadataSelector } from './metadataSelectors';
4+
import { useMetadata } from './useMetadata';
5+
6+
export const useMetadataTablePermissions = (table: QualifiedTable) => {
7+
const source: string = useAppSelector(
8+
state => state.tables.currentDataSource
9+
);
10+
return useMetadata(MetadataSelector.getTablePermissions(source, table));
11+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export { useRoles } from './hooks/useRoles';
22
export { useMetadataTables } from './hooks/useMetadataTables';
33
export { useMetadataVersion } from './hooks/useMetadataVersion';
4+
export { useMetadataTableComputedFields } from './hooks/useMetadataTableComputedFields';
5+
export { useMetadataTablePermissions } from './hooks/useMetadataTablePermissions';
46
export * from './types';

‎console/src/features/PermissionsTable/hooks/usePermissions.tsx‎

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { dataSource, Operations } from '@/dataSources';
22
import { ComputedField, TableColumn } from '@/dataSources/types';
3-
import { useRoles } from '@/features/MetadataAPI';
4-
import { useAllFunctions, useSchemaList, useTables } from '@/hooks';
3+
import {
4+
useMetadataTableComputedFields,
5+
useMetadataTablePermissions,
6+
useRoles,
7+
} from '@/features/MetadataAPI';
8+
import { useAllFunctions, useSchemaList, useSingleTable } from '@/hooks';
59
import { QualifiedTable } from '@/metadata/types';
610

711
export type RolePermissions = {
@@ -91,42 +95,32 @@ interface RolePermission {
9195
};
9296
}
9397

94-
export const useRolePermissions = ({
95-
name: tableName,
96-
schema: schemaName,
97-
}: QualifiedTable) => {
98+
export const useRolePermissions = (table: QualifiedTable) => {
9899
const { data: schemaList } = useSchemaList();
99-
const { data: tables } = useTables(
100-
{ schemas: schemaList! },
101-
{ enabled: !!schemaList }
102-
);
103-
const currentTableSchema = tables?.find(
104-
({ table_schema, table_name }) =>
105-
table_schema === schemaName && table_name === tableName
106-
);
100+
const { data: currentTableSchema } = useSingleTable(table);
101+
const { data: permissions } = useMetadataTablePermissions(table);
102+
const { data: computedFields } = useMetadataTableComputedFields(table);
107103
const { data: allFunctions } = useAllFunctions(schemaList!, {
108104
enabled: !!schemaList,
109105
});
110106
const { data: roles } = useRoles();
111107

112-
if (!currentTableSchema || !allFunctions) {
108+
if (!permissions || !allFunctions) {
113109
return { supportedQueries: [], rolePermissions: [] };
114110
}
115111

116-
const currentRolePermissions = currentTableSchema.permissions.reduce(
117-
(acc, p) => {
118-
acc[p.role_name] = p.permissions;
119-
return acc;
120-
},
121-
{} as Record<string, any>
122-
);
112+
const currentRolePermissions = permissions.reduce((acc, p) => {
113+
acc[p.role_name] = p.permissions;
114+
return acc;
115+
}, {} as Record<string, any>);
123116

124-
const supportedQueries = dataSource.getTableSupportedQueries(
125-
currentTableSchema
126-
);
117+
let supportedQueries: Operations[] = [];
118+
if (currentTableSchema) {
119+
supportedQueries = dataSource.getTableSupportedQueries(currentTableSchema);
120+
}
127121

128122
const groupedComputedFields = dataSource.getGroupedTableComputedFields(
129-
currentTableSchema.computed_fields,
123+
computedFields ?? [],
130124
allFunctions
131125
);
132126

‎console/src/hooks/common.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { getRunSqlQuery } from '@/components/Common/utils/v1QueryUtils';
2+
import Endpoints from '@/Endpoints';
3+
import { useAppSelector } from '@/store';
4+
import type { UseQueryOptions } from 'react-query';
5+
import { useQuery } from 'react-query';
6+
import { Api } from './apiUtils';
7+
import { RunSQLResponse } from './types';
8+
9+
export type RunSQLQueryOptions<KEY extends readonly unknown[], DATA> = Omit<
10+
UseQueryOptions<DATA, Error, DATA, KEY>,
11+
'queryKey' | 'queryFn'
12+
>;
13+
14+
type UseRunSQLArg<K extends readonly unknown[], D> = {
15+
sql: () => string;
16+
queryKey: K;
17+
transformFn: (data: RunSQLResponse) => D;
18+
queryOptions?: RunSQLQueryOptions<K, D>;
19+
};
20+
21+
export function useRunSQL<K extends readonly unknown[], D>({
22+
sql,
23+
queryKey,
24+
transformFn,
25+
queryOptions,
26+
}: UseRunSQLArg<K, D>) {
27+
const source: string = useAppSelector(
28+
state => state.tables.currentDataSource
29+
);
30+
const headers = useAppSelector(state => state.tables.dataHeaders);
31+
32+
return useQuery({
33+
...queryOptions,
34+
queryKey,
35+
queryFn() {
36+
const body = getRunSqlQuery(sql(), source, false, true);
37+
return Api.post<RunSQLResponse, D>(
38+
{
39+
url: Endpoints.query,
40+
headers,
41+
body,
42+
},
43+
transformFn
44+
);
45+
},
46+
});
47+
}

‎console/src/hooks/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ export * from './useFunctions';
33
export * from './useTables';
44
export * from './useServerConfig';
55
export * from './useSchemaList';
6+
export * from './useFKRelationships';
7+
export * from './usePrimaryKeys';
8+
export * from './useCheckConstraints';
9+
export * from './useUniqueKeys';

0 commit comments

Comments
 (0)