This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MatchProvider-mongodb.js
56 lines (47 loc) · 1.61 KB
/
MatchProvider-mongodb.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
var Db = require('mongodb').Db,
Server = require('mongodb').Server;
MatchProvider = function(host, port) {
this.db= new Db('matchurls', new Server(host, port, {auto_reconnect: true}), {w: 0});
this.db.open(function(){});
};
MatchProvider.prototype.getCollection = function(callback) {
this.db.collection('matches', function(error, matchCollection) {
if( error ) callback(error);
else callback(null, matchCollection);
});
};
MatchProvider.prototype.findAll = function(callback) {
this.getCollection(function(error, matchCollection) {
if( error ) callback(error);
else {
matchCollection.find().toArray(function(error, results) {
if( error ) callback(error);
else callback(null, results);
});
}
});
};
MatchProvider.prototype.findByMatchId = function(matchId, callback) {
this.getCollection(function(error, matchCollection) {
if( error ) callback(error);
else {
matchCollection.findOne({id: matchId}, function(error, result) {
if( error ) callback(error);
else callback(null, result);
});
}
});
};
MatchProvider.prototype.save = function(matches, callback) {
this.getCollection(function(error, matchCollection) {
if( error ) callback(error);
else {
if( typeof(matches.length)=="undefined")
matches = [matches];
matchCollection.insert(matches, function() {
callback(null, matches);
});
}
});
};
exports.MatchProvider = MatchProvider;