-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbManager.js
119 lines (107 loc) · 3.71 KB
/
dbManager.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var String = require('./scripts/sprintf.min.js');
var InsertLoanSQL = 'CALL insertLoan("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s");';
var InsertLanguagesSQL = 'CALL insertLanguage("%s","%s");';
var SelectLoanInfoSQL = 'SELECT * FROM loanInfo LIMIT %s';
var SelectLanguagesSQL = 'SELECT loan_id, GROUP_CONCAT(language) AS langList FROM languageView WHERE loan_id IN (%s) GROUP BY loan_id;';
//set up mysql connection
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'Passw0rd',
database : 'kivadb'
});
connection.connect();
module.exports = {
insertLoan: function(loan) {
console.log("Inserting loan: " + loan.id);
insertion(String.sprintf(InsertLoanSQL,loan.id, loan.activity, loan.basket_amount,
loan.borrower_count, loan.funded_amount, loan.image.id, loan.image.template_id,
loan.loan_amount, loan.name, loan.partner_id, loan.planned_expiration_date,
loan.posted_date, loan.sector, loan.status, loan.use, loan.location.country,
loan.location.country_code, loan.location.town));
loan.description.languages.forEach(function(language) {
insertion(String.sprintf(InsertLanguagesSQL, loan.id, language));
});
},
queryLoans: function(limit, callback) {
//Get newest 20 loans
console.log("limit: " + limit);
sqlQuery(String.sprintf(SelectLoanInfoSQL, limit), function(loanRows) {
var loans = [];
var rowIds = []
loanRows.forEach(function(row){
rowIds.push(row.id);
});
//Get lanuages associated with above loans grouped by loan id
sqlQuery(String.sprintf(SelectLanguagesSQL, rowIds.join(',')), function(languageRows){
var languagesByLoanId = [];
languageRows.forEach(function(languageRow){
languagesByLoanId[languageRow.loan_id] = languageRow.langList;
});
loanRows.forEach(function(loanRow){
loans.push(createLoanObj(loanRow, languagesByLoanId[loanRow.id]));
});
callback(loans);
});
});
}
};
//this is kinda gross, but need to build up the description and location objects to match kiva's api
function createLoanObj(row, languages){
var loanObj = new Object();
loanObj.activity = row.activity;
loanObj.basket_amount = row.basket_amount;
loanObj.borrower_count = row.borrower_count;
loanObj.description = new Object();
loanObj.description.languages = new Object();
loanObj.description.languages = languages.split(",");
loanObj.funded_amount = row.funded_amount;
loanObj.id = row.id;
loanObj.image = new Object();
loanObj.image.id = row.image_id;
loanObj.image.template_id = row.image_template_id;
loanObj.loan_amount = row.loan_amount;
loanObj.location = new Object();
loanObj.location.country = row.country;
loanObj.location.country_code = row.country_code;
loanObj.location.town = row.town;
loanObj.name = row.name;
loanObj.partner_id = row.partner_id;
loanObj.planned_expiration_date = row.planned_expiration_date;
loanObj.posted_date = row.posted_date;
loanObj.sector = row.sector;
loanObj.status = row.status;
loanObj.use = row.use;
return loanObj;
}
function insertion(insertString) {
connection.query(insertString, function(error, rows){
if (error == null){
console.log("Insertion successfull: " + insertString);
}
else{
if (error.code == "ER_DUP_ENTRY"){
console.log("Ignoring dup");
}
else {
console.error("Error inserting: " + insertString);
console.error(error);
}
}
});
}
function sqlQuery(queryString, callback) {
connection.query(queryString, function(error, rows){
if (error == null){
console.log("Query successfull");
}
else{
console.error("Error querying: " + queryString);
console.error(error);
}
if (rows != null){
callback(rows);
}
});
}