This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.js
108 lines (93 loc) · 2.23 KB
/
query.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
var mongo = require("mongodb"),
async = require("async"),
Tokenizer = require('./tokenizer.addon'),
Statement = Tokenizer.Statement,
StatementCollection = Tokenizer.StatementCollection,
MONGODB_URL;
// Initial connection setup.
if (process.env.MONGOHQ_URL) {
MONGODB_URL = process.env.MONGOHQ_URL;
} else {
MONGODB_URL = "mongodb://localhost:27017/degreeVU";
}
var db = { // Dummy object while we wait for a connection.
collection: function(name, cb) {
cb('A database connection has not been established. Try again.');
}
};
mongo.Db.connect(MONGODB_URL, function(err, dbPool) {
if (err) {
console.log("Couldn't establish a database connection. Exiting...");
process.exit(1);
}
db = dbPool;
});
// Factory function to get a collection. Can be used directly in async.waterfall.
var getCollection = function(name) {
return function(cb) {
db.collection(name, cb);
}
}
////// Query functions //////
var getCoursesFromTokens = function(tokens, callback) {
var collection = new StatementCollection(tokens),
mongoQuery = collection.mongoQuery();
if (mongoQuery) {
async.waterfall([
getCollection('courses'),
function(collection, cb) {
collection.find(mongoQuery, cb);
},
function(cursor, cb) {
cursor.toArray(cb);
}
],
callback);
} else {
callback(null, []);
}
}
var getCourseByKey = function(key, callback) {
async.waterfall([
getCollection('courses'),
function(collection, cb) {
try {
var id = new mongo.ObjectID(key);
collection.findOne({ _id: id }, cb);
} catch (e) {
cb(null, null);
}
}
],
callback);
};
var getGoalByKey = function(key, callback) {
async.waterfall([
getCollection('goals'),
function(collection, cb) {
try {
var id = new mongo.ObjectID(key);
collection.findOne({ _id: id }, cb);
} catch (e) {
cb(null, null);
}
}
],
callback);
};
var listMajors = function(callback) {
async.waterfall([
getCollection('goals'),
function(collection, cb) {
collection.find({ type: 'major' }, { items: 0 }, cb);
},
function(cursor, cb) {
cursor.toArray(cb);
}
],
callback);
};
exports.getCoursesFromTokens = getCoursesFromTokens;
exports.getCourseByKey = getCourseByKey;
exports.getGoalByKey = getGoalByKey;
exports.listMajors = listMajors;