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.

getSchemaTypes.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. const Mixed = require('../../schema/mixed');
  6. const get = require('../get');
  7. const leanPopulateMap = require('./leanPopulateMap');
  8. const mpath = require('mpath');
  9. /*!
  10. * @param {Schema} schema
  11. * @param {Object} doc POJO
  12. * @param {string} path
  13. */
  14. module.exports = function getSchemaTypes(schema, doc, path) {
  15. const pathschema = schema.path(path);
  16. const topLevelDoc = doc;
  17. if (pathschema) {
  18. return pathschema;
  19. }
  20. function search(parts, schema, subdoc, nestedPath) {
  21. let p = parts.length + 1;
  22. let foundschema;
  23. let trypath;
  24. while (p--) {
  25. trypath = parts.slice(0, p).join('.');
  26. foundschema = schema.path(trypath);
  27. if (foundschema == null) {
  28. continue;
  29. }
  30. if (foundschema.caster) {
  31. // array of Mixed?
  32. if (foundschema.caster instanceof Mixed) {
  33. return foundschema.caster;
  34. }
  35. let schemas = null;
  36. if (doc != null && foundschema.schema != null && foundschema.schema.discriminators != null) {
  37. const discriminators = foundschema.schema.discriminators;
  38. const discriminatorKeyPath = trypath + '.' +
  39. foundschema.schema.options.discriminatorKey;
  40. const keys = subdoc ? mpath.get(discriminatorKeyPath, subdoc) || [] : [];
  41. schemas = Object.keys(discriminators).
  42. reduce(function(cur, discriminator) {
  43. if (keys.indexOf(discriminator) !== -1) {
  44. cur.push(discriminators[discriminator]);
  45. }
  46. return cur;
  47. }, []);
  48. }
  49. // Now that we found the array, we need to check if there
  50. // are remaining document paths to look up for casting.
  51. // Also we need to handle array.$.path since schema.path
  52. // doesn't work for that.
  53. // If there is no foundschema.schema we are dealing with
  54. // a path like array.$
  55. if (p !== parts.length && foundschema.schema) {
  56. let ret;
  57. if (parts[p] === '$') {
  58. if (p + 1 === parts.length) {
  59. // comments.$
  60. return foundschema;
  61. }
  62. // comments.$.comments.$.title
  63. ret = search(
  64. parts.slice(p + 1),
  65. schema,
  66. subdoc ? mpath.get(trypath, subdoc) : null,
  67. nestedPath.concat(parts.slice(0, p))
  68. );
  69. if (ret) {
  70. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  71. !foundschema.schema.$isSingleNested;
  72. }
  73. return ret;
  74. }
  75. if (schemas != null && schemas.length > 0) {
  76. ret = [];
  77. for (let i = 0; i < schemas.length; ++i) {
  78. const _ret = search(
  79. parts.slice(p),
  80. schemas[i],
  81. subdoc ? mpath.get(trypath, subdoc) : null,
  82. nestedPath.concat(parts.slice(0, p))
  83. );
  84. if (_ret != null) {
  85. _ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray ||
  86. !foundschema.schema.$isSingleNested;
  87. if (_ret.$isUnderneathDocArray) {
  88. ret.$isUnderneathDocArray = true;
  89. }
  90. ret.push(_ret);
  91. }
  92. }
  93. return ret;
  94. } else {
  95. ret = search(
  96. parts.slice(p),
  97. foundschema.schema,
  98. subdoc ? mpath.get(trypath, subdoc) : null,
  99. nestedPath.concat(parts.slice(0, p))
  100. );
  101. if (ret) {
  102. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  103. !foundschema.schema.$isSingleNested;
  104. }
  105. return ret;
  106. }
  107. }
  108. }
  109. const fullPath = nestedPath.concat([trypath]).join('.');
  110. if (topLevelDoc.$__ && topLevelDoc.populated(fullPath) && p < parts.length) {
  111. const schema = get(doc.$__.populated[fullPath], 'options.model.schema');
  112. if (schema != null) {
  113. const ret = search(
  114. parts.slice(p),
  115. schema,
  116. subdoc ? mpath.get(trypath, subdoc) : null,
  117. nestedPath.concat(parts.slice(0, p))
  118. );
  119. if (ret) {
  120. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  121. !schema.$isSingleNested;
  122. }
  123. return ret;
  124. }
  125. }
  126. const _val = get(topLevelDoc, trypath);
  127. if (_val != null) {
  128. const model = Array.isArray(_val) && _val.length > 0 ?
  129. leanPopulateMap.get(_val[0]) :
  130. leanPopulateMap.get(_val);
  131. // Populated using lean, `leanPopulateMap` value is the foreign model
  132. const schema = model != null ? model.schema : null;
  133. if (schema != null) {
  134. const ret = search(
  135. parts.slice(p),
  136. schema,
  137. subdoc ? mpath.get(trypath, subdoc) : null,
  138. nestedPath.concat(parts.slice(0, p))
  139. );
  140. if (ret) {
  141. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  142. !schema.$isSingleNested;
  143. }
  144. return ret;
  145. }
  146. }
  147. return foundschema;
  148. }
  149. }
  150. // look for arrays
  151. const parts = path.split('.');
  152. for (let i = 0; i < parts.length; ++i) {
  153. if (parts[i] === '$') {
  154. // Re: gh-5628, because `schema.path()` doesn't take $ into account.
  155. parts[i] = '0';
  156. }
  157. }
  158. return search(parts, schema, doc, []);
  159. };