Skip to content

Commit

Permalink
fix: use lazy loading to avoid browserDocument edge case
Browse files Browse the repository at this point in the history
Fix #5842
  • Loading branch information
vkarpov15 committed Jan 26, 2018
1 parent a5b3899 commit 31b84f0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ var Types = {
ObjectId: require('./objectid'),
Buffer: require('./buffer')
};
var MongooseArray = require('../types').Array;
var EmbeddedDoc = require('../types').Embedded;
var Mixed = require('./mixed');
var cast = require('../cast');
var util = require('util');
var utils = require('../utils');
var castToNumber = require('./operators/helpers').castToNumber;
var geospatial = require('./operators/geospatial');

var MongooseArray;
var EmbeddedDoc;

/**
* Array SchemaType constructor
*
Expand All @@ -35,6 +36,9 @@ var geospatial = require('./operators/geospatial');
*/

function SchemaArray(key, cast, options, schemaOptions) {
// lazy load
EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);

var typeKey = 'type';
if (schemaOptions && schemaOptions.typeKey) {
typeKey = schemaOptions.typeKey;
Expand Down Expand Up @@ -155,6 +159,9 @@ SchemaArray.prototype.applyGetters = function(value, scope) {
*/

SchemaArray.prototype.cast = function(value, doc, init) {
// lazy load
MongooseArray || (MongooseArray = require('../types').Array);

if (Array.isArray(value)) {
if (!value.length && doc) {
var indexes = doc.schema.indexedPaths();
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var handleBitwiseOperator = require('./operators/bitwise');
var utils = require('../utils');

var MongooseBuffer = require('../types').Buffer;
var MongooseBuffer = require('../types/buffer');
var SchemaType = require('../schematype');

var Binary = MongooseBuffer.Binary;
Expand Down
13 changes: 11 additions & 2 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ var ArrayType = require('./array');
var CastError = require('../error/cast');
var Document = require('../document');
var EventEmitter = require('events').EventEmitter;
var MongooseDocumentArray = require('../types/documentarray');
var SchemaType = require('../schematype');
var Subdocument = require('../types/embedded');
var discriminator = require('../services/model/discriminator');
var util = require('util');
var utils = require('../utils');

var MongooseDocumentArray;
var Subdocument;

/**
* SubdocsArray SchemaType constructor
*
Expand Down Expand Up @@ -66,6 +67,8 @@ DocumentArray.prototype.constructor = DocumentArray;
*/

function _createConstructor(schema, options) {
Subdocument || (Subdocument = require('../types/embedded'));

// compile an embedded document for this schema
function EmbeddedDocument() {
Subdocument.apply(this, arguments);
Expand Down Expand Up @@ -130,6 +133,9 @@ DocumentArray.prototype.discriminator = function(name, schema) {
*/

DocumentArray.prototype.doValidate = function(array, fn, scope, options) {
// lazy load
MongooseDocumentArray || (MongooseDocumentArray = require('../types/documentarray'));

var _this = this;
SchemaType.prototype.doValidate.call(this, array, function(err) {
if (err) {
Expand Down Expand Up @@ -251,6 +257,9 @@ DocumentArray.prototype.doValidateSync = function(array, scope) {
*/

DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
// lazy load
MongooseDocumentArray || (MongooseDocumentArray = require('../types/documentarray'));

var selected;
var subdoc;
var i;
Expand Down
6 changes: 5 additions & 1 deletion lib/schema/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
var $exists = require('./operators/exists');
var EventEmitter = require('events').EventEmitter;
var SchemaType = require('../schematype');
var Subdocument = require('../types/subdocument');
var castToNumber = require('./operators/helpers').castToNumber;
var discriminator = require('../services/model/discriminator');
var geospatial = require('./operators/geospatial');

var Subdocument;

module.exports = Embedded;

/**
Expand Down Expand Up @@ -43,6 +44,9 @@ Embedded.prototype = Object.create(SchemaType.prototype);
*/

function _createConstructor(schema) {
// lazy load
Subdocument || (Subdocument = require('../types/subdocument'));

var _embedded = function SingleNested(value, path, parent) {
var _this = this;

Expand Down

0 comments on commit 31b84f0

Please sign in to comment.