-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataProvider-mongodb.js
More file actions
123 lines (113 loc) · 2.92 KB
/
Copy pathdataProvider-mongodb.js
File metadata and controls
123 lines (113 loc) · 2.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Retrieve
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
BSON = require('mongodb').BSONPure,
Code = require('mongodb').Code,
mongo = require('mongodb');
DataProvider = function(host, port) {
dataTable = 'dataTable';
db = null;
that = this;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/data", function(err, db) {
if(!err) {
console.log("We are connected");
db.createCollection(dataTable, function(err, collection) {
});
that.db = db;
}
});
this.fetchAllData = function(cb) {
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
data.find().toArray(function(error, results) {
cb(error, results);
});
}
});
};
this.fetchDataById = function(id, cb) {
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
data.findOne({
_id:new BSON.ObjectID(id)
}, function(error, result) {
cb(error, result);
});
}
});
};
this.fetchDataLast = function(last, cb) {
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
console.log(last);
data.find().sort({time: -1}).limit(parseInt(last)).toArray(function(error, result) {
cb(error, result.reverse());
});
}
});
}
this.fetchDataAfterTime = function(last, cb) {
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
data.find({time: {$gt:new mongo.Double(last)}}).toArray(function(error, result) {
cb(error, result);
});
}
});
}
this.insertData = function(dataEl, cb) {
dataEl._id = new BSON.ObjectID();
console.log('Inserting:' + JSON.stringify(dataEl));
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
data.insert([dataEl], function() {
cb(null, dataEl);
});
}
});
};
this.updateData = function(dataEl, cb) {
console.log('Updating:' + JSON.stringify(dataEl));
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
data.update({_id:new BSON.ObjectID(dataEl._id)},
{time:dataEl.time, data:dataEl.data},
function(error, result) {
cb(error, result);
});
}
});
};
this.deleteData = function(id, cb) {
console.log('Deleting id of:' + id);
this.db.collection(dataTable, function(error, data) {
if (error) {
cb(error, null);
} else {
data.remove({_id:new BSON.ObjectID(id)},
function(error, result) {
cb(error, result);
});
}
});
};
};
exports.DataProvider = DataProvider;