mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 09:22:43 +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 { getIntrospectionQuery, buildClientSchema } from 'graphql';
|
||||
import endpoints from '../../../Endpoints';
|
||||
|
||||
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 [error, setError] = React.useState(null);
|
||||
|
||||
const introspect = () => {
|
||||
setLoading(true);
|
||||
|
||||
fetch(endpoints.graphQLUrl, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
@ -28,6 +33,7 @@ export const useIntrospectionSchema = (headers = {}) => {
|
||||
setLoading(false);
|
||||
setError(e);
|
||||
});
|
||||
|
||||
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 = {
|
||||
isProClicked: false,
|
||||
};
|
||||
const setLoveConsentState = stateData => {
|
||||
|
||||
const setLoveConsentState = (stateData: { isDismissed: boolean }) => {
|
||||
window.localStorage.setItem(loveConsentState, JSON.stringify(stateData));
|
||||
};
|
||||
|
||||
const getLoveConsentState = stateData => {
|
||||
const s = window.localStorage.getItem(
|
||||
loveConsentState,
|
||||
JSON.stringify(stateData)
|
||||
);
|
||||
const getLoveConsentState = () => {
|
||||
const s = window.localStorage.getItem(loveConsentState);
|
||||
|
||||
if (s) {
|
||||
return JSON.parse(s);
|
||||
}
|
||||
|
||||
window.localStorage.setItem(loveConsentState, JSON.stringify(defaultState));
|
||||
|
||||
return defaultState;
|
||||
};
|
||||
|
||||
const setProClickState = proStateData => {
|
||||
const setProClickState = (proStateData: { isProClicked: boolean }) => {
|
||||
window.localStorage.setItem(proClickState, JSON.stringify(proStateData));
|
||||
};
|
||||
|
||||
const getProClickState = () => {
|
||||
try {
|
||||
const p = window.localStorage.getItem(proClickState);
|
||||
|
||||
if (p) {
|
||||
return JSON.parse(p);
|
||||
}
|
||||
|
||||
window.localStorage.setItem(
|
||||
proClickState,
|
||||
JSON.stringify(defaultProClickState)
|
||||
);
|
||||
|
||||
return defaultProClickState;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return defaultProClickState;
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
getLoveConsentState,
|
||||
setLoveConsentState,
|
Loading…
Reference in New Issue
Block a user