handleSpreadDoc.js 847 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const utils = require('../../utils');
  3. const keysToSkip = new Set(['__index', '__parentArray', '_doc']);
  4. /**
  5. * Using spread operator on a Mongoose document gives you a
  6. * POJO that has a tendency to cause infinite recursion. So
  7. * we use this function on `set()` to prevent that.
  8. */
  9. module.exports = function handleSpreadDoc(v, includeExtraKeys) {
  10. if (utils.isPOJO(v) && v.$__ != null && v._doc != null) {
  11. if (includeExtraKeys) {
  12. const extraKeys = {};
  13. for (const key of Object.keys(v)) {
  14. if (typeof key === 'symbol') {
  15. continue;
  16. }
  17. if (key[0] === '$') {
  18. continue;
  19. }
  20. if (keysToSkip.has(key)) {
  21. continue;
  22. }
  23. extraKeys[key] = v[key];
  24. }
  25. return { ...v._doc, ...extraKeys };
  26. }
  27. return v._doc;
  28. }
  29. return v;
  30. };