-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
43 lines (35 loc) · 870 Bytes
/
db.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
const assert = require("assert");
const mongoose = require("mongoose");
const dbKey = require("./config/keys").mongoURI;
let _db;
function init_db(callback) {
if (_db) {
console.warn("Init existing DB attempt");
return callback(null, _db);
}
mongoose.connect(dbKey, { useNewUrlParser: true }, connected);
function connected(err, db) {
if (err) {
return callback(err);
}
console.log("MongoDB connected");
_db = db;
callback(null, _db);
}
}
function get_db() {
assert.ok(_db, "Db not initialized yet. Called init_db first.");
return _db;
}
function check_db() {
if (_db) return true;
return false;
}
const close_db = () => _db.close();
module.exports = {
get_db,
init_db,
close_db,
check_db
};
//credit: https://itnext.io/how-to-share-a-single-database-connection-in-a-node-js-express-js-app-fcad4cbcb1e