Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to ts: GraphQL utils,SQL utils,Main utils #4522

Merged
merged 14 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,6 +33,7 @@ export const useIntrospectionSchema = (headers = {}) => {
setLoading(false);
setError(e);
});

return () => setSchema(null);
};

Expand Down
76 changes: 0 additions & 76 deletions console/src/components/Common/utils/sqlUtils.js

This file was deleted.

98 changes: 98 additions & 0 deletions console/src/components/Common/utils/sqlUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down