'use strict'
const commit = require('../operations/commit')
const { DB_OPS: { REMOVE } } = require('../constants')
const { pick, omit, isObject } = require('lodash')
const { JoiRG, validate, checkValidation } = require('../routes/helpers')
const { REMOVE_BODY_SCHEMA } = require('../routes/schemas')
const shallowOptKeys = ['returnNew', 'returnOld', 'silent']
const optionsSchema = JoiRG.object().keys({
returnOld: JoiRG.boolean(),
silent: JoiRG.boolean(),
ignoreRevs: JoiRG.boolean()
})
const providerSchemas = [JoiRG.string().collection().required(), REMOVE_BODY_SCHEMA, optionsSchema]
function removeSingle ({ pathParams, body }, options, deepOpts) {
let shallowOpts
if (!isObject(deepOpts)) {
shallowOpts = pick(options, shallowOptKeys)
deepOpts = omit(options, shallowOptKeys)
} else {
shallowOpts = options
}
return commit(
pathParams.collection,
body,
REMOVE,
shallowOpts,
deepOpts
)
}
function removeMultiple ({ pathParams, body }, options) {
const shallowOpts = pick(options, shallowOptKeys)
const deepOpts = omit(options, shallowOptKeys)
const nodes = []
body.forEach(node => {
try {
nodes.push(
removeSingle({ pathParams, body: node }, shallowOpts, deepOpts)
)
} catch (e) {
console.error(e.stack)
nodes.push(e)
}
})
return nodes
}
removeProvider
Removes an existing document or documents and updates the tracking index.
Args:
collection
- The collection from which to remove the document.data
- An object or array of objects containing meta information of documents to be removed.options
- An optional object, containing any combination of the following keys:returnOld
- Whether to return the old object. Defaultfalse
.silent
- Whether to return anything in the result. Defaultfalse
.ignoreRevs
- Whether to ignore a revision match before delete. Defaulttrue
.Return:
The contents of the result returned by the method is identical to the contents of the response body of the corresponding HTTP API, invoked with identical input, except when the method throws an error. In the latter case, the error message would be identical to the error response of the HTTP call.
Errors:
If
data
is a single object, then any error that occurs while executing the method is thrown back to the caller.If, on the other hand,
data
is an array of objects, then the method always returns an array of results. For every element of thedata
array which incurs an error, the error object is present at the corresponding index in the result array. For every input element that the method handles successfully, the result array contains an element that is identical to the result that would have been returned had the method been invoked singly for this element, with identical options.Examples: