forked from deleepa/simple-api-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
33 lines (28 loc) · 882 Bytes
/
database.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
var mysql = require('mysql');
var log = require('debug-logger')('database.js');
module.exports = function Database() {
var config = {
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'root',
database: process.env.DB_NAME || 'simple-api-server'
};
//check if the user is using a password or not
if(typeof connection !== undefined) {
config.password = process.env.DB_PASSWORD || '';
}
//create the connection with the database
var connection = mysql.createConnection(config);
connection.connect(function(err) {
//failed attempt - log error
if(err) {
console.log("error: " + err.stack);
return null;
}
//success
else {
console.log("success");
return connection;
}
});
return connection;
};