getSchemaTypes.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. const Mixed = require('../../schema/mixed');
  6. const get = require('../get');
  7. const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
  8. const leanPopulateMap = require('./leanPopulateMap');
  9. const mpath = require('mpath');
  10. const populateModelSymbol = require('../symbols').populateModelSymbol;
  11. /*!
  12. * Given a model and its schema, find all possible schema types for `path`,
  13. * including searching through discriminators. If `doc` is specified, will
  14. * use the doc's values for discriminator keys when searching, otherwise
  15. * will search all discriminators.
  16. *
  17. * @param {Schema} schema
  18. * @param {Object} doc POJO
  19. * @param {string} path
  20. */
  21. module.exports = function getSchemaTypes(model, schema, doc, path) {
  22. const pathschema = schema.path(path);
  23. const topLevelDoc = doc;
  24. if (pathschema) {
  25. return pathschema;
  26. }
  27. const discriminatorKey = schema.discriminatorMapping &&
  28. schema.discriminatorMapping.key;
  29. if (discriminatorKey && model != null) {
  30. if (doc != null && doc[discriminatorKey] != null) {
  31. const discriminator = getDiscriminatorByValue(model.discriminators, doc[discriminatorKey]);
  32. schema = discriminator ? discriminator.schema : schema;
  33. } else if (model.discriminators != null) {
  34. return Object.keys(model.discriminators).reduce((arr, name) => {
  35. const disc = model.discriminators[name];
  36. return arr.concat(getSchemaTypes(disc, disc.schema, null, path));
  37. }, []);
  38. }
  39. }
  40. function search(parts, schema, subdoc, nestedPath) {
  41. let p = parts.length + 1;
  42. let foundschema;
  43. let trypath;
  44. while (p--) {
  45. trypath = parts.slice(0, p).join('.');
  46. foundschema = schema.path(trypath);
  47. if (foundschema == null) {
  48. continue;
  49. }
  50. if (foundschema.caster) {
  51. // array of Mixed?
  52. if (foundschema.caster instanceof Mixed) {
  53. return foundschema.caster;
  54. }
  55. let schemas = null;
  56. if (foundschema.schema != null && foundschema.schema.discriminators != null) {
  57. const discriminators = foundschema.schema.discriminators;
  58. const discriminatorKeyPath = trypath + '.' +
  59. foundschema.schema.options.discriminatorKey;
  60. const keys = subdoc ? mpath.get(discriminatorKeyPath, subdoc) || [] : [];
  61. schemas = Object.keys(discriminators).
  62. reduce(function(cur, discriminator) {
  63. const tiedValue = discriminators[discriminator].discriminatorMapping.value;
  64. if (doc == null || keys.indexOf(discriminator) !== -1 || keys.indexOf(tiedValue) !== -1) {
  65. cur.push(discriminators[discriminator]);
  66. }
  67. return cur;
  68. }, []);
  69. }
  70. // Now that we found the array, we need to check if there
  71. // are remaining document paths to look up for casting.
  72. // Also we need to handle array.$.path since schema.path
  73. // doesn't work for that.
  74. // If there is no foundschema.schema we are dealing with
  75. // a path like array.$
  76. if (p !== parts.length && foundschema.schema) {
  77. let ret;
  78. if (parts[p] === '$') {
  79. if (p + 1 === parts.length) {
  80. // comments.$
  81. return foundschema;
  82. }
  83. // comments.$.comments.$.title
  84. ret = search(
  85. parts.slice(p + 1),
  86. schema,
  87. subdoc ? mpath.get(trypath, subdoc) : null,
  88. nestedPath.concat(parts.slice(0, p))
  89. );
  90. if (ret) {
  91. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  92. !foundschema.schema.$isSingleNested;
  93. }
  94. return ret;
  95. }
  96. if (schemas != null && schemas.length > 0) {
  97. ret = [];
  98. for (const schema of schemas) {
  99. const _ret = search(
  100. parts.slice(p),
  101. schema,
  102. subdoc ? mpath.get(trypath, subdoc) : null,
  103. nestedPath.concat(parts.slice(0, p))
  104. );
  105. if (_ret != null) {
  106. _ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray ||
  107. !foundschema.schema.$isSingleNested;
  108. if (_ret.$isUnderneathDocArray) {
  109. ret.$isUnderneathDocArray = true;
  110. }
  111. ret.push(_ret);
  112. }
  113. }
  114. return ret;
  115. } else {
  116. ret = search(
  117. parts.slice(p),
  118. foundschema.schema,
  119. subdoc ? mpath.get(trypath, subdoc) : null,
  120. nestedPath.concat(parts.slice(0, p))
  121. );
  122. if (ret) {
  123. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  124. !foundschema.schema.$isSingleNested;
  125. }
  126. return ret;
  127. }
  128. } else if (p !== parts.length &&
  129. foundschema.$isMongooseArray &&
  130. foundschema.casterConstructor.$isMongooseArray) {
  131. // Nested arrays. Drill down to the bottom of the nested array.
  132. let type = foundschema;
  133. while (type.$isMongooseArray && !type.$isMongooseDocumentArray) {
  134. type = type.casterConstructor;
  135. }
  136. const ret = search(
  137. parts.slice(p),
  138. type.schema,
  139. null,
  140. nestedPath.concat(parts.slice(0, p))
  141. );
  142. if (ret != null) {
  143. return ret;
  144. }
  145. if (type.schema.discriminators) {
  146. const discriminatorPaths = [];
  147. for (const discriminatorName of Object.keys(type.schema.discriminators)) {
  148. const _schema = type.schema.discriminators[discriminatorName] || type.schema;
  149. const ret = search(parts.slice(p), _schema, null, nestedPath.concat(parts.slice(0, p)));
  150. if (ret != null) {
  151. discriminatorPaths.push(ret);
  152. }
  153. }
  154. if (discriminatorPaths.length > 0) {
  155. return discriminatorPaths;
  156. }
  157. }
  158. }
  159. }
  160. const fullPath = nestedPath.concat([trypath]).join('.');
  161. if (topLevelDoc != null && topLevelDoc.$__ && topLevelDoc.$populated(fullPath) && p < parts.length) {
  162. const model = doc.$__.populated[fullPath].options[populateModelSymbol];
  163. if (model != null) {
  164. const ret = search(
  165. parts.slice(p),
  166. model.schema,
  167. subdoc ? mpath.get(trypath, subdoc) : null,
  168. nestedPath.concat(parts.slice(0, p))
  169. );
  170. if (ret) {
  171. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  172. !model.schema.$isSingleNested;
  173. }
  174. return ret;
  175. }
  176. }
  177. const _val = get(topLevelDoc, trypath);
  178. if (_val != null) {
  179. const model = Array.isArray(_val) && _val.length > 0 ?
  180. leanPopulateMap.get(_val[0]) :
  181. leanPopulateMap.get(_val);
  182. // Populated using lean, `leanPopulateMap` value is the foreign model
  183. const schema = model != null ? model.schema : null;
  184. if (schema != null) {
  185. const ret = search(
  186. parts.slice(p),
  187. schema,
  188. subdoc ? mpath.get(trypath, subdoc) : null,
  189. nestedPath.concat(parts.slice(0, p))
  190. );
  191. if (ret != null) {
  192. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  193. !schema.$isSingleNested;
  194. return ret;
  195. }
  196. }
  197. }
  198. return foundschema;
  199. }
  200. }
  201. // look for arrays
  202. const parts = path.split('.');
  203. for (let i = 0; i < parts.length; ++i) {
  204. if (parts[i] === '$') {
  205. // Re: gh-5628, because `schema.path()` doesn't take $ into account.
  206. parts[i] = '0';
  207. }
  208. }
  209. return search(parts, schema, doc, []);
  210. };