forked from hoodiehq/pouchdb-hoodie-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-all.js
44 lines (33 loc) · 935 Bytes
/
update-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
'use strict'
var extend = require('pouchdb-extend')
module.exports = updateAll
/**
* updates all existing objects
*
* @param {Object|Function} change changed properties or function that
* alters passed object
* @return {Promise}
*/
function updateAll (changedProperties) {
var Promise = this.constructor.utils.Promise
var type = typeof changedProperties
if (type !== 'object' && type !== 'function') {
return Promise.reject(new Error('Must provide object or function'))
}
return this.allDocs({
include_docs: true
})
.then(function (res) {
var docs = res.rows.map(function (row) {
return row.doc
})
if (type === 'function') return docs.map(changedProperties)
return docs.map(function (doc) {
return extend(doc, changedProperties)
})
})
.then(this.bulkDocs.bind(this))
.then(function (results) {
return results
})
}