In Node.js I am using Npm MySQL to insert 6000 records in my database and would like to proceed after every single record is inserted.
I am experiencing a problem where the results are actually returned but I can see by executing select statements on the database that they are incrementing slowly and not completed
How can I get the results only after all the insert are completed on my MysQl database?
I have tried using Promise.all which didn’t work.
The structure of my insert looks something like this [[ {id:1, value: ‘Test’}, {id:2, value: ‘Test2’} ]]
My code is:
export function insertAssets(assets) {
const dbConfig = config.mysql.connection;
const connection = mysql.createConnection(dbConfig);
const insertCompletionPromises = [];
const sqlQuery = 'INSERT INTO MYTABLE SET ?';
// return new Promise ((resolve, reject ) => {
assets.map( assetMain => {
assetMain.map(assetInsert => {
connection.query(sqlQuery, assetInsert, (err, results) => {
try {
if(err)
throw err;
insertCompletionPromises.push(results);
console.log(results);
}
catch(err){
console.log(`MYTABLE insert error ${err}`);
}
});
});
});
return Promise.all(insertCompletionPromises).then(() => {return 'Completed'; } );
}
via user1526912
No comments:
Post a Comment