-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectionDriver.js
103 lines (94 loc) · 3.22 KB
/
collectionDriver.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
var ObjectID = require('mongodb').ObjectID;
CollectionDriver = function(db) {
this.db = db;
};
CollectionDriver.prototype.getCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else callback(null, the_collection);
});
};
//find all objects for a collection
CollectionDriver.prototype.findAll = function(collectionName, callback) {
this.getCollection(collectionName, function(error, the_collection) { //A
if( error ) callback(error)
else {
the_collection.find().toArray(function(error, results) { //B
if( error ) callback(error)
else callback(null, results)
});
}
});
};
//find a specific object
CollectionDriver.prototype.get = function(collectionName, id, callback) { //A
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error)
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) callback({error: "invalid id"});
else the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) { //C
if (error) callback(error)
else callback(null, doc);
});
}
});
}
//save new object
CollectionDriver.prototype.save = function(collectionName, obj, callback) {
this.getCollection(collectionName, function(error, the_collection) { //A
if( error ) callback(error)
else {
obj.created_at = new Date(); //B
the_collection.insert(obj, function() { //C
callback(null, obj);
});
}
});
};
//update a specific object
CollectionDriver.prototype.update = function(collectionName, obj, entityId, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error)
else {
obj._id = ObjectID(entityId); //A convert to a real obj id
obj.updated_at = new Date(); //B
the_collection.save(obj, function(error,doc) { //C
if (error) callback(error)
else callback(null, obj);
});
}
});
}
//delete a specific object
CollectionDriver.prototype.delete = function(collectionName, entityId, callback) {
console.log("initializing delete");
this.getCollection(collectionName, function(error, the_collection) { //A
if (error) callback(error)
else {
console.log("pre removal");
the_collection.remove({'_id':ObjectID(entityId)}, function(error,doc) { //B
if (error){
console.log("MHEIRENDT_ERROR");
callback(error);
}else{
console.log("MHEIRENDT_CALLBACK");
callback(null, doc);
}
});
}
});
}
//Perform a collection query
CollectionDriver.prototype.query = function(collectionName, query, callback) { //1
this.getCollection(collectionName, function(error, the_collection) { //2
if( error ) callback(error)
else {
the_collection.find(query).toArray(function(error, results) { //3
if( error ) callback(error)
else callback(null, results)
});
}
});
};
exports.CollectionDriver = CollectionDriver;