-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
34 lines (27 loc) · 873 Bytes
/
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
import abstractMapper from '../abstract-mapper';
import { upsert } from 'pouchdb-utils';
function deleteIndex(db, index) {
if (!index.ddoc) {
throw new Error('you must supply an index.ddoc when deleting');
}
if (!index.name) {
throw new Error('you must supply an index.name when deleting');
}
var docId = index.ddoc;
var viewName = index.name;
function deltaFun(doc) {
if (Object.keys(doc.views).length === 1 && doc.views[viewName]) {
// only one view in this ddoc, delete the whole ddoc
return {_id: docId, _deleted: true};
}
// more than one view here, just remove the view
delete doc.views[viewName];
return doc;
}
return upsert(db, docId, deltaFun).then(function () {
return abstractMapper(db).viewCleanup.apply(db);
}).then(function () {
return {ok: true};
});
}
export default deleteIndex;