forked from hoodiehq/pouchdb-hoodie-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-all.js
47 lines (38 loc) · 925 Bytes
/
remove-all.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
'use strict'
var toObject = require('./utils/to-object')
var toDoc = require('./utils/to-doc')
module.exports = removeAll
/**
* removes all existing objects
*
* @param {Function} [filter] Function returning `true` for any object
* to be removed.
* @return {Promise}
*/
function removeAll (filter) {
var objects
var db = this
return db.allDocs({
include_docs: true
})
.then(function (res) {
objects = res.rows.map(function (row) {
return toObject(row.doc)
})
if (typeof filter === 'function') {
objects = objects.filter(filter)
}
return objects.map(function (object) {
var doc = toDoc(object)
doc._deleted = true
return doc
})
})
.then(db.bulkDocs.bind(db))
.then(function (results) {
return results.map(function (result, i) {
objects[i]._rev = result.rev
return objects[i]
})
})
}