-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathdb.js
35 lines (30 loc) · 842 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
'use strict';
var q = require('q');
var fs = require('fs-extra');
var mongoose = require('mongoose');
var path = require('path');
function connect(config) {
var defer = q.defer();
mongoose.connect(config.connectionString, function(err) {
if (err) {
return defer.reject(err);
}
console.log('Connected to MongoDB at', config.connectionString);
defer.resolve();
});
return defer.promise;
}
module.exports.connect = connect;
module.exports.connectFromFileConfig = function() {
var dbPath = path.resolve(__dirname + '/config/data/db.json');
var dbConf = fs.readJsonSync(dbPath);
return connect(dbConf);
};
module.exports.disconnect = function() {
var defer = q.defer();
console.log('Disconnecting from MongoDB');
mongoose.disconnect(function() {
defer.resolve();
});
return defer.promise;
};