-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfhprovider.js
60 lines (49 loc) · 1.51 KB
/
dfhprovider.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
54
55
56
57
58
59
60
var mongo = require('mongodb').MongoClient;
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/helperapp';
var database = null;
DFHProvider = function() {
mongo.connect(mongoUri, {}, function(error, db) {
database = db;
database.addListener("error", function(error){
console.log("Error connecting to MongoLab");
});
});
};
DFHProvider.prototype.getCollection= function(callback) {
database.collection('usercollection', function(error, helper_collection) {
if( error ) callback(error);
else callback(null, helper_collection);
});
};
//find all helpers
DFHProvider.prototype.findAll = function(callback) {
this.getCollection(function(error, helper_collection) {
if( error ) callback(error)
else {
helper_collection.find().toArray(function(error, results) {
if( error ) callback(error)
else callback(null, results)
});
}
});
};
//save new helper
DFHProvider.prototype.save = function(helpers, callback) {
this.getCollection(function(error, helper_collection) {
if( error ) callback(error)
else {
if( typeof(helpers.length)=="undefined")
helpers = [helpers];
for( var i =0;i< helpers.length;i++ ) {
helper = helpers[i];
helper.created_at = new Date();
}
helper_collection.insert(helpers, function() {
callback(null, helpers);
});
}
});
};
exports.DFHProvider = DFHProvider;