-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (68 loc) · 2.76 KB
/
index.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
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory( require('couchr'));
} else if (typeof define === 'function' && define.amd) {
define(['couchr'],factory);
} else {
root.app_settings = factory(root.couchr);
}
}(this, function (couchr) {
var app_settings = {};
/**
* Clear any existing app settings property form a design doc
*/
app_settings.clear = function(ddoc_url, callback) {
couchr.get(ddoc_url, function(err, current_doc) {
if (err) return callback(err);
if (!current_doc.app_settings) return(null);
delete current_doc.app_settings;
couchr.put(ddoc_url, current_doc, callback);
});
};
/**
* copy app settings from one design doc to another design doc
*/
app_settings.copy = function(ddoc_url, ddoc_url_to, callback) {
couchr.get(ddoc_url, function(err, current_doc) {
if (err) return callback(err);
if (!current_doc.app_settings) return('No settings exist');
couchr.get(ddoc_url_to, function(err, target_doc){
if (err) return callback(err);
target_doc.app_settings = current_doc.app_settings;
couchr.put(ddoc_url_to, target_doc, callback);
});
});
}
/**
* Restore app_settings that have been cleared from the last revision of the doc that had app_settings
*/
app_settings.restore = function(ddoc_url, callback) {
couchr.get(ddoc_url, {revs_info: true}, function(err, resp) {
var _revs_info = resp._revs_info.slice(1); // eliminate the first one
var current_doc = resp;
delete current_doc._revs_info;
if (current_doc.app_settings) return callback('The current doc has existing app_settings. Please use clear first.');
app_settings.find_last_app_settings(ddoc_url, _revs_info, function(err, last_app_settings){
if (err) return callback(err);
if (!last_app_settings) return callback('No previous app_settings found');
current_doc.app_settings = last_app_settings;
couchr.put(ddoc_url, current_doc, callback);
});
});
}
/**
* Given a doc, and a list of its revisions, return the first revisions app_settings
*/
app_settings.find_last_app_settings = function(ddoc_url, revs_info, callback) {
if (revs_info.length === 0) return callback(null);
var rev_details = revs_info[0];
if (rev_details.status !== 'available') return find_last_app_settings(revs_info.slice(1), callback);
// get the doc
couchr.get(ddoc_url, {rev: rev_details.rev}, function(err, rev_doc){
if (err) return callback(err);
if (rev_doc.app_settings) return callback(null, rev_doc.app_settings);
return find_last_app_settings(revs_info.slice(1), callback);
})
}
return app_settings;
}));