mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
console: migrate graphql, sql, main utils to TS (#4522)
This commit is contained in:
parent
79a3b6131d
commit
08aecff498
@ -1,14 +1,19 @@
|
|||||||
|
import {
|
||||||
|
buildClientSchema,
|
||||||
|
getIntrospectionQuery,
|
||||||
|
GraphQLSchema,
|
||||||
|
} from 'graphql';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { getIntrospectionQuery, buildClientSchema } from 'graphql';
|
|
||||||
import endpoints from '../../../Endpoints';
|
import endpoints from '../../../Endpoints';
|
||||||
|
|
||||||
export const useIntrospectionSchema = (headers = {}) => {
|
export const useIntrospectionSchema = (headers = {}) => {
|
||||||
const [schema, setSchema] = React.useState(null);
|
const [schema, setSchema] = React.useState<GraphQLSchema | null>(null);
|
||||||
const [loading, setLoading] = React.useState(true);
|
const [loading, setLoading] = React.useState(true);
|
||||||
const [error, setError] = React.useState(null);
|
const [error, setError] = React.useState(null);
|
||||||
|
|
||||||
const introspect = () => {
|
const introspect = () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
fetch(endpoints.graphQLUrl, {
|
fetch(endpoints.graphQLUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers,
|
headers,
|
||||||
@ -28,6 +33,7 @@ export const useIntrospectionSchema = (headers = {}) => {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
setError(e);
|
setError(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => setSchema(null);
|
return () => setSchema(null);
|
||||||
};
|
};
|
||||||
|
|
@ -1,76 +0,0 @@
|
|||||||
export const sqlEscapeText = text => {
|
|
||||||
let _text = text;
|
|
||||||
|
|
||||||
if (_text) {
|
|
||||||
_text = _text.replace(/'/g, "\\'");
|
|
||||||
}
|
|
||||||
|
|
||||||
return `E'${_text}'`;
|
|
||||||
};
|
|
||||||
|
|
||||||
// detect DDL statements in SQL
|
|
||||||
export const checkSchemaModification = _sql => {
|
|
||||||
let _isSchemaModification = false;
|
|
||||||
|
|
||||||
const sqlStatements = _sql
|
|
||||||
.toLowerCase()
|
|
||||||
.split(';')
|
|
||||||
.map(s => s.trim());
|
|
||||||
|
|
||||||
sqlStatements.forEach(statement => {
|
|
||||||
if (
|
|
||||||
statement.startsWith('create ') ||
|
|
||||||
statement.startsWith('alter ') ||
|
|
||||||
statement.startsWith('drop ')
|
|
||||||
) {
|
|
||||||
_isSchemaModification = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return _isSchemaModification;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getCheckConstraintBoolExp = check => {
|
|
||||||
if (check) {
|
|
||||||
return check.substring(7, check.length - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return check;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* queries */
|
|
||||||
|
|
||||||
export const getCreateCheckConstraintSql = (
|
|
||||||
tableName,
|
|
||||||
schemaName,
|
|
||||||
constraintName,
|
|
||||||
check
|
|
||||||
) => {
|
|
||||||
return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" check (${check})`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getDropConstraintSql = (tableName, schemaName, constraintName) => {
|
|
||||||
return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getCreatePkSql = ({
|
|
||||||
schemaName,
|
|
||||||
tableName,
|
|
||||||
selectedPkColumns,
|
|
||||||
constraintName,
|
|
||||||
}) => {
|
|
||||||
return `alter table "${schemaName}"."${tableName}"
|
|
||||||
add constraint "${constraintName}"
|
|
||||||
primary key ( ${selectedPkColumns.map(pkc => `"${pkc}"`).join(', ')} );`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getDropPkSql = ({ schemaName, tableName, constraintName }) => {
|
|
||||||
return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const terminateSql = sql => {
|
|
||||||
const sqlSanitised = sql.trim();
|
|
||||||
return sqlSanitised[sqlSanitised.length - 1] !== ';'
|
|
||||||
? sqlSanitised + ';'
|
|
||||||
: sqlSanitised;
|
|
||||||
};
|
|
98
console/src/components/Common/utils/sqlUtils.ts
Normal file
98
console/src/components/Common/utils/sqlUtils.ts
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
interface SqlUtilsOptions {
|
||||||
|
tableName: string;
|
||||||
|
schemaName: string;
|
||||||
|
constraintName: string;
|
||||||
|
check?: string;
|
||||||
|
selectedPkColumns?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const sqlEscapeText = (text: string) => {
|
||||||
|
let escapedText = text;
|
||||||
|
|
||||||
|
if (escapedText) {
|
||||||
|
escapedText = escapedText.replace(/'/g, "\\'");
|
||||||
|
}
|
||||||
|
|
||||||
|
return `E'${escapedText}'`;
|
||||||
|
};
|
||||||
|
|
||||||
|
// detect DDL statements in SQL
|
||||||
|
export const checkSchemaModification = (_sql: string) => {
|
||||||
|
let isSchemaModification = false;
|
||||||
|
|
||||||
|
const sqlStatements = _sql
|
||||||
|
.toLowerCase()
|
||||||
|
.split(';')
|
||||||
|
.map(s => s.trim());
|
||||||
|
|
||||||
|
sqlStatements.forEach((statement: string) => {
|
||||||
|
if (
|
||||||
|
statement.startsWith('create ') ||
|
||||||
|
statement.startsWith('alter ') ||
|
||||||
|
statement.startsWith('drop ')
|
||||||
|
) {
|
||||||
|
isSchemaModification = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return isSchemaModification;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getCheckConstraintBoolExp = (check: string) => {
|
||||||
|
if (check) {
|
||||||
|
return check.substring(7, check.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return check;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* queries */
|
||||||
|
|
||||||
|
export const getCreateCheckConstraintSql = (
|
||||||
|
tableName: string,
|
||||||
|
schemaName: string,
|
||||||
|
constraintName: string,
|
||||||
|
check: string
|
||||||
|
) => {
|
||||||
|
return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" check (${check})`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDropConstraintSql = (
|
||||||
|
tableName: string,
|
||||||
|
schemaName: string,
|
||||||
|
constraintName: string
|
||||||
|
) => {
|
||||||
|
return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getCreatePkSql = ({
|
||||||
|
schemaName,
|
||||||
|
tableName,
|
||||||
|
selectedPkColumns,
|
||||||
|
constraintName,
|
||||||
|
}: SqlUtilsOptions) => {
|
||||||
|
// if no primary key columns provided, return empty query
|
||||||
|
if (!selectedPkColumns || selectedPkColumns.length === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return `alter table "${schemaName}"."${tableName}"
|
||||||
|
add constraint "${constraintName}"
|
||||||
|
primary key ( ${selectedPkColumns.map(pkc => `"${pkc}"`).join(', ')} );`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDropPkSql = ({
|
||||||
|
schemaName,
|
||||||
|
tableName,
|
||||||
|
constraintName,
|
||||||
|
}: SqlUtilsOptions) => {
|
||||||
|
return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const terminateSql = (sql: string) => {
|
||||||
|
const sqlTerminated = sql.trim();
|
||||||
|
|
||||||
|
return sqlTerminated[sqlTerminated.length - 1] !== ';'
|
||||||
|
? `${sqlTerminated};`
|
||||||
|
: sqlTerminated;
|
||||||
|
};
|
@ -6,41 +6,47 @@ const defaultState = {
|
|||||||
const defaultProClickState = {
|
const defaultProClickState = {
|
||||||
isProClicked: false,
|
isProClicked: false,
|
||||||
};
|
};
|
||||||
const setLoveConsentState = stateData => {
|
|
||||||
|
const setLoveConsentState = (stateData: { isDismissed: boolean }) => {
|
||||||
window.localStorage.setItem(loveConsentState, JSON.stringify(stateData));
|
window.localStorage.setItem(loveConsentState, JSON.stringify(stateData));
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLoveConsentState = stateData => {
|
const getLoveConsentState = () => {
|
||||||
const s = window.localStorage.getItem(
|
const s = window.localStorage.getItem(loveConsentState);
|
||||||
loveConsentState,
|
|
||||||
JSON.stringify(stateData)
|
|
||||||
);
|
|
||||||
if (s) {
|
if (s) {
|
||||||
return JSON.parse(s);
|
return JSON.parse(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.localStorage.setItem(loveConsentState, JSON.stringify(defaultState));
|
window.localStorage.setItem(loveConsentState, JSON.stringify(defaultState));
|
||||||
|
|
||||||
return defaultState;
|
return defaultState;
|
||||||
};
|
};
|
||||||
|
|
||||||
const setProClickState = proStateData => {
|
const setProClickState = (proStateData: { isProClicked: boolean }) => {
|
||||||
window.localStorage.setItem(proClickState, JSON.stringify(proStateData));
|
window.localStorage.setItem(proClickState, JSON.stringify(proStateData));
|
||||||
};
|
};
|
||||||
|
|
||||||
const getProClickState = () => {
|
const getProClickState = () => {
|
||||||
try {
|
try {
|
||||||
const p = window.localStorage.getItem(proClickState);
|
const p = window.localStorage.getItem(proClickState);
|
||||||
|
|
||||||
if (p) {
|
if (p) {
|
||||||
return JSON.parse(p);
|
return JSON.parse(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
proClickState,
|
proClickState,
|
||||||
JSON.stringify(defaultProClickState)
|
JSON.stringify(defaultProClickState)
|
||||||
);
|
);
|
||||||
|
|
||||||
return defaultProClickState;
|
return defaultProClickState;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return defaultProClickState;
|
return defaultProClickState;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getLoveConsentState,
|
getLoveConsentState,
|
||||||
setLoveConsentState,
|
setLoveConsentState,
|
Loading…
Reference in New Issue
Block a user