Skip to content

Instantly share code, notes, and snippets.

@maxant
Created December 30, 2014 21:04
Show Gist options
  • Save maxant/e99bd8a69c190bb2f651 to your computer and use it in GitHub Desktop.
Save maxant/e99bd8a69c190bb2f651 to your computer and use it in GitHub Desktop.
Modifications to the node.jsj trading engine
function persistSales(sales, callback){
if(sales.length === 0 || process.env.skipPersistence) {
callback(); //nothing to do, so continue immediately
}else{
resources.dbConnection(function(err, connection) {
if(err) callback(err); else {
logger.info('preparing to persist ' + sales.length + ' sales');
var count = sales.length;
_.each(sales, function(sale){ //save them in parallel
connection.query(
'INSERT INTO SALES (BUYER_NAME, SELLER_NAME, PRODUCT_ID, PRICE, QUANTITY, PO_ID, SO_ID) values (?, ?, ?, ?, ?, ?, ?)',
[sale.buyer.name, sale.seller.name, sale.productId, sale.price, sale.quantity, sale.po.id, sale.so.id],
function(err, rows, fields) {
if(err) callback(err); else {
sale.id = rows.insertId;
count--;
if(count == 0){
logger.info('persisted all sales');
connection.release();
callback();
}
}
}
);
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment