-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.js
192 lines (161 loc) · 5.92 KB
/
getData.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var mongo = require('mongodb');
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server,
BSON = require('mongodb').BSONNative;
// Get the objectID type
var ObjectID = require('mongodb').ObjectID;
var fs = require('fs');
//var dbname = 'plate';
var host = GLOBAL.mongoHost;
var port = GLOBAL.mongoPort;
var dbname = GLOBAL.mongoDBName;
//var localstr = "mongodb://mongo:[email protected]:33757/heroku_app5057630";
var localstr = "mongodb://localhost:27017/plate";
//var localstr = GLOBAL.mongoConnString;
// ### INTERNAL WORKER METHODS
//Generic function to fetch an object by its ObjectId
var getObjectById = function(objectId, collection, nextFn) {
console.log("in fetchObjectById for objectID " + objectId + " and collection: " + collection);
var queryParams = {};
queryParams['_id'] = new ObjectID(objectId);
runQuery(collection, queryParams, null, function(err, results) {
console.log('object retrieved was: ' + JSON.stringify(results));
nextFn(err, results);
});
}
// //Generic function to fetch objects based on queryParams and collection
// var getObjectsByQueryParams = function (queryParams, collection, nextFn) {
// getObjectsByQueryParams(queryParams, null, collection, nextFn);
// // runQuery(collection, queryParams, null, function(err, results) {
// // if (err) nextFn(err, null);
// // console.log("in getObjectsByQueryParams for coll: " + collection + ", the results jsonstringified are " + JSON.stringify(results));
// // nextFn(err, results);
// // });
// };
//Generic function to fetch objects based on queryParams and collection and options
var getObjectsByQueryParams = function (queryParams, options, collection, nextFn) {
runQuery(collection, queryParams, options, function(err, results) {
if (err) nextFn(err, null);
console.log("in getObjectsByQueryParams for coll: " + collection + ", the results jsonstringified are " + JSON.stringify(results));
nextFn(err, results);
});
};
//Generic worker function to run a find query
var runQuery = function(myCollection, query, options, nextFn) {
// perform the {query} on the collection and invoke the nextFn when done
// var db = new Db(dbname, new Server(host, port, {}), {native_parser:false});
mongo.connect(localstr, {}, function(error, db){
db.collection(myCollection, function(err, collection) {
var optionsArray = {};
//if (typeof(options) != 'undefined') {
// optionsArray = options;
if (options) {
optionsArray = options;
} else {
optionsArray['limit'] = 100;
optionsArray['sort'] = {};
optionsArray['sort']['_id']= -1;
}
optionsArray['slaveOk'] = true;
collection.find(query, optionsArray, function (err, cursor){
if (err) {
console.log("error is: " + err);
nextFn(err, null);
}
cursor.toArray(function(err, docs) {
db.close();
if (err || (docs.length == 0)) {
nextFn(err, null);
} else {
console.log("found " + docs.length + " documents in " + myCollection);
var queryResults = [];
for(var i=0; i<docs.length; i++) {
queryResults[queryResults.length] = docs[i];
}
nextFn(err, queryResults);
}
});
});
});
});
}
var createDocument = function (newDoc, collection, nextFn) {
// perform the {insert} on the collection and invoke the nextFn when done
mongo.connect(localstr, {}, function(error, client){
client.collection(collection, function(err, col) {
col.insert(newDoc, {safe:true}, function (err, result) {
if (err) console.warn(err.message);
console.log("Created " + collection + ": insert results are " + JSON.stringify(result));
nextFn(err, result);
});
});
});
}
// ## END OF INTERNAL WORKER FUNCTIONS
// ## BEGIN EXPORTED FUNCTIONS
// Insert user into Mongo
exports.createUser = function(newUser, nextFn) {
createDocument(newUser, 'plateUser', nextFn);
}
// Insert order into Mongo
exports.createOrder = function(newOrder, nextFn) {
createDocument(newOrder, 'order', nextFn);
}
// Insert menu item into Mongo
exports.addMenuItem = function(menuitem, nextFn) {
createDocument(menuitem, 'menuitem', nextFn);
}
//Fetch a menu item by its id
exports.getMenuItem = function(menuItemId, nextFn) {
getObjectById(menuItemId, 'menuitem', nextFn);
}
//Fetch all menu items; flag for getting unavailable ones; default false
exports.getAllMenuItems = function(includeUnavailableItems, type, nextFn) {
var queryParams = {};
if (!includeUnavailableItems) {
queryParams['disc'] = {};
queryParams['disc']['$ne'] = "yes";
}
if (type) {
queryParams['type'] = type;
}
var optionsArray = {};
optionsArray['sort'] = {};
optionsArray['sort']['type']= 1;
optionsArray['sort']['veg']= 1;
getObjectsByQueryParams(queryParams, optionsArray, 'menuitem', nextFn);
}
//Fetch the menu for the passed in day
exports.getMenu = function(day, nextFn) {
var queryParams = {};
queryParams['day'] = day;
getObjectsByQueryParams(queryParams, null, 'menu', nextFn);
}
//Fetch the order history of this userId
exports.getOrderHistory = function(userId, nextFn) {
var queryParams = {};
queryParams['userid'] = userId;
getObjectsByQueryParams(queryParams, null, 'order', nextFn);
}
//Get the users by this userLogin
exports.fetchUsersByLogin = function(userLogin, nextFn) {
var queryParams = {};
queryParams['login'] = userLogin;
getObjectsByQueryParams(queryParams, null, 'plateUser', nextFn);
}
//Fetch all users
exports.getAllUsers = function(nextFn) {
var queryParams;
getObjectsByQueryParams(queryParams, null, 'plateUser', nextFn);
}
//Fetch a user by userid
exports.findUserById = function(userId, nextFn) {
getObjectById(userId, 'plateUser', nextFn);
}
//Fetch a user by userid
exports.findUserByEmail = function(login, nextFn) {
var queryParams = {};
queryParams['email'] = login;
getObjectsByQueryParams(queryParams, null, 'plateUser', nextFn);
}