-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.js
53 lines (36 loc) · 1.25 KB
/
wrapper.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
44
45
46
47
48
49
50
51
52
53
/*
This file is meant to abstract calls to the database from route logic.
Each call to the database should return a promise such that a route can
pass relevant information to the wrapper function:
***from other files the interaction should look like the following***
db.getTags().then(function success(data) {
... small amount of logic ...
res.send(data);
}, function error(err) {
res.status(50X).send(err);
});
*/
const mongoose = require('mongoose');
// Use JS native promises
mongoose.Promise = global.Promise;
// Connect to db
const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost/test';
connectToDB(mongoURI);
// Schemas we define:
const Bob = require('./bob');
const Tag = require('./tag');
const Flavors = require('./flavor');
var dbWrapper = {};
dbWrapper.Bob = Bob;
dbWrapper.Tag = Tag;
dbWrapper.Flavors = Flavors;
dbWrapper.ObjectId = mongoose.Types.ObjectId; // Mongo requires _id key to be an ObjectId
module.exports = dbWrapper;
function connectToDB(url) {
mongoose.connect(url);
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'mongoose connection error:'));
db.once('open', function(){
console.log("Connected to mongodb (Note: deprecation warning above is a bug)");
});
}