I am using node-mysql, node-js, and Q promises.
I have successfully updated, deleted, and inserted single rows using the above. As well as inserted multiple rows in
this piece of code was taken from vcl.js for node.js, it is written in typescript and provides a multiple update,delete,inseer statment's in a single transaction.
export class SQLStatment {
sql: string;
params: Object
}
var dbError: string;
var execCount: number = 0;
function DBexecuteBatchMYSQLSingle(connection: any, SQLStatmentArray: Array, index: number, callback: () => void) {
execCount++;
connection.query(SQLStatmentArray[index].sql, SQLStatmentArray[index].params, function (err, rows, fields) {
if (err) dbError = err;
if (index + 1 == SQLStatmentArray.length) callback();
else {
if (!dbError) {
DBexecuteBatchMYSQLSingle(connection, SQLStatmentArray, index + 1, function () {
if (index == 0) callback();
});
}
}
});
}
function DBBatchUpdateMYSQL(SQLStatmentArray: Array, callback?: (err) => void) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host: "host",user: "user",database: "db",
port: 1022, password: "pass"});
dbError = null;
execCount = 0;
connection.beginTransaction(function (err) {
if (err && callback) callback("Database error:" + err);
else {
DBexecuteBatchMYSQLSingle(connection, SQLStatmentArray, 0, () => {
if (dbError) {
connection.rollback(function () {
if (callback) callback("Database error:" + dbError);
});
} else {
connection.commit(function (err) {
if (callback) callback(null);
})
}
});
}
});
}