NPM - MYSQL - Passing query results to another variable - mysql

I was looking for some help in regards to a database package that I'm building for a larger application. The application will essentially keep certain information on a database through the use of the mysql package in npm. The problem I'm facing is that when I try to pass the results variable to a variable that I've created I always get undefined. Though if I place a log statement underneath the variable transfer statement displaying the results variable it will be properly populated. I've done a fair amount of research and believe that this has something to do with promises and/or the callback function that's part of the query. I was wondering if I could have someone examine my code and let me know the best course of action. I've spent a couple of hours on this researching online and trying various solutions but nothing has worked.
const mysql = require("mysql");
var sqlResults;
var pool = mysql.createPool({
host: "example.com",
user: "exampleUser",
password: "123456Password",
database: "TestDB"
});
module.exports = {
...
databaseSelect: function(table, fields, conditionalStmt) {
pool.getConnection(function(err, connection) {
if (err)
throw err;
console.log("Connected to the example DB!");
var sql = "SELECT " + fields + " FROM " + table + " " + conditionalStmt;
connection.query(sql, function(error, results, fields) {
console.log("Successfully retrieved records from " + table + "\n\t" + sql);
sqlResults = results;
connection.release();
sqlResults = results;
console.log(results);
if (error)
throw error;
});
});
console.log(sqlResults);
return sqlResults;
}
}
Here's a sample of the output that I'm receiving:
I am ready!
undefined
undefined
Connected to the example DB!
Successfully retrieved records from User_Level_Info
SELECT HashID, Level, Experience FROM User_Level_Info WHERE HashID = 'e578059cabc6f937f0219127384126143e272acbac52c331345d573e0f085d21'
[ RowDataPacket {
HashID: 'e578059cabc6f937f0219127384126143e272acbac52c331345d573e0f085d21',
Level: 1,
Experience: 0 } ]

Here convert it into a Promise.
So this will wait until it gets resolved or reject
databaseSelect: function(table, fields, conditionalStmt) {
return new Promise(function(resolve, reject) {
pool.getConnection(function(err, connection) {
if (err)
return reject(err);
console.log("Connected to the example DB!");
var sql = "SELECT " + fields + " FROM " + table + " " + conditionalStmt;
connection.query(sql, function(error, results, fields) {
console.log("Successfully retrieved records from " + table + "\n\t" + sql);
sqlResults = results;
connection.release();
sqlResults = results;
console.log(results);
if (error)
return reject(error);
});
});
resolve(sqlResults);
});
}
//call your function
databaseSelect(params)
.then(function(rows) {
console.log(rows)
})
.catch((err) {
console.log(err)
}); // Throw async to escape the promise chain

Related

nodejs mysql how to close connection after a loop entered data into db

I have a fairly simple program which collect data from aws using aws-cli and nodejs and then enter it into MySQL. To be fair it does work, the only thing is that I would like this program to be scheduled to run every now and then but I cannot as it does not exit : when all data have been entered I do not end the connection. I tried to figure out how to do just that (close/end the connection to MySQL) with promises and async/await without success. Surely something I am not doing right (find it hard to grasp the concept of promises and asynchronous programing so far) but did try for days on end though.
[...]
function insertInstanceDetails(date, zone, instance_id, name){
let insert_details = "INSERT INTO instances (date, zone, instance_type, name) VALUES(" + db.escape(date) + "," + db.escape(zone) + "," + db.escape(instance_id) + "," + db.escape(name) + ")";
db.query(insert_details, function(err, result){
if(err) throw err;
});
return;
}
for(instance-id of instance-ids){
aws.command('ec2 describe-instaces .....').then(function (data) {
var result = JSON.parse(data.raw);
for (var key in result.xyz) {
insertInstanceDetails(.......);
}
});
}
Thanks for helping. ;-)
One way of keeping track of whether the function insertInstanceDetails done executing is to convert it into a promise as well.
function insertInstanceDetails(date, zone, instance_id, name) {
return new Promise((resolve, reject) => {
let insert_details = "INSERT INTO instances (date, zone, instance_type, name) VALUES(" + db.escape(date) + "," + db.escape(zone) + "," + db.escape(instance_id) + "," + db.escape(name) + ")";
db.query(insert_details, function(err, result) {
if (err) return reject(err);
});
return resolve(result);
});
}
And as for the for..of loop, it will act as the promise handler, and once it knows all the SQL job is done, it will close out the SQL connection as soon as possible
for (instance - id of instance - ids) {
aws.command('ec2 describe-instaces .....').then(function(data) {
var result = JSON.parse(data.raw);
const insertInserteDetailsJobs = [];
for (var key in result.xyz) {
insertInstanceDetailsJobs.push(insertInstanceDetails(.......));
}
// Handle, and watch over all those micro-promise
return Promise.all(insertInstanceDetailsJobs);
})
.then(sqlResults => {
// Now we know all the insert details has beeing successfully inseted into your DB
// We can now close the SQL connection safely
...
myDB.close();
})
.catch(err => {
// Something fail
console.error(err);
});
}

