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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. 'distinct',
  15. 'estimatedDocumentCount',
  16. 'find',
  17. 'findOne',
  18. 'findOneAndDelete',
  19. 'findOneAndRemove',
  20. 'findOneAndReplace',
  21. 'findOneAndUpdate',
  22. 'remove',
  23. 'replaceOne',
  24. 'update',
  25. 'updateMany',
  26. 'updateOne'
  27. ];
  28. /*!
  29. * Apply query middleware
  30. *
  31. * @param {Query} query constructor
  32. * @param {Model} model
  33. */
  34. function applyQueryMiddleware(Query, model) {
  35. const kareemOptions = {
  36. useErrorHandlers: true,
  37. numCallbackParams: 1,
  38. nullResultByDefault: true
  39. };
  40. const middleware = model.hooks.filter(hook => {
  41. if (hook.name === 'updateOne' || hook.name === 'deleteOne') {
  42. return hook.query == null || !!hook.query;
  43. }
  44. if (hook.name === 'remove') {
  45. return !!hook.query;
  46. }
  47. return true;
  48. });
  49. // `update()` thunk has a different name because `_update` was already taken
  50. Query.prototype._execUpdate = middleware.createWrapper('update',
  51. Query.prototype._execUpdate, null, kareemOptions);
  52. // `distinct()` thunk has a different name because `_distinct` was already taken
  53. Query.prototype.__distinct = middleware.createWrapper('distinct',
  54. Query.prototype.__distinct, null, kareemOptions);
  55. applyQueryMiddleware.middlewareFunctions.
  56. filter(v => v !== 'update' && v !== 'distinct').
  57. forEach(fn => {
  58. Query.prototype[`_${fn}`] = middleware.createWrapper(fn,
  59. Query.prototype[`_${fn}`], null, kareemOptions);
  60. });
  61. }