Skip to content

Commit eb80982

Browse files
committed
Lint but keep debug instructions for now
1 parent 108614b commit eb80982

File tree

5 files changed

+83
-85
lines changed

5 files changed

+83
-85
lines changed

src/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ app.get('*', async (req, res, next) => {
119119
},
120120
// Sending Material-UI theme through context
121121
muiTheme,
122-
//send apollo client in the context
122+
// send apollo client in the context
123123

124124
};
125125

tools/port_script/config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ module.exports = {
66
password: 'swd_base',
77
db: {
88
source: 'db_swd',
9-
destination: 'new_db_swd'
10-
}
11-
}
9+
destination: 'new_db_swd',
10+
},
11+
};

tools/port_script/index.js

+38-71
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,113 @@
1+
/* eslint-disable no-unused-expressions */
12
const mysql = require('mysql');
3+
const _ = require('lodash');
24
const config = require('./config.js');
35
const transform = require('./transform.js');
4-
const _ = require('lodash');
6+
const util = require('./util.js');
57

68
const DEBUG = 1;
79

810
// Connect to the source database
9-
const source = mysql .createConnection({
11+
const source = mysql.createConnection({
1012
host: config.host,
1113
user: config.user,
1214
password: config.password,
13-
database: config.db.source
15+
database: config.db.source,
1416
});
15-
source.connect(function(err) {
17+
source.connect((err) => {
1618
if (err) { throw err; }
1719
console.log('Connected to source');
1820
});
1921

2022
// Connect to the destination
21-
const dest = mysql .createConnection({
23+
const dest = mysql.createConnection({
2224
host: config.host,
2325
user: config.user,
2426
password: config.password,
25-
database: config.db.destination
26-
})
27-
dest.connect(function(err) {
27+
database: config.db.destination,
28+
});
29+
dest.connect((err) => {
2830
if (err) { throw err; }
2931
console.log('Connected to destination');
3032
});
3133

3234

35+
// Start execution
3336
_.forEach(transform, (rule) => {
34-
let select_query = null;
37+
let selectQuery = null;
3538
if (rule.table.old.union) {
3639
console.log('union');
37-
select_query = 'SELECT * FROM ' + rule.table.old.union[0] + ' INNER JOIN (';
40+
selectQuery = `SELECT * FROM ${rule.table.old.union[0]} INNER JOIN (`;
3841

3942
let count = 0;
4043
_.forEach(rule.table.old.union, (table) => {
41-
42-
if (count == 0) {
44+
if (count === 0) {
4345
console.log('hi');
44-
} else if (count == 1) {
45-
select_query += table;
46+
} else if (count === 1) {
47+
selectQuery += table;
4648
} else {
47-
select_query += ', ' + table;
49+
selectQuery += `, ${table}`;
4850
}
49-
count++;
51+
count += 1;
5052
});
5153

52-
select_query += ') ON (' + rule.table.old.on + ')';
54+
selectQuery += `) ON (${rule.table.old.on})`;
5355
} else {
54-
select_query = 'SELECT * FROM ' + rule.table.old;
56+
selectQuery = `SELECT * FROM ${rule.table.old}`;
5557
}
56-
DEBUG && (select_query += ' LIMIT 5');
57-
select_query += ';';
58-
console.log(select_query);
58+
DEBUG && (selectQuery += ' LIMIT 5');
59+
selectQuery += ';';
60+
console.log(selectQuery);
5961

6062
// Run the query
61-
source.query(select_query, function(err, tuples, fields) {
62-
63+
source.query(selectQuery, (err, tuples) => {
6364
if (err) { throw err; }
64-
console.log('\nCopying table ' + rule.table.new);
65+
console.log(`\nCopying table ${rule.table.new}`);
6566

6667
_.forEach(tuples, (tuple) => {
67-
6868
let query = 'INSERT INTO ';
6969
query += rule.table.new;
7070
query += ' (';
7171

7272
// Load fields in order
7373
let values = '';
7474
let count = 0;
75-
_.forEach(rule.fields, (field, field_name) => {
76-
75+
_.forEach(rule.fields, (field, fieldName) => {
7776
// Add comma if not the first item
78-
if (count != 0) {
77+
if (count !== 0) {
7978
query += ', ';
8079
values += ', ';
8180
}
8281

8382
// Add field name to query
84-
query += field_name;
83+
query += fieldName;
8584

8685
// TODO: Add various data cleaning techniques here
87-
let attribute = field;
86+
const attribute = field;
8887

8988
// check if there's a condition
9089
if (attribute.if) {
91-
92-
if (condition_eval(attribute.if.condition, tuple)) {
93-
values += '"' + attribute.if.pass.value + '"';
90+
if (util.conditionEval(attribute.if.condition, tuple)) {
91+
values += `"${attribute.if.pass.value}"`;
9492
} else {
95-
values += '"' + attribute.if.fail.value + '"';
93+
values += `"${attribute.if.fail.value}"`;
9694
}
97-
9895
} else {
99-
values += '"' + tuple[field] + '"';
100-
count++;
96+
values += `"${tuple[field]}"`;
97+
count += 1;
10198
}
10299
}); // end forEach(fields)
103100

104-
query += ') VALUES (' + values;
101+
query += `) VALUES (${values}`;
105102
query += ');';
106103

107104
// Run the query
108-
DEBUG && console.log(query + '\n');
109-
DEBUG || dest.query(query, function(err, ans) {
110-
if (err) { throw err; }
111-
else {
105+
DEBUG && console.log(`${query}\n`);
106+
DEBUG || dest.query(query, (error) => {
107+
if (error) { throw err; } else {
112108
console.log(query);
113109
}
114110
});
115-
116111
}); // end of forEach(res)
117112
});
118113
});
119-
120-
function condition_eval(condition, data) {
121-
if (condition.eval) {
122-
let first = data[condition.eval.first_attribute] || condition.eval.first_value || null;
123-
let second = data[condition.eval.second_attribute] || condition.eval.second_value || null;
124-
125-
switch (condition.eval.operator) {
126-
case '==':
127-
case '===':
128-
if (first == second) {
129-
return true;
130-
} else {
131-
return false;
132-
}
133-
break;
134-
default:
135-
throw 'Operator not supported';
136-
}
137-
} else if (condition.or) {
138-
// TODO: make this variable
139-
return condition_eval(condition.or[0], data) || condition_eval(condition.or[1], data);
140-
} else if (condition.and) {
141-
// TODO: make this variable
142-
return condition_eval(condition.or[0], data) && condition_eval(condition.or[1], data);
143-
} else {
144-
throw 'Could not find eval or a condition';
145-
}
146-
}

tools/port_script/transform.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module.exports = {
2-
0 : {
2+
0: {
33
table: {
44
old: {
55
union: ['student_info', 'cgpa'],
6-
on: 'student_info.login_id = cgpa.login_id'
6+
on: 'student_info.login_id = cgpa.login_id',
77
},
8-
new: 'student'
8+
new: 'student',
99
},
1010
fields: {
1111
id: 'login_id',
@@ -15,12 +15,12 @@ module.exports = {
1515
if: {
1616
condition: {
1717
or: [
18-
{ eval: { operator: '===', first_attribute: 'hostel', second_value: 'PS2' }, },
18+
{ eval: { operator: '===', first_attribute: 'hostel', second_value: 'PS2' } },
1919
{ eval: { operator: '===', first_attribute: 'hostel', second_value: 'THESIS' } },
20-
]
20+
],
2121
},
2222
pass: { value: 1 },
23-
fail: { value: 0 }
23+
fail: { value: 0 },
2424
},
2525
},
2626
gender: 'gender',
@@ -33,7 +33,7 @@ module.exports = {
3333
admit: 'admit',
3434
parentName: 'father_name',
3535
parentPhone: 'father_phone',
36-
parentEmail: 'father_email'
37-
}
38-
}
39-
}
36+
parentEmail: 'father_email',
37+
},
38+
},
39+
};

tools/port_script/util.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
// Function to evaluate conditions recursively
3+
function conditionEval(condition, data) {
4+
if (condition.eval) {
5+
const first = data[condition.eval.first_attribute] || condition.eval.first_value || null;
6+
const second = data[condition.eval.second_attribute] || condition.eval.second_value || null;
7+
8+
switch (condition.eval.operator) {
9+
case '==':
10+
case '===':
11+
if (first === second) {
12+
return true;
13+
}
14+
return false;
15+
default:
16+
throw new Error('Operator not supported');
17+
}
18+
} else if (condition.or) {
19+
// TODO: make this variable
20+
return conditionEval(condition.or[0], data) || conditionEval(condition.or[1], data);
21+
} else if (condition.and) {
22+
// TODO: make this variable
23+
return conditionEval(condition.or[0], data) && conditionEval(condition.or[1], data);
24+
} else {
25+
throw new Error('Could not find eval or a condition');
26+
}
27+
}
28+
29+
module.exports = {
30+
conditionEval,
31+
};

0 commit comments

Comments
 (0)