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.

applyMethods.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const get = require('../get');
  3. /*!
  4. * Register methods for this model
  5. *
  6. * @param {Model} model
  7. * @param {Schema} schema
  8. */
  9. module.exports = function applyMethods(model, schema) {
  10. function apply(method, schema) {
  11. Object.defineProperty(model.prototype, method, {
  12. get: function() {
  13. const h = {};
  14. for (const k in schema.methods[method]) {
  15. h[k] = schema.methods[method][k].bind(this);
  16. }
  17. return h;
  18. },
  19. configurable: true
  20. });
  21. }
  22. for (const method of Object.keys(schema.methods)) {
  23. const fn = schema.methods[method];
  24. if (schema.tree.hasOwnProperty(method)) {
  25. throw new Error('You have a method and a property in your schema both ' +
  26. 'named "' + method + '"');
  27. }
  28. if (schema.reserved[method] &&
  29. !get(schema, `methodOptions.${method}.suppressWarning`, false)) {
  30. console.warn(`mongoose: the method name "${method}" is used by mongoose ` +
  31. 'internally, overwriting it may cause bugs. If you\'re sure you know ' +
  32. 'what you\'re doing, you can suppress this error by using ' +
  33. `\`schema.method('${method}', fn, { suppressWarning: true })\`.`);
  34. }
  35. if (typeof fn === 'function') {
  36. model.prototype[method] = fn;
  37. } else {
  38. apply(method, schema);
  39. }
  40. }
  41. // Recursively call `applyMethods()` on child schemas
  42. model.$appliedMethods = true;
  43. for (const key of Object.keys(schema.paths)) {
  44. const type = schema.paths[key];
  45. if (type.$isSingleNested && !type.caster.$appliedMethods) {
  46. applyMethods(type.caster, type.schema);
  47. }
  48. if (type.$isMongooseDocumentArray && !type.Constructor.$appliedMethods) {
  49. applyMethods(type.Constructor, type.schema);
  50. }
  51. }
  52. };