Skip to content

Commit 6615117

Browse files
authored
Merge pull request #9 from NewCapital/addMarket
Wallets to the map data
2 parents b635060 + ef750f2 commit 6615117

File tree

10 files changed

+473
-2
lines changed

10 files changed

+473
-2
lines changed

lib/helpers/sql.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Created by Ori Braun on 2/9/2019.
3+
*/
4+
5+
var Sql = (function () {
6+
function Sql() {
7+
this._str = "";
8+
}
9+
Sql.prototype.select = function (str) {
10+
this._str += "SELECT " + str + " ";
11+
};
12+
Sql.prototype.from = function (table) {
13+
this._str += "FROM " + table + " ";
14+
};
15+
Sql.prototype.where = function (str) {
16+
this._str += "WHERE " + str + " ";
17+
};
18+
Sql.prototype.and_where = function (str) {
19+
this._str += "AND " + str + " ";
20+
};
21+
Sql.prototype.or_where = function (str) {
22+
this._str += "OR " + str + " ";
23+
};
24+
Sql.prototype.having = function (str) {
25+
this._str += "HAVING " + str + " ";
26+
};
27+
Sql.prototype.or_having = function (str) {
28+
this._str += "OR " + str + " ";
29+
};
30+
Sql.prototype.and_having = function (str) {
31+
this._str += "AND " + str + " ";
32+
};
33+
Sql.prototype.limit = function (limit, offset) {
34+
this._str += "LIMIT " + limit + " ";
35+
if(offset) {
36+
this._str += "OFFSET " + offset + " ";
37+
}
38+
};
39+
Sql.prototype.group_by = function (group_by) {
40+
this._str += "GROUP BY " + group_by + " ";
41+
};
42+
Sql.prototype.order_by = function (order_by) {
43+
this._str += "ORDER BY " + order_by + " ";
44+
};
45+
Sql.prototype.join = function (table, cond, type) {
46+
this._str += type + " JOIN " + table + " ON " + cond + " ";
47+
};
48+
Sql.prototype.union = function () {
49+
this._str += " UNION ";
50+
};
51+
Sql.prototype.union_all = function () {
52+
this._str += " UNION ALL ";
53+
};
54+
Sql.prototype.set = function (keys, values) {
55+
this._str += "SET ";
56+
for(var i = 0; i < keys.length; i++) {
57+
this._str += keys[i] + " = " + values[i];
58+
if(i < keys.length - 1) {
59+
this._str += ", ";
60+
}
61+
}
62+
this._str += " ";
63+
};
64+
Sql.prototype.insert = function (table, columns, values) {
65+
this._str += "INSERT INTO " + table + " ";
66+
if(columns.length) {
67+
this._str += "( " + columns.join(",") + " )";
68+
}
69+
this._str += "VALUES ( " + values.join(",") + " )";
70+
this._str += " ";
71+
};
72+
Sql.prototype.update = function (table) {
73+
this._str += "UPDATE " + table + " ";
74+
};
75+
Sql.prototype.delete = function (table) {
76+
this._str += "DELETE FROM " + table + " ";
77+
};
78+
Sql.prototype.truncate = function (table) {
79+
this._str += "TRUNCATE TABLE " + table + " ";
80+
};
81+
Sql.prototype.get = function () {
82+
return this._str;
83+
};
84+
return Sql;
85+
}());
86+
exports.Sql = Sql;

lib/mysql.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Created by Ori Braun on 2/9/2019.
3+
*/
4+
5+
var mysql = require('mysql');
6+
7+
var con = mysql.createConnection({
8+
host: "localhost",
9+
user: "yourusername",
10+
password: "yourpassword"
11+
});
12+
13+
con.connect(function(err) {
14+
if (err) {
15+
console.log(err.code)
16+
};
17+
console.log("Connected!");
18+
obj.getIps(function(results){
19+
console.log(results);
20+
})
21+
});
22+
23+
var obj = {
24+
getIps: function(cb) {
25+
var sql = "SELECT * FROM table";
26+
con.query(sql, function (err, result) {
27+
if (err) {
28+
console.log("err: " + err);
29+
cb(false)
30+
} else {
31+
console.log("Result: " + result);
32+
cb(result);
33+
}
34+
});
35+
}
36+
}
37+
38+
module.exports = obj;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Created by Ori Braun on 2/9/2019.
3+
*/
4+
5+
var mysql = require("mysql");
6+
var express = require('express');
7+
var app = express();
8+
var mySqlConnectionString = require("./mySqlConnectionString.js");
9+
var settings = mySqlConnectionString.mySqlConnection.connectionString.development;
10+
console.log("process.env.production",process.env.production);
11+
if (process.env.production) {
12+
settings = mySqlConnectionString.mySqlConnection.connectionString.production;
13+
}
14+
var mysqlConnectionProvider = {
15+
getSqlConnection : function() {
16+
var connection = mysql.createConnection(settings);
17+
connection.connect(function(error) {
18+
if (error) {
19+
// throw error;
20+
console.log(error);
21+
return;
22+
}
23+
console.log("mysql connection success");
24+
});
25+
// connection.end();
26+
return connection;
27+
},
28+
closeSqlConnection : function(currentConnection) {
29+
currentConnection.end(function(error) {
30+
if (error) {
31+
// throw error;
32+
console.log(error);
33+
return;
34+
}
35+
console.log("mysql connection closed");
36+
});
37+
return currentConnection;
38+
}
39+
};
40+
41+
module.exports.mysqlConnectionProvider = mysqlConnectionProvider;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Created by Ori Braun on 2/9/2019.
3+
*/
4+
5+
var mySqlConnectionString = {
6+
connectionString : {
7+
development : {
8+
host : "104.248.255.39",
9+
user : "twinsdb",
10+
password : "OAkw5CSfui2z390ZOpqQ6om3E",
11+
database : "twinsdb"
12+
},
13+
production : {
14+
host : "104.248.255.39",
15+
user : "twinsdb",
16+
password : "OAkw5CSfui2z390ZOpqQ6om3E",
17+
database : "twinsdb"
18+
}
19+
}
20+
};
21+
22+
exports.mySqlConnection = mySqlConnectionString;

