-
Notifications
You must be signed in to change notification settings - Fork 2
/
permalink.js
47 lines (41 loc) · 1.15 KB
/
permalink.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
var permalink = {};
// import config
var config = require('./config');
var mongo = require('mongoskin');
// setup MongoDB
var db = mongo.db(config.mongohost);
db.bind('permalinks');
permalink.listPermalinks = function(done) {
db.permalinks.find().toArray(function(err, found) {
if (err) {
done(null, err);
} else {
done(found);
}
});
}
permalink.getPermalink = function(pid, deliver) {
console.log(pid);
db.permalinks.findById(pid, function(err, found) {
console.log("found prev permalink?", found, err);
if (err || !found) {
deliver(null, err);
} else {
console.log("returning", found);
deliver(found.query);
}
});
}
permalink.savePermalink = function(query, deliver) {
db.permalinks.findOne({query: query}, function(err, found) {
console.log("found one?", found);
if (found) {
deliver(found._id, null);
} else {
db.permalinks.insert({query: query}, function(err, result) {
deliver(result[0]._id, err);
});
}
});
}
module.exports = permalink;