sanitise data of special characters before inserting using json2graphql (#770)

This commit is contained in:
Rishichandra Wawhal 2018-10-16 14:53:01 +05:30 committed by Shahidh K Muhammed
parent 58d02ef1c4
commit 043b76bab7
7 changed files with 31 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -13,6 +13,7 @@
"node/no-unsupported-features": "off",
"no-warning-comments": "off",
"semi": [1, "always"],
"camelcase": "off"
"camelcase": "off",
"guard-for-in": "off"
}
}

View File

@ -1,7 +1,7 @@
{
"name": "json2graphql",
"description": "A CLI tool to import JSON data in Hasura GraphQL Engine",
"version": "0.1.1",
"version": "0.1.3",
"author": "Hasura",
"bin": {
"json2graphql": "./bin/run",

View File

@ -69,6 +69,24 @@ const hasPrimaryKey = dataObj => {
return has;
};
const sanitizeData = db => {
const newDb = {};
for (var tableName in db) {
const newTableName = tableName.replace(/[^a-zA-Z0-9]/g, '_').replace(' ', '_');
newDb[newTableName] = [];
for (var i = db[tableName].length - 1; i >= 0; i--) {
const data = db[tableName][i];
const newData = {};
for (var key in data) {
const newKey = key.replace(/[^a-zA-Z0-9]/g, '_').replace(' ', '_');
newData[newKey] = data[key];
}
newDb[tableName].push(newData);
}
}
return newDb;
};
const generate = db => {
const metaData = [];
Object.keys(db).forEach(rootField => {
@ -89,4 +107,7 @@ const generate = db => {
return metaData;
};
module.exports = generate;
module.exports = {
generate,
sanitizeData,
};

View File

@ -1,4 +1,4 @@
const generate = require('./generateTables');
const {generate, sanitizeData} = require('./generateTables');
const {generateSql, runSql} = require('./sql');
const {cli} = require('cli-ux');
const {trackTables} = require('./track');
@ -6,11 +6,11 @@ const {getInsertOrder, insertData} = require('./insert');
const {createRelationships} = require('./relationships');
const {createTables} = require('./check');
const importData = async (db, url, headers, overwrite) => {
const importData = async (jsonDb, url, headers, overwrite) => {
cli.action.start('Processing JSON data');
const db = sanitizeData(jsonDb);
const tables = generate(db);
const sql = generateSql(tables);
sql.forEach(s => console.log(s + '\n'));
cli.action.stop('Done!');
cli.action.start('Checking database');
createTables(tables, url, headers, overwrite, runSql, sql).then(() => {

View File

@ -60,7 +60,7 @@ const insertData = async (insertOrder, sampleData, tables, url, headers) => {
let objectString = '';
const variables = {};
insertOrder.forEach(tableName => {
mutationString += `insert_${tableName} ( objects: $objects_${tableName} ) { returning { id } } \n`;
mutationString += `insert_${tableName} ( objects: $objects_${tableName} ) { affected_rows } \n`;
objectString += `$objects_${tableName}: [${tableName}_insert_input!]!,\n`;
variables[`objects_${tableName}`] = transformedData[tableName];
});

View File

@ -6,7 +6,8 @@ query {
routesByRoutesId {
leaguesByLeaguesId {
flightssByLeaguesId {
flightCommentssByFlightsId {
flightCommentssByFlightsId (order_by: users_id_asc){
users_id
usersByUsersId {
email
}