lib/mysql/models/Model.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* Created by Ori Braun on 2/9/2019.
3+
*/
4+
5+
var connectionProvider = require("./../connection/mySqlConnectionProvider.js");
6+
var sqlHelper = require("../../helpers/sql.js");
7+
var tableName = "";
8+
var Model = {
9+
runSql : function(sqlStatement, callback) {
10+
var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection();
11+
var obj = {
12+
err : 0 ,
13+
errMessage : ""
14+
};
15+
var data = [];
16+
if (connection) {
17+
connection.query(sqlStatement, function(err, rows, fields) {
18+
if (err) {
19+
obj.err = 1;
20+
obj.errMessage = err;
21+
} else {
22+
rows.forEach(function (row) {
23+
data.push(row);
24+
});
25+
}
26+
obj.data = data;
27+
callback(obj);
28+
});
29+
}
30+
connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection);
31+
},
32+
getAll : function(callback) {
33+
console.log("getAll");
34+
var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection();
35+
var obj = {
36+
err : 0 ,
37+
errMessage : ""
38+
};
39+
var query = new sqlHelper.Sql();
40+
query.select("*");
41+
query.from(tableName);
42+
// query to the database and get the records
43+
if (connection) {
44+
connection.query(query.get(), function (err, rows) {
45+
46+
if (err) {
47+
obj.err = 1;
48+
obj.errMessage = err;
49+
}
50+
51+
// send records as a response
52+
// console.log(rows);
53+
if (rows) {
54+
obj.entities = rows;
55+
}
56+
callback(obj);
57+
});
58+
}
59+
connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection);
60+
},
61+
insert : function(columns, values, callback) {
62+
var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection();
63+
var obj = {
64+
err : 0 ,
65+
errMessage : ""
66+
};
67+
var query = new sqlHelper.Sql();
68+
query.insert(tableName,columns, values);
69+
// query to the database and get the records
70+
if (connection) {
71+
connection.query(query.get(), function (err, rows) {
72+
73+
if (err) {
74+
obj.err = 1;
75+
obj.errMessage = err;
76+
}
77+
78+
// send records as a response
79+
// console.log(rows);
80+
if (rows) {
81+
obj.data = rows;
82+
}
83+
callback(obj);
84+
});
85+
}
86+
connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection);
87+
},
88+
update : function(keys, values, condition, callback) {
89+
var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection();
90+
var obj = {
91+
err : 0 ,
92+
errMessage : ""
93+
};
94+
var query = new sqlHelper.Sql();
95+
query.update(tableName);
96+
query.set(keys,values);
97+
query.where(condition);
98+
// query to the database and get the records
99+
if (connection) {
100+
connection.query(query.get(), function (err, rows) {
101+
102+
if (err) {
103+
obj.err = 1;
104+
obj.errMessage = err;
105+
}
106+
107+
// send records as a response
108+
// console.log(rows);
109+
if (rows) {
110+
obj.data = rows;
111+
}
112+
callback(obj);
113+
});
114+
}
115+
connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection);
116+
},
117+
delete : function(condition,callback) {
118+
var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection();
119+
var obj = {
120+
err : 0 ,
121+
errMessage : ""
122+
};
123+
var query = new sqlHelper.Sql();
124+
query.delete(tableName);
125+
query.where(condition);
126+
// query to the database and get the records
127+
if (connection) {
128+
connection.query(query.get(), function (err, rows) {
129+
130+
if (err) {
131+
obj.err = 1;
132+
obj.errMessage = err;
133+
}
134+
135+
// send records as a response
136+
// console.log(rows);
137+
if (rows) {
138+
obj.data = rows;
139+
}
140+
callback(obj);
141+
});
142+
}
143+
connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection);
144+
},
145+
setTable: function(table) {
146+
tableName = table;
147+
},
148+
};
149+
150+
exports.Model = Model;

lib/mysql/models/ipsModel.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Created by Ori Braun on 2/9/2019.
3+
*/
4+
5+
var connectionProvider = require("./../connection/mySqlConnectionProvider.js");
6+
var sqlHelper = require("../../helpers/sql.js");
7+
var tableName = "twins_peer_list T";
8+
var ModelClass = require("./Model");
9+
ModelClass.Model.setTable(tableName);
10+
module.exports = {
11+
getAllIps : function(callback) {
12+
ModelClass.Model.getAll(callback);
13+
},
14+
};
15+
16+
// ipsModel.getAllIps(function(results) {
17+
// var ips = [];
18+
// for(var i in results.entities) {
19+
// ips.push(results.entities[i].ip);
20+
// }
21+
// console.log(ips);
22+
// })
23+
24+
// module.exports = ipsModel;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"start": "node --stack-size=10000 ./bin/cluster",
7+
"start-prod": " production=true node --stack-size=10000 ./bin/cluster",
78
"stop": "kill -2 $(cat tmp/cluster.pid)",
89
"test": "node ./node_modules/jasmine/bin/jasmine.js"
910
},
@@ -24,6 +25,7 @@
2425
"mongodb": "2.0.45",
2526
"mongoose": "4.1.10",
2627
"morgan": "^1.9.1",
28+
"mysql": "^2.16.0",
2729
"qr-image": "~2.0.0",
2830
"request": "^2.88.0",
2931
"static-favicon": "~1.0.0"

0 commit comments

Comments
 (0)