setParentPointers.js 955 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. /*!
  3. * Set `$parentSchema` on all schema types, and `$schemaType` on single
  4. * nested docs.
  5. *
  6. * This is a slow path function, should only run when model is compiled
  7. */
  8. module.exports = function setParentPointers(schema, skipRecursion) {
  9. for (const path of Object.keys(schema.paths)) {
  10. const schemaType = schema.paths[path];
  11. if (schemaType.schema != null) {
  12. Object.defineProperty(schemaType.schema, '$schemaType', {
  13. configurable: true,
  14. writable: false,
  15. enumerable: false,
  16. value: schemaType
  17. });
  18. }
  19. Object.defineProperty(schemaType, '$parentSchema', {
  20. configurable: true,
  21. writable: false,
  22. enumerable: false,
  23. value: schema
  24. });
  25. }
  26. // `childSchemas` contains all descendant schemas, so no need to recurse
  27. // further.
  28. if (skipRecursion) {
  29. return;
  30. }
  31. for (const obj of schema.childSchemas) {
  32. setParentPointers(obj.schema, true);
  33. }
  34. };