Skip to content

Commit

Permalink
feat: allow update before delete
Browse files Browse the repository at this point in the history
  • Loading branch information
luoto committed Aug 25, 2015
1 parent 7600c4b commit c6023ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions remove.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

var markAsDeleted = require('./utils/mark-as-deleted')

var updateOne = require('./helpers/update-one')
var updateMany = require('./helpers/update-many')

Expand All @@ -8,12 +10,13 @@ module.exports = remove
/**
* removes existing object
*
* @param {Object|Function} change changed properties or function that
* alters passed object
* @param {Object|Function} objectsOrIds id or object with `.id` property
* @param {Object|Function} [change] Change properties or function that
* changes existing object
* @return {Promise}
*/
function remove (objectsOrIds) {
function remove (objectsOrIds, change) {
return Array.isArray(objectsOrIds) ?
updateMany.call(this, objectsOrIds, {_deleted: true}) :
updateOne.call(this, objectsOrIds, {_deleted: true})
updateMany.call(this, objectsOrIds.map(markAsDeleted.bind(null, change))) :
updateOne.call(this, markAsDeleted(change, objectsOrIds))
}
15 changes: 15 additions & 0 deletions utils/mark-as-deleted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

var extend = require('pouchdb-extend')
var changeObject = require('./change-object')

// Normalizes objectOrId, applies changes if any, and mark as deleted
module.exports = function markAsDeleted (change, objectOrId) {
var object = typeof objectOrId === 'string' ? { id: objectOrId } : objectOrId

if (change) {
changeObject(change, object)
}

return extend({_deleted: true}, object)
}

0 comments on commit c6023ec

Please sign in to comment.