I am reading a json file and pulling key values that I want to insert into a MYSQL database at various points. There are inserts which need to be completed before other inserts inside jq.run()const jq = require('node-jq') const filter = '' const jsonPath = 'terraform.json' const options = { output: 'json' } jq.run(filter, jsonPath, options) .then((tfstate) => { var resobj = Object.keys(tfstate.modules[0].resources) // HERE I WANT TO INSERT a value INTO A MYSQL DATABASE, and return its insertId for (var i = 0; i < resobj.length; i++) { if ((tfstate.modules[0].resources[resobj[i]].type === 'vsphere_virtual_machine') && (tfstate.modules[0].resources[resobj[i]].primary.attributes['extra_config.fsct.friendlyname']) ) { var iname = tfstate.modules[0].resources[resobj[i]].primary.attributes['name'] // etc. console.log(`name->${iname}`) // HERE I WANT TO INSERT iname INTO A MYSQL DATABASE and use insertId from above } } }) .catch((err) => { console.error(err) // Something went wrong... }) I can wrap the mysql inserts separately and of course it works fine, like:var mysql = require('mysql') var con = mysql.createConnection({ host: 'localhost', user: 'user', password: 'pass', database: 'database' }) insert() async function insert () { con.connect() console.log('Connected to ' + con.config.host) jamSomeSQL("INSERT INTO `database`.`connection_parameter` (`connection_id`, `parameter_name`, `parameter_value`) VALUES ('" + conn.insertId + "', 'hostname', '" + iname + "');") console.log('Disconnecting... bye') con.end() } function jamSomeSQL (sql) { return new Promise(resolve => { con.query(sql, function (err, result) { if (err) throw err resolve(result) }) }) } But I don't find a way to make things run synchronously inside the jq.run(), so looking for advice. Maybe I just need to just build it in python, but am trying to learn node...thanks!
Submitted January 02, 2019 at 04:20PM by Jacksonp2008
No comments:
Post a Comment