-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
48 lines (38 loc) · 982 Bytes
/
db.js
File metadata and controls
48 lines (38 loc) · 982 Bytes
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
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const {UserMongoose} = require('./models/user');
const {ToyMongoose} = require('./models/toy');
const state = {
db: null,
}
if (!process.env.MONGODB_URI) {
process.env.MONGODB_URI = 'mongodb://siestaderek:SpearFish1990!@ds127842.mlab.com:27842/wyroo'
}
exports.connect = function(done) {
if (state.db) {
return done();
}
mongoose.connect(process.env.MONGODB_URI);
const db = mongoose.connection;
db.on('error', function(error) {
console.log(error)
return done(error);
});
db.once('open', function() {
console.log('we\'re connected')
state.db = db;
done();
})
}
exports.get = function() {
return state.db;
}
exports.close = function(done) {
if (state.db) {
state.db.close(function(err, result) {
state.db = null;
state.mode = null;
done(err);
})
}
}