utilities/handler-helper.js
async function _addOneHandler(
  ownerModel,
  ownerId,
  childModel,
  childId,
  associationName,
  request,
  Log
) {
  try {
    let ownerObject = await ownerModel
      .findOne({ _id: ownerId })
      .select(associationName)
    let payload = Object.assign({}, request.payload)
    if (ownerObject) {
      if (!payload) {
        payload = {}
      }
      payload.childId = childId
      payload = [payload]

      try {
        if (
          ownerModel.routeOptions &&
          ownerModel.routeOptions.add &&
          ownerModel.routeOptions.add[associationName] &&
          ownerModel.routeOptions.add[associationName].pre
        ) {
          payload = await ownerModel.routeOptions.add[associationName].pre(
            payload,
            request,
            Log
          )
        }
      } catch (err) {
        handleError(
          err,
          'There was a preprocessing error while setting the association.',
          Boom.badRequest,
          Log
        )
      }

      try {
        await _setAssociation(
          ownerModel,
          ownerObject,
          childModel,
          childId,
          associationName,
          payload,
          Log
        )
      } catch (err) {
        handleError(
          err,
          'There was a database error while setting the association.',
          Boom.badImplementation,
          Log
        )
      }
      return true
    } else {
      throw Boom.notFound('No resource was found with that id.')
    }
  } catch (err) {
    handleError(err, null, null, Log)
  }
}
async function _addOneHandler(
  ownerModel,
  ownerId,
  childModel,
  childId,
  associationName,
  request,
  Log
) {
  try {
    let ownerObject = await ownerModel
      .findOne({ _id: ownerId })
      .select(associationName)
    let payload = Object.assign({}, request.payload)
    if (ownerObject) {
      if (!payload) {
        payload = {}
      }
      payload.childId = childId
      payload = [payload]

      try {
        if (
          ownerModel.routeOptions &&
          ownerModel.routeOptions.add &&
          ownerModel.routeOptions.add[associationName] &&
          ownerModel.routeOptions.add[associationName].pre
        ) {
          payload = await ownerModel.routeOptions.add[associationName].pre(
            payload,
            request,
            Log
          )
        }
      } catch (err) {
        handleError(
          err,
          'There was a preprocessing error while setting the association.',
          Boom.badRequest,
          Log
        )
      }

      try {
        await _setAssociation(
          ownerModel,
          ownerObject,
          childModel,
          childId,
          associationName,
          payload,
          Log
        )
      } catch (err) {
        handleError(
          err,
          'There was a database error while setting the association.',
          Boom.badImplementation,
          Log
        )
      }
      return true
    } else {
      throw Boom.notFound('No resource was found with that id.')
    }
  } catch (err) {
    handleError(err, null, null, Log)
  }
}
async function _addOneHandler ( ownerModel , ownerId , childModel , childId , associationName , request , Log ) { try { let ownerObject = await ownerModel . findOne ( { _id : ownerId } ) . select ( associationName ) let payload = Object . assign ( { } , request . payload ) if ( ownerObject ) { if ( ! payload ) { payload = { } } payload . childId = childId payload = [ payload ] try { if ( ownerModel . routeOptions && ownerModel . routeOptions . add && ownerModel . routeOptions . add [ associationName ] && ownerModel . routeOptions . add [ associationName ] . pre ) { payload = await ownerModel . routeOptions . add [ associationName ] . pre ( payload , request , Log ) } } catch ( err ) { handleError ( err , 'There was a preprocessing error while setting the association.' , Boom . badRequest , Log ) } try { await _setAssociation ( ownerModel , ownerObject , childModel , childId , associationName , payload , Log ) } catch ( err ) { handleError ( err , 'There was a database error while setting the association.' , Boom . badImplementation , Log ) } return true } else { throw Boom . notFound ( 'No resource was found with that id.' ) } } catch ( err ) { handleError ( err , null , null , Log ) } }
Adds an association to a document
@param ownerModel: The model that is being added to.
@param ownerId: The id of the owner document.
@param childModel: The model that is being added.
@param childId: The id of the child document.
@param associationName: The name of the association from the ownerModel's perspective.
@param request: The Hapi request object, or a container for the wrapper payload.
@param Log: A logging object
@returns {object} A promise returning true if the add succeeds.
@private
Adds an association to a document