Skip to content

Commit

Permalink
fix promise problem and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
YoavKo committed May 1, 2022
1 parent a3b1e73 commit f4c09c7
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions db_data_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ const db = mysql.createConnection({

db.query('DELETE FROM aliens;');

const strOrNULL = (str) => { return str ? `\"${str}\"` : "NULL"; };
const strOrNULL = (str) => { return str ? `${str}` : null; };

const insertQuery = `INSERT INTO aliens(id, commander_id, name, weapon, vehicle, type)
VALUES ( ?, ?, ?, ?, ?, ?)`;

const insertQueryPrefix = 'INSERT INTO aliens(id, commander_id, name, weapon, vehicle, type) VALUES (';
fs.readFile('db.json', (err, data)=>{
const parseData = Object.entries(JSON.parse(data)).reverse();
const promises = [];

parseData.forEach(([type, aliens]) => {
aliens.forEach(alien => {
const q = insertQueryPrefix + `${alien.id}, ${alien.commanderId || "NULL"}, "${alien.name}" , ${strOrNULL(alien.weapon)}, ${strOrNULL(alien.vehicle)}, \"${type}\");`;
db.query(q);
const commander_id = alien.commanderId || null;
const weapon = strOrNULL(alien.weapon);
const vehicle = strOrNULL(alien.vehicle);
const valuseArray = [ alien.id, commander_id, alien.name, weapon, vehicle, type];

promises.push(db.promise().query(insertQuery, valuseArray));
});
});
Promise.all(promises).then(() => process.exit());
});

process.exit(0);

0 comments on commit f4c09c7

Please sign in to comment.