MySQL NodeJs - Proper way to get rows.each to work

When I look for simple examples, everybody's style seems quite different. I'm tried 2 different styles, and got 2 different issues. In the code below, I have identified the source of the code and the error it gets in comments. I comment out or uncomment out each section and run separately, but each one has it's own errors. The "console.log(rows); " statement is showing the data, so the query itself is running and working.
// get the client
const mysql = require('mysql2');
const dashline = '---------------------------------';
console.log (dashline);
console.log ("Starting test");
// create the connection to database
const connection = mysql.createConnection({
host: 'myhost',
user: 'myuser',
password: 'mypass',
database: 'myDatabase'
});
console.log ("Got the database connection");
query = "select ID, user_nicename, user_email from wp_users where user_login = 'admin' limit 3 ";
console.log ("Starting query");
// Attempt 1
/*
connection.query(query, function(err, rows, fields){
if (err) throw err;
// from: https://html5hive.org/node-js-quickies-working-with-mysql/
// error: SyntaxError: Unexpected token { (on the rows.each line below)
rows.each(element, index) {
console.log(element.ID+ " " + element.user_nicename);
}
console.log (dashline);
console.log ("Query End");
process.exit(); // Else Node hangs and must hit cntl-break to exit
});
*/
// Attempt 2
connection.query(query, function(err, rows, fields){
if (err) throw err;
console.log(rows);
// Roughly based on example on this page:
// https://datatables.net/reference/api/each()
// TypeError: rows.each is not a function
rows.each( function(element, index) {
console.log(element.ID + " " + element.user_nicename);
});
console.log (dashline);
console.log ("The end");
process.exit(); // Else Node hangs and must hit cntl-break to exit
});
The method .each for Arrays doesn't exist, you should be using .forEach(function (element, index) {...}) instead
Use the following:
rows.forEach( function(element, index) {
console.log(element.ID + " " + element.user_nicename);
});
They are certainly similar, but there are differences. For example, "forEach" is an array method, but "$.each" can be used on any type of collection. And "forEach" is a built-in, whereas "$.each" requires loading the jQuery library.
Got an answer here: [https://github.com/sidorares/node-mysql2/issues/999[1]. Problem with .forEach, .each or .map is that you are inside another function which is not an async function, meaning you cannot use "await" to call another async routine.
for (let r=0; r < rows.length; ++r) {
console.log(rows[r].ID + " " + rows[r].user_nicename);
await UpdatePassword(connection, rows[r].ID);
}
He also provided this alternative:
One "functional" way to iterate sequentially similar to map ( imo slightly less readable then for loop ):
await rows.reduce( async (previousPromise, row) => {
await previousPromise;
return UpdatePassword(row.ID);
}, Promise.resolve());

Node.js mysql db connects after the query

I am trying to write a database manager module in node.js for some of the common queries I am going to use in a server app. I want to test the module. However, my code does not connect to the database before I do the queries. How can I arrange my code in such way, it connects to db before the queries, not after them?
Code:
var mysql = require('mysql');
var conn = mysql.createConnection(
{
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb'
});
conn.connect(function(error)
{
if ( error) throw error;
console.log('->connected to database');
});
// user as json
getUserData = function(username)
{
conn.query('SELECT * FROM USERS WHERE USERNAME = \"' + username +
'\"' , function (error, result, fields)
{
if (error) throw error;
return result;
});
}
console.log('result->' + JSON.stringify(getUserData('eness')));
conn.end();
Output:
result->undefined
->connected to database
Your getUserData() has nothing to return. Returning result from the query's callback function does not make it the return value of getUserData() as callback is executed asynchronously.
You can't return from an asynchronous function call inside a synchronous function.
If you move the console.log() line into the callback, you will see the correct output:
getUserData = function(username) {
conn.query('SELECT * FROM USERS WHERE USERNAME = \"' + username + '\"' , function (error, result, fields) {
if (error) throw error;
console.log('result->' + JSON.stringify(result));
});
}
getUserData('eness');
conn.end();
If you don't want to put the logic in the query's callback (separate query and business logic):
getUserData = function(username, cb) {
conn.query('SELECT * FROM USERS WHERE USERNAME = \"' + username + '\"' , function (error, result, fields) {
if (error) throw error;
cb(result)
});
}
getUserData('eness', function(result) {
console.log('result->' + JSON.stringify(result));
});
conn.end();
Welcome to callback hell :) Now consider using Promise.
This is happening because of the async nature of node.js , the code after conn.connect will not wait for that connection to complete to execute getUserData, therefore since the getUserData operation is not waiting for the connection to happen before executing you're probably having this error. Try the following to fix this for only testing purposes. And if youre using the getUserData function connected to a REST endpoint this will not occur since connection happens when the nodejs app is starting.
conn.connect(function(error)
{
if ( error) throw error;
else console.log('result->' + JSON.stringify(getUserData('eness')));
console.log('->connected to database');
});
function getUserData(username)
{
conn.query('SELECT * FROM USERS WHERE USERNAME = \"' + username +
'\"' , function (error, result, fields)
{
if (error) throw error;
return result;
});
}
conn.end();

