Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

applyQueryMiddleware.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. module.exports = applyQueryMiddleware;
  6. /*!
  7. * ignore
  8. */
  9. applyQueryMiddleware.middlewareFunctions = [
  10. 'count',
  11. 'countDocuments',
  12. 'deleteMany',
  13. 'deleteOne',
  14. 'estimatedDocumentCount',
  15. 'find',
  16. 'findOne',
  17. 'findOneAndDelete',
  18. 'findOneAndRemove',
  19. 'findOneAndReplace',
  20. 'findOneAndUpdate',
  21. 'remove',
  22. 'replaceOne',
  23. 'update',
  24. 'updateMany',
  25. 'updateOne'
  26. ];
  27. /*!
  28. * Apply query middleware
  29. *
  30. * @param {Query} query constructor
  31. * @param {Model} model
  32. */
  33. function applyQueryMiddleware(Query, model) {
  34. const kareemOptions = {
  35. useErrorHandlers: true,
  36. numCallbackParams: 1,
  37. nullResultByDefault: true
  38. };
  39. const middleware = model.hooks.filter(hook => {
  40. if (hook.name === 'updateOne') {
  41. return hook.query == null || !!hook.query;
  42. }
  43. if (hook.name === 'remove') {
  44. return !!hook.query;
  45. }
  46. return true;
  47. });
  48. // `update()` thunk has a different name because `_update` was already taken
  49. Query.prototype._execUpdate = middleware.createWrapper('update',
  50. Query.prototype._execUpdate, null, kareemOptions);
  51. applyQueryMiddleware.middlewareFunctions.
  52. filter(v => v !== 'update').
  53. forEach(fn => {
  54. Query.prototype[`_${fn}`] = middleware.createWrapper(fn,
  55. Query.prototype[`_${fn}`], null, kareemOptions);
  56. });
  57. }