All files / mongoose/lib/helpers/discriminator mergeDiscriminatorSchema.js

14.28% Statements 6/42
0% Branches 0/51
0% Functions 0/1
14.28% Lines 6/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  1x 1x 1x 1x 1x                   1x                                                                                                                                    
'use strict';
const schemaMerge = require('../schema/merge');
const specialProperties = require('../../helpers/specialProperties');
const isBsonType = require('../../helpers/isBsonType');
const ObjectId = require('../../types/objectid');
const isObject = require('../../helpers/isObject');
/**
 * Merges `from` into `to` without overwriting existing properties.
 *
 * @param {Object} to
 * @param {Object} from
 * @param {String} [path]
 * @api private
 */
 
module.exports = function mergeDiscriminatorSchema(to, from, path, seen) {
  const keys = Object.keys(from);
  let i = 0;
  const len = keys.length;
  let key;
 
  path = path || '';
  seen = seen || new WeakSet();
 
  if (seen.has(from)) {
    return;
  }
  seen.add(from);
 
  while (i < len) {
    key = keys[i++];
    if (!path) {
      if (key === 'discriminators' ||
        key === 'base' ||
        key === '_applyDiscriminators' ||
        key === '_userProvidedOptions' ||
        key === 'options' ||
        key === 'tree') {
        continue;
      }
    }
    if (path === 'tree' && from != null && from.instanceOfSchema) {
      continue;
    }
    if (specialProperties.has(key)) {
      continue;
    }
    if (to[key] == null) {
      to[key] = from[key];
    } else if (isObject(from[key])) {
      if (!isObject(to[key])) {
        to[key] = {};
      }
      if (from[key] != null) {
        // Skip merging schemas if we're creating a discriminator schema and
        // base schema has a given path as a single nested but discriminator schema
        // has the path as a document array, or vice versa (gh-9534)
        if ((from[key].$isSingleNested && to[key].$isMongooseDocumentArray) ||
              (from[key].$isMongooseDocumentArray && to[key].$isSingleNested) ||
              (from[key].$isMongooseDocumentArrayElement && to[key].$isMongooseDocumentArrayElement)) {
          continue;
        } else if (from[key].instanceOfSchema) {
          if (to[key].instanceOfSchema) {
            schemaMerge(to[key], from[key].clone(), true);
          } else {
            to[key] = from[key].clone();
          }
          continue;
        } else if (isBsonType(from[key], 'ObjectId')) {
          to[key] = new ObjectId(from[key]);
          continue;
        }
      }
      mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key, seen);
    }
  }
 
  if (from != null && from.instanceOfSchema) {
    to.tree = Object.assign({}, from.tree, to.tree);
  }
};