forked from hoodiehq/pouchdb-hoodie-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-all.js
61 lines (49 loc) · 1.32 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict'
var extend = require('pouchdb-extend')
var toObject = require('./utils/to-object')
var toDoc = require('./utils/to-doc')
var isntDesignDoc = require('./utils/isnt-design-doc')
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
var objects
if (type !== 'object' && type !== 'function') {
return Promise.reject(new Error('Must provide object or function'))
}
return this.allDocs({
include_docs: true
})
.then(function (res) {
objects = res.rows
.filter(isntDesignDoc)
.map(function (row) {
return toObject(row.doc)
})
if (type === 'function') {
objects.forEach(changedProperties)
return objects.map(toDoc)
}
return objects.map(function (object) {
extend(object, changedProperties)
return toDoc(object)
})
})
.then(function (result) {
return result
})
.then(this.bulkDocs.bind(this))
.then(function (results) {
return results.map(function (result, i) {
objects[i]._rev = result.rev
return objects[i]
})
})
}