-
Notifications
You must be signed in to change notification settings - Fork 1
/
collectionUtils.js
129 lines (117 loc) · 4.14 KB
/
collectionUtils.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
/**
* I've freely borrowed code from
* http://www.raywenderlich.com/61078/write-simple-node-jsmongodb-web-service-ios-app
*/
var ObjectID = require('mongodb').ObjectID;
CollectionUtils = function(db) {
this.db = db;
};
CollectionUtils.prototype.createCollection = function(collectionName, callback) {
this.db.createCollection(collectionName, function(error, result) {
callback(error, result);
});
};
CollectionUtils.prototype.getCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
callback(error, the_collection);
});
};
//Find all objects for a collection
CollectionUtils.prototype.findAll = function(collectionName, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if( error ) callback(error)
else {
the_collection.find().toArray(function(error, results) {
callback(error, results)
});
}
});
};
//Return a collection matching general search criteria
CollectionUtils.prototype.searchFor = function(collectionName, keyName, searchString, callback) {
this.getCollection(collectionName, function(error, the_collection) {
var searchObj=new Object;
searchObj[keyName]=searchString;
console.log("Search Object",searchObj,keyName,searchString);
if( error ) callback(error)
else {
the_collection.find(searchObj).toArray(function(error, results) {
callback(error, results)
});
}
});
};
//Find a specific object
CollectionUtils.prototype.getById = function(collectionName, id, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error)
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
if (!checkForHexRegExp.test(id)) callback({error: "invalid id"});
else the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) {
if (error) callback(error)
else callback(null, doc);
});
}
});
}
//Save new object
//Note that .insert method is very strict on JSON formatting.
CollectionUtils.prototype.save = function(collectionName, obj, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if( error ) callback(error)
else {
obj.creationTime = (new Date()).toString();
obj.status="New";
the_collection.insert(obj, function() {
callback(null, obj);
});
}
});
};
//Update a specific object
CollectionUtils.prototype.update = function(collectionName, entityId, obj, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error)
else {
//Find the original object. We'll retain the creationTime value but
//discard everything else.
the_collection.findOne({'_id':ObjectID(entityId)},function(error, doc){
if(error) {
console.log(error);
}
else {
obj._id = ObjectID(entityId); //Convert to a real obj id
obj.creationTime=doc.creationTime;
//TODO: this is saved in Mongo as an ISODate object, which is not
//easily readable. The native JS date is in an easier format, so
//see if we can preseve this.
obj.updateTime = (new Date()).toString();
the_collection.save(obj, function(error,doc) {
if (error) callback(error)
else callback(null, obj);
});
}
});
}
});
}
//Delete a specific object
CollectionUtils.prototype.delete = function(collectionName, entityId, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) {
console.error("Could not get the collection:",error);
callback(error);
}
else {
the_collection.remove({'_id':ObjectID(entityId)}, function(error,doc) {
if (error) {
console.error("Could not remove the document from the collection", error);
callback(error);
}
else callback(null, doc);
});
}
});
}
exports.CollectionUtils = CollectionUtils;