-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (86 loc) · 4.25 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var mysql = require('mysql');
var async = require("async");
var user_name = 'root';
var connection = mysql.createConnection({
host : 'localhost',
user : user_name,
password : '1234',
database : 'arsystems'
});
connection.connect();
connection.beginTransaction(function (err) {
if (err) { throw err; }
// Join consumer_product and product_alert which have the same prod_id
connection.query('SELECT product_alert.product_alert_id, consumer_product.consumer_id, product_alert.prod_id from product_alert INNER JOIN consumer_product ON product_alert.prod_id = consumer_product.prod_id', function (err, matchedTrans, fields) {
if (err) {
console.log('Error while performing Query.\n' + err);
throw err;
}
console.log('The solution is: ', matchedTrans);
console.log(matchedTrans.length);
var count = 0;
// Doing sync loop to make sure manufacturer_id received before next query get executed
async.whilst(
function () { return count < matchedTrans.length; },
function (callback) {
// Find the manufacturer_id
connection.query('SELECT product.manufacturer_id from product WHERE product.prod_id = ?', [matchedTrans[count].prod_id], function (err, mID, fields) {
if (err) {
console.log('Error while performing Query2.\n' + err);
throw err;
}
console.log('MID: ', mID);
matchedTrans[count].manufacturer_id = mID[0].manufacturer_id;
count++;
callback(err, matchedTrans);
})
},
function (err, result) {
if (err) {
console.log('Error in callback' + err);
throw err;
}
console.log('The solution2222 is: ', result);
count = 0;
// Doing sync loop to make sure the transaction can commit after everything is done
async.whilst(
function () { return count < result.length; },
function (callback) {
// insert new data if not exist
console.log('Date.now(): ' + Date.now());
console.log('Date.UTC(): ' + Date.UTC(2017));
connection.query('INSERT INTO consumer_product_alert (product_alert_id, consumer_id, prod_id, manufacturer_id, txn_userid, txn_dttm) VALUES (?, ?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE consumer_id=consumer_id;',
[result[count].product_alert_id, result[count].consumer_id, result[count].prod_id, result[count].manufacturer_id, user_name], function (err, rows, fields) {
if (err) {
console.log('Error while performing Query3.\n' + err);
return connection.rollback(function () {
throw err;
});
}
//console.log('INSERT RESULT ', count, rows);
count++;
callback(err, rows);
});
},
function (err, newResult) {
if (err) {
console.log('Error in callback' + err);
return connection.rollback(function () {
throw err;
});
}
//console.log('FINAL RESULT ', newResult);
connection.commit(function (err) {
if (err) {
return connection.rollback(function () {
throw err;
});
}
console.log('success!');
});
connection.end();
}
);
});
});
});