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.

getEmbeddedDiscriminatorPath.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const get = require('../get');
  3. /*!
  4. * Like `schema.path()`, except with a document, because impossible to
  5. * determine path type without knowing the embedded discriminator key.
  6. */
  7. module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
  8. options = options || {};
  9. const typeOnly = options.typeOnly;
  10. const parts = path.split('.');
  11. let schema = null;
  12. let type = 'adhocOrUndefined';
  13. for (let i = 0; i < parts.length; ++i) {
  14. const subpath = parts.slice(0, i + 1).join('.');
  15. schema = doc.schema.path(subpath);
  16. if (schema == null) {
  17. continue;
  18. }
  19. type = doc.schema.pathType(subpath);
  20. if ((schema.$isSingleNested || schema.$isMongooseDocumentArrayElement) &&
  21. schema.schema.discriminators != null) {
  22. const discriminators = schema.schema.discriminators;
  23. const discriminatorKey = doc.get(subpath + '.' +
  24. get(schema, 'schema.options.discriminatorKey'));
  25. if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
  26. continue;
  27. }
  28. const rest = parts.slice(i + 1).join('.');
  29. schema = discriminators[discriminatorKey].path(rest);
  30. if (schema != null) {
  31. type = discriminators[discriminatorKey].pathType(rest);
  32. break;
  33. }
  34. }
  35. }
  36. // Are we getting the whole schema or just the type, 'real', 'nested', etc.
  37. return typeOnly ? type : schema;
  38. };