Use promise to process MySQL return value in node.js

I have a python background and is currently migrating to node.js. I have problem adjusting to node.js due to its asynchronous nature.
For example, I am trying to return a value from a MySQL function.
function getLastRecord(name)
{
var connection = getMySQL_connection();
var query_str =
"SELECT name, " +
"FROM records " +
"WHERE (name = ?) " +
"LIMIT 1 ";
var query_var = [name];
var query = connection.query(query_str, query_var, function (err, rows, fields) {
//if (err) throw err;
if (err) {
//throw err;
console.log(err);
logger.info(err);
}
else {
//console.log(rows);
return rows;
}
}); //var query = connection.query(query_str, function (err, rows, fields) {
}
var rows = getLastRecord('name_record');
console.log(rows);
After some reading up, I realize the above code cannot work and I need to return a promise due to node.js's asynchronous nature. I cannot write node.js code like python. How do I convert getLastRecord() to return a promise and how do I handle the returned value?
In fact, what I want to do is something like this;
if (getLastRecord() > 20)
{
console.log("action");
}
How can this be done in node.js in a readable way?
I would like to see how promises can be implemented in this case using bluebird.
This is gonna be a little scattered, forgive me.
First, assuming this code uses the mysql driver API correctly, here's one way you could wrap it to work with a native promise:
function getLastRecord(name)
{
return new Promise(function(resolve, reject) {
// The Promise constructor should catch any errors thrown on
// this tick. Alternately, try/catch and reject(err) on catch.
var connection = getMySQL_connection();
var query_str =
"SELECT name, " +
"FROM records " +
"WHERE (name = ?) " +
"LIMIT 1 ";
var query_var = [name];
connection.query(query_str, query_var, function (err, rows, fields) {
// Call reject on error states,
// call resolve with results
if (err) {
return reject(err);
}
resolve(rows);
});
});
}
getLastRecord('name_record').then(function(rows) {
// now you have your rows, you can see if there are <20 of them
}).catch((err) => setImmediate(() => { throw err; })); // Throw async to escape the promise chain
So one thing: You still have callbacks. Callbacks are just functions that you hand to something to call at some point in the future with arguments of its choosing. So the function arguments in xs.map(fn), the (err, result) functions seen in node and the promise result and error handlers are all callbacks. This is somewhat confused by people referring to a specific kind of callback as "callbacks," the ones of (err, result) used in node core in what's called "continuation-passing style", sometimes called "nodebacks" by people that don't really like them.
For now, at least (async/await is coming eventually), you're pretty much stuck with callbacks, regardless of whether you adopt promises or not.
Also, I'll note that promises aren't immediately, obviously helpful here, as you still have a callback. Promises only really shine when you combine them with Promise.all and promise accumulators a la Array.prototype.reduce. But they do shine sometimes, and they are worth learning.
I have modified your code to use Q(NPM module) promises.
I Assumed your 'getLastRecord()' function that you specified in above snippet works correctly.
You can refer following link to get hold of Q module
Click here : Q documentation
var q = require('q');
function getLastRecord(name)
{
var deferred = q.defer(); // Use Q
var connection = getMySQL_connection();
var query_str =
"SELECT name, " +
"FROM records " +
"WHERE (name = ?) " +
"LIMIT 1 ";
var query_var = [name];
var query = connection.query(query_str, query_var, function (err, rows, fields) {
//if (err) throw err;
if (err) {
//throw err;
deferred.reject(err);
}
else {
//console.log(rows);
deferred.resolve(rows);
}
}); //var query = connection.query(query_str, function (err, rows, fields) {
return deferred.promise;
}
// Call the method like this
getLastRecord('name_record')
.then(function(rows){
// This function get called, when success
console.log(rows);
},function(error){
// This function get called, when error
console.log(error);
});
I am new to Node.js and promises. I was searching for a while for something that will meet my needs and this is what I ended up using after combining several examples I found. I wanted the ability to acquire connection per query and release it right after the query finishes (querySql), or to get a connection from pool and use it within Promise.using scope, or release it whenever I would like it (getSqlConnection).
Using this method you can concat several queries one after another without nesting them.
db.js
var mysql = require('mysql');
var Promise = require("bluebird");
Promise.promisifyAll(mysql);
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);
var pool = mysql.createPool({
host: 'my_aws_host',
port: '3306',
user: 'my_user',
password: 'my_password',
database: 'db_name'
});
function getSqlConnection() {
return pool.getConnectionAsync().disposer(function (connection) {
console.log("Releasing connection back to pool")
connection.release();
});
}
function querySql (query, params) {
return Promise.using(getSqlConnection(), function (connection) {
console.log("Got connection from pool");
if (typeof params !== 'undefined'){
return connection.queryAsync(query, params);
} else {
return connection.queryAsync(query);
}
});
};
module.exports = {
getSqlConnection : getSqlConnection,
querySql : querySql
};
usage_route.js
var express = require('express');
var router = express.Router();
var dateFormat = require('dateformat');
var db = require('../my_modules/db');
var getSqlConnection = db.getSqlConnection;
var querySql = db.querySql;
var Promise = require("bluebird");
function retrieveUser(token) {
var userQuery = "select id, email from users where token = ?";
return querySql(userQuery, [token])
.then(function(rows){
if (rows.length == 0) {
return Promise.reject("did not find user");
}
var user = rows[0];
return user;
});
}
router.post('/', function (req, res, next) {
Promise.resolve().then(function () {
return retrieveUser(req.body.token);
})
.then(function (user){
email = user.email;
res.status(200).json({ "code": 0, "message": "success", "email": email});
})
.catch(function (err) {
console.error("got error: " + err);
if (err instanceof Error) {
res.status(400).send("General error");
} else {
res.status(200).json({ "code": 1000, "message": err });
}
});
});
module.exports = router;
I am still a bit new to node, so maybe I missed something let me know how it works out. Instead of triggering async node just forces it on you, so you have to think ahead and plan it.
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'localhost',
user: 'user', password: 'password',
database: 'database',
});
db.connect((err) => {
// you should probably add reject instead of throwing error
// reject(new Error());
if(err){throw err;}
console.log('Mysql: Connected');
});
db.promise = (sql) => {
return new Promise((resolve, reject) => {
db.query(sql, (err, result) => {
if(err){reject(new Error());}
else{resolve(result);}
});
});
};
Here I am using the mysql module like normal, but instead I created a new function to handle the promise ahead of time, by adding it to the db const. (you see this as "connection" in a lot of node examples.
Now lets call a mysql query using the promise.
db.promise("SELECT * FROM users WHERE username='john doe' LIMIT 1;")
.then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
What I have found this useful for is when you need to do a second query based on the first query.
db.promise("SELECT * FROM users WHERE username='john doe' LIMIT 1;")
.then((result)=>{
console.log(result);
var sql = "SELECT * FROM friends WHERE username='";
sql = result[0];
sql = "';"
return db.promise(sql);
}).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
You should actually use the mysql variables, but this should at least give you an example of using promises with mysql module.
Also with above you can still continue to use the db.query the normal way anytime within these promises, they just work like normal.
Hope this helps with the triangle of death.
You don't need to use promises, you can use a callback function, something like that:
function getLastRecord(name, next)
{
var connection = getMySQL_connection();
var query_str =
"SELECT name, " +
"FROM records " +
"LIMIT 1 ";
var query_var = [name];
var query = connection.query(query_str, query_var, function (err, rows, fields) {
//if (err) throw err;
if (err) {
//throw err;
console.log(err);
logger.info(err);
next(err);
}
else {
//console.log(rows);
next(null, rows);
}
}); //var query = connection.query(query_str, function (err, rows, fields) {
}
getLastRecord('name_record', function(err, data) {
if(err) {
// handle the error
} else {
// handle your data
}
});
Using the package promise-mysql the logic would be to chain promises using then(function(response){your code})
and
catch(function(response){your code}) to catch errors from the "then" blocks preceeding the catch block.
Following this logic, you will pass query results in objects or arrays using return at the end of the block. The return will help passing the query results to the next block. Then, the result will be found in the function argument (here it is test1). Using this logic you can chain several MySql queries and the code that is required to manipulate the result and do whatever you want.
the Connection object is created to be global because every object and variable created in every block are only local. Don't forget that you can chain more "then" blocks.
var config = {
host : 'host',
user : 'user',
password : 'pass',
database : 'database',
};
var mysql = require('promise-mysql');
var connection;
let thename =""; // which can also be an argument if you embed this code in a function
mysql.createConnection(config
).then(function(conn){
connection = conn;
let test = connection.query('select name from records WHERE name=? LIMIT 1',[thename]);
return test;
}).then(function(test1){
console.log("test1"+JSON.stringify(test1)); // result of previous block
var result = connection.query('select * from users'); // A second query if you want
connection.end();
connection = {};
return result;
}).catch(function(error){
if (connection && connection.end) connection.end();
//logs out the error from the previous block (if there is any issue add a second catch behind this one)
console.log(error);
});
To answer your initial question: How can this be done in node.js in a readable way?
There is a library called co, which gives you the possibility to write async code in a synchronous workflow. Just have a look and npm install co.
The problem you face very often with that approach, is, that you do not get Promise back from all the libraries you like to use. So you have either wrap it yourself (see answer from #Joshua Holbrook) or look for a wrapper (for example: npm install mysql-promise)
(Btw: its on the roadmap for ES7 to have native support for this type of workflow with the keywords async await, but its not yet in node: node feature list.)
This can be achieved quite simply, for example with bluebird, as you asked:
var Promise = require('bluebird');
function getLastRecord(name)
{
return new Promise(function(resolve, reject){
var connection = getMySQL_connection();
var query_str =
"SELECT name, " +
"FROM records " +
"WHERE (name = ?) " +
"LIMIT 1 ";
var query_var = [name];
var query = connection.query(query_str, query_var, function (err, rows, fields) {
//if (err) throw err;
if (err) {
//throw err;
console.log(err);
logger.info(err);
reject(err);
}
else {
resolve(rows);
//console.log(rows);
}
}); //var query = connection.query(query_str, function (err, rows, fields) {
});
}
getLastRecord('name_record')
.then(function(rows){
if (rows > 20) {
console.log("action");
}
})
.error(function(e){console.log("Error handler " + e)})
.catch(function(e){console.log("Catch handler " + e)});
May be helpful for others, extending #Dillon Burnett answer
Using async/await and params
db.promise = (sql, params) => {
return new Promise((resolve, reject) => {
db.query(sql,params, (err, result) => {
if(err){reject(new Error());}
else{resolve(result);}
});
});
};
module.exports = db;
async connection(){
const result = await db.promise("SELECT * FROM users WHERE username=?",[username]);
return result;
}

Node.js mysql transaction

Can anyone provide an example of how I could achieve MySQL transactions in Node.js. I am trying to get my head around using the node-mysql driver and node-mysql-queue.
As far are I can tell, using node-mysql-queue greatly reduces the asynchronous nature of Node.js as new queries have to wait until existing ones have completed. To get around this, has anyone attempted to combine node-mysql-queue with node-mysql's connection-pooling capabilities. i.e starting a new mysql connection for each new http request, and starting transaction queues on individual connections?
Update
See the edit below for async/await syntax
I spent some time writing a generalized version of the transaction example given by node mysql, so I thought I would share it here. I am using Bluebird as my promise library, and used it to 'promisify' the connection object which simplified the asynchronous logic a lot.
const Promise = ('bluebird');
const mysql = ('mysql');
/**
* Run multiple queries on the database using a transaction. A list of SQL queries
* should be provided, along with a list of values to inject into the queries.
* #param {array} queries An array of mysql queries. These can contain `?`s
* which will be replaced with values in `queryValues`.
* #param {array} queryValues An array of arrays that is the same length as `queries`.
* Each array in `queryValues` should contain values to
* replace the `?`s in the corresponding query in `queries`.
* If a query has no `?`s, an empty array should be provided.
* #return {Promise} A Promise that is fulfilled with an array of the
* results of the passed in queries. The results in the
* returned array are at respective positions to the
* provided queries.
*/
function transaction(queries, queryValues) {
if (queries.length !== queryValues.length) {
return Promise.reject(
'Number of provided queries did not match the number of provided query values arrays'
)
}
const connection = mysql.createConnection(databaseConfigs);
Promise.promisifyAll(connection);
return connection.connectAsync()
.then(connection.beginTransactionAsync())
.then(() => {
const queryPromises = [];
queries.forEach((query, index) => {
queryPromises.push(connection.queryAsync(query, queryValues[index]));
});
return Promise.all(queryPromises);
})
.then(results => {
return connection.commitAsync()
.then(connection.endAsync())
.then(() => {
return results;
});
})
.catch(err => {
return connection.rollbackAsync()
.then(connection.endAsync())
.then(() => {
return Promise.reject(err);
});
});
}
If you wanted to use pooling as you suggested in the question, you could easily switch the createConnection line with myPool.getConnection(...), and switch the connection.end lines with connection.release().
Edit
I made another iteration of the code using the mysql2 library (same api as mysql but with promise support) and the new async/await operators. Here is that
const mysql = require('mysql2/promise')
/** See documentation from original answer */
async function transaction(queries, queryValues) {
if (queries.length !== queryValues.length) {
return Promise.reject(
'Number of provided queries did not match the number of provided query values arrays'
)
}
const connection = await mysql.createConnection(databaseConfigs)
try {
await connection.beginTransaction()
const queryPromises = []
queries.forEach((query, index) => {
queryPromises.push(connection.query(query, queryValues[index]))
})
const results = await Promise.all(queryPromises)
await connection.commit()
await connection.end()
return results
} catch (err) {
await connection.rollback()
await connection.end()
return Promise.reject(err)
}
}
The following transaction example was added to the documentation a month ago:
https://github.com/felixge/node-mysql#transactions
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO posts SET title=?', title, function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
var log = 'Post ' + result.insertId + ' added';
connection.query('INSERT INTO log SET data=?', log, function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('success!');
});
});
});
});
I am using the following approach. There is an add function in my Model where I am performing database operations.
add : function (data, callback) {
//Begin transaction
connection.beginTransaction(function(err) {
if (err) {
throw err;
}
var user_query = "INSERT INTO `calldata`.`users` (`username`, `password`, `enabled`, `accountNonExpired`, `accountNonLocked`, `credentialsNonExpired`) VALUES ('" + data.mobile + "', '" + sha1(data.password) + "', '1', '1', '1', '1')";
connection.query(user_query, function(err, results) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
var accnt_dtls_query = "INSERT INTO `calldata`.`accnt_dtls` (`req_mob_nmbr`, `usr_nme`, `dvce_id`, `mngr_id`, `cmpny_id`, `actve_flg`, `crtd_on`, `usr`) VALUES (" + data.mobile + ", '" + data.name + "', '', " + data.managerId + ", " + data.companyId + ", 1, now(), '" + data.mobile+ "')";
connection.query(accnt_dtls_query, function(err, results) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
var user_role_query = "INSERT INTO `calldata`.`user_roles` (`username`, `ROLE`) VALUES ('" + data.mobile + "', '" + data.role + "')";
connection.query(user_role_query, function(err, result) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
//add an entry to manager table
var mngr_dtls_query = "INSERT INTO `calldata`.`mngr_dtls` (`mngr_nm`, `cmpny_id`, `crtd_on`, `usr_nm`, `eml_id`) VALUES ('" + data.name + "'," + data.companyId + " , now(), '" + data.mobile + "', '" + data.mobile + "')";
connection.query(mngr_dtls_query, function(err, result) {
if (err) {
return connection.rollback(function () {
throw err;
});
}
console.log('Changed ' + result.changedRows + ' results');
connection.commit(function (err) {
console.log('Commiting transaction.....');
if (err) {
return connection.rollback(function () {
throw err;
});
}
console.log('Transaction Complete.');
connection.end();
callback(null, result);
});
});
});
});
});
});
//transaction ends here
}
and calling from controller:
agentAccountModel.add(data, function(err, results) {
if(err)
{
res.status(500);
res.json({
"status": 500,
"message": err
});
}
res.status(200);
res.json({
"status": 200,
"message": "Saved successfully"
});
});
I have come up with a solution using a recursive function.
var sql = 'INSERT INTO logs SET data = ?';
// array of rows to insert
var rows = [[/*first row*/], [/*additional row*/]];
connection.beginTransaction(function (err) {
if (err) {
throw err;
}
var insertEachRow = function () {
var row = rows.shift();
if (! row) {
// Done, now commit
return noMoreRows();
}
connection.query(sql, row, function (err, result) {
if (err) {
connection.rollback(function () {
throw err;
});
}
insertEachRow();
});
};
var noMoreRows = function () {
connection.commit(function (err) {
if (err) {
connection.rollback(function () {
throw err;
});
}
console.log('success!');
});
};
insertEachRow();
});
I found one useful links which uses node js mysql pooling with transaction. Database Connection pooling is always useful. One can check this link
https://github.com/mysqljs/mysql
I created a wrapper ORM type thing for this specific purpose, hope it helps SQl-connecton - pool ORM type helper methods
You can use it with the mysql also, just use this function without any parameters /call back .
Make sure you have async function and use these instead
await con.rollback();
await con.beginTransaction();
await con.commit();
Life set.....