-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircDataProvider.js
77 lines (69 loc) · 2.32 KB
/
ircDataProvider.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var Db = require('mongodb').Db
, Connection = require('mongodb').Connection
, Server = require('mongodb').Server
, BSON = require('mongodb').BSON
, ObjectID = require('mongodb').ObjectID
, moment = require('moment-timezone');
ircDataProvider = function(host, port, dbName, callback){
this.db = new Db(dbName, new Server(host, port, {auto_reconnect: true}, {}), {w: 1});
this.db.open(function(error, db) {
if(error) callback(error);
else callback(null);
});
};
ircDataProvider.prototype.getCollection = function(callback, collectionName) {
this.db.collection(collectionName, function(error, collectionData) {
if(error) callback(error);
else callback(null, collectionData);
});
};
ircDataProvider.prototype.find = function(callback, collectionName, criteria, limit) {
this.getCollection(function(error, collection) {
if(error) callback(error);
else {
if(criteria === undefined || criteria === null) {
criteria = {};
}
if(limit === undefined || limit === null || isNaN(limit)) {
limit = 0;
}
collection.find(criteria, function(error, cursor) {
if(error) callback(error);
else {
cursor.limit(limit).sort({"_id":-1}).toArray(function(error, data) {
if(error) callback(error);
else callback(null, data);
});
}
});
}
}, collectionName);
};
ircDataProvider.prototype.delete = function(callback, collectionName, id) {
this.getCollection(function(error, collection) {
if(error) callback(error);
else {
collection.remove({"_id" : new ObjectID(id)}, function(error, nRemoved) {
if(error) callback(error);
else callback(null, nRemoved);
});
}
}, collectionName);
};
ircDataProvider.prototype.update = function(callback, collectionName, criteria, updateData, options) {
this.getCollection(function(error, collection) {
if(error) callback(error);
else {
collection.update(criteria, updateData, options, callback);
}
}, collectionName);
};
ircDataProvider.prototype.insert = function(callback, collectionName, insertData) {
this.getCollection(function(error, collection) {
if(error) callback(error);
else {
collection.insert(insertData, callback);
}
}, collectionName);
};
exports.ircDataProvider = ircDataProvider;