-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
151 lines (105 loc) · 2.97 KB
/
db.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
DB = function(host, port) {
this.db= new Db('tst', new Server(host, port, {safe: false}, {auto_reconnect: true}, {}));
this.db.open(function(){});
};
DB.prototype.getCollection= function(coll_name,callback) {
this.db.collection(coll_name, function(error, collection) {
if( error ) callback(error);
else callback(null, collection);
});
};
DB.prototype.printCollection= function(coll_name,callback) {
this.db.collection(coll_name, function(error, collection) {
if( error ) callback(error);
else collection.count(function(err,cnt){
console.log(cnt);
})
});
};
//find all coll_items
DB.prototype.findAll = function(callback) {
this.getCollection('coll_items',function(error, collection) {
if( error ) callback(error)
else {
collection.find().toArray(function(error, results) {
if( error ) callback(error)
else callback(null, results)
});
}
});
};
//find all coll_items
DB.prototype.find = function(coll_name, query, callback) {
this.getCollection(coll_name,function(error, collection) {
if( error ) callback(error)
else {
collection.find(query).toArray(function(error, results) {
if( error )
callback(error)
else
callback(null, results)
});
}
});
};
//save new message
DB.prototype.saveLocalNet = function(data, callback) {
this.getCollection("localNets",function(error, collection) {
data.epoch = new Date();
data.active = true;
var localNet = data;
if( error ) callback(error)
else {
var name = data.name;
var coords = data.location.coordinates;
collection.update(
{
name: name,
"location:coordinates" : coords
},
{ $set: localNet },
{ safe:true, upsert: true, new: 1 },
function(err,num){
if(!err)
callback( null, collection );
else callback(err)
});
}
});
};
//save new message
DB.prototype.save = function(coll_name, coll_items, callback) {
this.getCollection(coll_name,function(error, collection) {
if( error ) callback(error)
else {
if( typeof(coll_items.length)=="undefined")
coll_items = [coll_items];
for( var i =0;i< coll_items.length;i++ ) {
coll_item = coll_items[i];
coll_item.created_at = new Date();
}
collection.insert(coll_items, function() {
callback(null, collection);
});
}
});
};
DB.prototype.clear = function(coll_name, callback) {
this.getCollection(coll_name,function(error, collection) {
if (error)
callback(error)
else {
collection.remove();
console.log( " cleared DB : " + coll_name );
callback();
}
})
}
DB.prototype.clearUsers = function(callback) {
}
exports.DB = DB;