-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove.js
42 lines (37 loc) · 1.29 KB
/
remove.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
/**
* Created by wbison on 04-05-15.
*/
// remove docs that are expired
module.exports = function(config) {
var purge = require('./purge')(config);
var map = require('./map')(config);
var nano = require('nano')(config);
var log = require('./log')(config);
return function(databaseName, cb) {
var db = nano.use(databaseName);
purge(db, done);
function done(e, res) {
// if err and it is view not found (404) the create view
if (e && e.statusCode === 404) {
// create view and rerun
log(db, "Creating design document '" + "_design/" + config._design_doc._id + "'");
config._design_doc.views._default = {
map: map.toString()
};
db.insert(config._design_doc, "_design/" + config._design_doc._id,
function(err) {
if (err) {
log(db, "Error: " + err.reason);
return cb(err);
}
purge(db, done);
});
return;
} else if(e) {
// need to report err
return cb(e);
}
cb(null, res);
}
}
}