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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. } else if (p !== parts.length &&
  108. foundschema.$isMongooseArray &&
  109. foundschema.casterConstructor.$isMongooseArray) {
  110. // Nested arrays. Drill down to the bottom of the nested array.
  111. // Ignore discriminators.
  112. let type = foundschema;
  113. while (type.$isMongooseArray && !type.$isMongooseDocumentArray) {
  114. type = type.casterConstructor;
  115. }
  116. return search(
  117. parts.slice(p),
  118. type.schema,
  119. null,
  120. nestedPath.concat(parts.slice(0, p))
  121. );
  122. }
  123. }
  124. const fullPath = nestedPath.concat([trypath]).join('.');
  125. if (topLevelDoc.$__ && topLevelDoc.populated(fullPath) && p < parts.length) {
  126. const schema = get(doc.$__.populated[fullPath], 'options.model.schema');
  127. if (schema != null) {
  128. const ret = search(
  129. parts.slice(p),
  130. schema,
  131. subdoc ? mpath.get(trypath, subdoc) : null,
  132. nestedPath.concat(parts.slice(0, p))
  133. );
  134. if (ret) {
  135. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  136. !schema.$isSingleNested;
  137. }
  138. return ret;
  139. }
  140. }
  141. const _val = get(topLevelDoc, trypath);
  142. if (_val != null) {
  143. const model = Array.isArray(_val) && _val.length > 0 ?
  144. leanPopulateMap.get(_val[0]) :
  145. leanPopulateMap.get(_val);
  146. // Populated using lean, `leanPopulateMap` value is the foreign model
  147. const schema = model != null ? model.schema : null;
  148. if (schema != null) {
  149. const ret = search(
  150. parts.slice(p),
  151. schema,
  152. subdoc ? mpath.get(trypath, subdoc) : null,
  153. nestedPath.concat(parts.slice(0, p))
  154. );
  155. if (ret) {
  156. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  157. !schema.$isSingleNested;
  158. }
  159. return ret;
  160. }
  161. }
  162. return foundschema;
  163. }
  164. }
  165. // look for arrays
  166. const parts = path.split('.');
  167. for (let i = 0; i < parts.length; ++i) {
  168. if (parts[i] === '$') {
  169. // Re: gh-5628, because `schema.path()` doesn't take $ into account.
  170. parts[i] = '0';
  171. }
  172. }
  173. return search(parts, schema, doc, []);
  174. };