PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/mongoose/lib/services/populate/getSchemaTypes.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 115 lines | 82 code | 14 blank | 19 comment | 28 complexity | 82fd703b05e5e620b9d82366d816dac4 MD5 | raw file
  1'use strict';
  2
  3/*!
  4 * ignore
  5 */
  6
  7var Mixed = require('../../schema/mixed');
  8var mpath = require('mpath');
  9
 10/*!
 11 * @param {Schema} schema
 12 * @param {Object} doc POJO
 13 * @param {string} path
 14 */
 15
 16module.exports = function getSchemaTypes(schema, doc, path) {
 17  const pathschema = schema.path(path);
 18
 19  if (pathschema) {
 20    return pathschema;
 21  }
 22
 23  function search(parts, schema) {
 24    let p = parts.length + 1;
 25    let foundschema;
 26    let trypath;
 27
 28    while (p--) {
 29      trypath = parts.slice(0, p).join('.');
 30      foundschema = schema.path(trypath);
 31      if (foundschema) {
 32        if (foundschema.caster) {
 33          // array of Mixed?
 34          if (foundschema.caster instanceof Mixed) {
 35            return foundschema.caster;
 36          }
 37
 38          let schemas = null;
 39          if (doc != null && foundschema.schema != null && foundschema.schema.discriminators != null) {
 40            const discriminators = foundschema.schema.discriminators;
 41            const keys = mpath.get(trypath + '.' + foundschema.schema.options.discriminatorKey,
 42              doc);
 43            schemas = Object.keys(discriminators).
 44              reduce(function(cur, discriminator) {
 45                if (keys.indexOf(discriminator) !== -1) {
 46                  cur.push(discriminators[discriminator]);
 47                }
 48                return cur;
 49              }, []);
 50          }
 51
 52          // Now that we found the array, we need to check if there
 53          // are remaining document paths to look up for casting.
 54          // Also we need to handle array.$.path since schema.path
 55          // doesn't work for that.
 56          // If there is no foundschema.schema we are dealing with
 57          // a path like array.$
 58          if (p !== parts.length && foundschema.schema) {
 59            let ret;
 60            if (parts[p] === '$') {
 61              if (p + 1 === parts.length) {
 62                // comments.$
 63                return foundschema;
 64              }
 65              // comments.$.comments.$.title
 66              ret = search(parts.slice(p + 1), schema);
 67              if (ret) {
 68                ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
 69                  !foundschema.schema.$isSingleNested;
 70              }
 71              return ret;
 72            }
 73
 74            if (schemas != null && schemas.length > 0) {
 75              ret = [];
 76              for (var i = 0; i < schemas.length; ++i) {
 77                let _ret = search(parts.slice(p), schemas[i]);
 78                if (_ret != null) {
 79                  _ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray ||
 80                    !foundschema.schema.$isSingleNested;
 81                  if (_ret.$isUnderneathDocArray) {
 82                    ret.$isUnderneathDocArray = true;
 83                  }
 84                  ret.push(_ret);
 85                }
 86              }
 87              return ret;
 88            } else {
 89              ret = search(parts.slice(p), foundschema.schema);
 90
 91              if (ret) {
 92                ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
 93                  !foundschema.schema.$isSingleNested;
 94              }
 95
 96              return ret;
 97            }
 98          }
 99        }
100
101        return foundschema;
102      }
103    }
104  }
105
106  // look for arrays
107  const parts = path.split('.');
108  for (let i = 0; i < parts.length; ++i) {
109    if (parts[i] === '$') {
110      // Re: gh-5628, because `schema.path()` doesn't take $ into account.
111      parts[i] = '0';
112    }
113  }
114  return search(parts, schema);
115};