lib/db.js
function mysqlBulkUpsert(model, data) {
  var mysql = require("mysql");
  var queries = ["SET AUTOCOMMIT = 0"];

  // Transform data
  _.each(data, function(d) {
    var query = "REPLACE INTO " + model.tableName + " ([[COLUMNS]]) VALUES (?)";
    query = query.replace("[[COLUMNS]]", _.keys(d).join(", "));
    query = mysql.format(query, [ _.values(d) ]);
    queries.push(query);
  });

  // Create final output and format
  queries.push("COMMIT");
  queries.push("SET AUTOCOMMIT = 1");
  return queries.join("; ");
}
function mysqlBulkUpsert(model, data) {
  var mysql = require("mysql");
  var queries = ["SET AUTOCOMMIT = 0"];

  // Transform data
  _.each(data, function(d) {
    var query = "REPLACE INTO " + model.tableName + " ([[COLUMNS]]) VALUES (?)";
    query = query.replace("[[COLUMNS]]", _.keys(d).join(", "));
    query = mysql.format(query, [ _.values(d) ]);
    queries.push(query);
  });

  // Create final output and format
  queries.push("COMMIT");
  queries.push("SET AUTOCOMMIT = 1");
  return queries.join("; ");
}
function mysqlBulkUpsert ( model , data ) { var mysql = require ( "mysql" ) ; var queries = [ "SET AUTOCOMMIT = 0" ] ; // Transform data _ . each ( data , function ( d ) { var query = "REPLACE INTO " + model . tableName + " ([[COLUMNS]]) VALUES (?)" ; query = query . replace ( "[[COLUMNS]]" , _ . keys ( d ) . join ( ", " ) ) ; query = mysql . format ( query , [ _ . values ( d ) ] ) ; queries . push ( query ) ; } ) ; // Create final output and format queries . push ( "COMMIT" ) ; queries . push ( "SET AUTOCOMMIT = 1" ) ; return queries . join ( "; " ) ; }
Mysql query format bulk upsert
Mysql query format bulk upsert