/
database.js
41 lines (35 loc) · 911 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
34
35
36
37
38
39
40
41
/**
* Created by airswoop1 on 6/23/14.
*/
var MongoClient = require('mongodb').MongoClient,
config = require('./config.js'),
mongo_url = config.db.mongodb;
var mongoDatabase;
function getConnection(callback) {
if (mongoDatabase) {
callback(null, mongoDatabase);
}
else {
MongoClient.connect(mongo_url, function(err,db) {
if(err) {
console.error("Unable to connect to Mongo database.");
mongoDatabase = null;
callback(err, null);
}
else {
mongoDatabase = db;
callback(null, mongoDatabase);
}
});
}
};
function isConnected() {
if(typeof mongoDatabase === "undefined") {
return false;
}
else {
return true;
}
}
module.exports.getConnection = getConnection;
module.exports.isConnected = isConnected;