-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
executable file
·134 lines (123 loc) · 3.51 KB
/
db.js
File metadata and controls
executable file
·134 lines (123 loc) · 3.51 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* @author
* @copyright
*/
var g = require('./global.js'),
conf = require('./private.js');
mysql = require('mysql'),
// see https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
module.exports = function (env) {
var _conf = conf[env].mysql;
g.debug(' Instantiating database connection pool as ' + _conf['username'] + '@' + _conf['host'] + ':' + _conf['port']);
var _pool = mysql.createPool({
connectionLimit: 100, // max # of connections created
host: _conf.host,
port: _conf.port,
user: _conf.username,
password: _conf.password,
database: _conf.database,
debug: false,//true,
queueLimit: 0,
//acquireTimeout: 5000,
//connectTimeout: 5000,
});
_pool.getConnection(function (err, con) {
if (err) {
throw new Error('Error connecting to database - ' + err);
}
//g.debug(' Connected to database');
con.release();
});
function Table(name, overrides) {
this.name = name;
this.colUsername = 'email';
this.colPassword = 'password';
this.colToken = 'api_token';
// overrides
for (property in overrides) {
if (overrides.hasOwnProperty(property)) {
this[property] = overrides[property];
}
}
}
Table.prototype.getToken = function (pk, callback) {
var sql = 'select {0} as token from {1} where pk_{1}={2}'
.format(this.colToken, this.name, pk);
//g.debug(sql);
_pool.query(sql, function (err, rows, fields) {
if (err) {
g.error('Error fetching token from database - ' + err);
callback(null);
} else {
callback(rows);
}
});
}
Table.prototype.updateToken = function (pk, token, callback) {
// disable for customers for now
if (this.name == 'User') {
callback(1);
return;
}
var sql = "update {0} set {1}='{2}' where pk_{0}={3}"
.format(this.name, this.colToken, token, pk);
//g.debug(sql);
_pool.query(sql, function (err, rows, fields) {
if (err) {
g.error('Error updating token - ' + err);
callback(null);
} else {
callback(rows);
}
});
}
Table.prototype.getTokenByPrimaryKey = function (key, callback) {
var sql = "select api_token from {0} where pk_{0}={1}".format(this.name, key);
_pool.query(sql, function (err, rows, fields) {
if (err) {
g.error('Error fetching token from database by primary key - ' + err.message);
throw err;
} else {
callback(rows);
}
});
};
Table.prototype.getAuth = function (username, callback) {
var sql = "select pk_{0} as pk, {1} as username, {2} as password, {3} as api_token from {0} where {1}='{4}'"
.format(this.name, this.colUsername, this.colPassword, this.colToken, username);
//g.debug(sql);
// see https://github.com/felixge/node-mysql/
_pool.query(sql, function (err, rows, fields) {
if (err) {
g.error('Error fetching data from database - ' + err.message);
callback(null);
} else {
callback(rows);
}
});
};
return {
customer: new Table('User'),
driver: new Table('Driver'),
admin: new Table('admin_User', { colUsername: 'username' }),
system: new Table('houston_api_User', { colUsername: 'api_username', colPassword: 'api_password' }),
/*
exec: function (sql, callback) {
//console.log(sql);
// XXX: https://github.com/felixge/node-mysql/
_pool.query(sql, function (err, rows, fields) {
if (err) {
console.log('Error fetching data from database - ' + err.message);
callback(null);
} else {
// rows is an array of objects where each object is a single row and the keys
// are the fields
// rows = [
//console.log(rows);
callback(rows);
}
});
},
*/
};
}