Skip to content

Commit

Permalink
Merge pull request #13420 from Automattic/7.2
Browse files Browse the repository at this point in the history
7.2
  • Loading branch information
vkarpov15 committed May 19, 2023
2 parents 06cd519 + 3a333f8 commit 71162ef
Show file tree
Hide file tree
Showing 23 changed files with 566 additions and 21 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports.Mixed = mongoose.Mixed;
module.exports.Date = mongoose.Date;
module.exports.Number = mongoose.Number;
module.exports.Error = mongoose.Error;
module.exports.MongooseError = mongoose.MongooseError;
module.exports.now = mongoose.now;
module.exports.CastError = mongoose.CastError;
module.exports.SchemaTypeOptions = mongoose.SchemaTypeOptions;
Expand Down
18 changes: 16 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3676,6 +3676,15 @@ Document.prototype.$toObject = function(options, json) {
flattenMaps = schemaOptions.flattenMaps;
}

let flattenObjectIds;
if (options._calledWithOptions.flattenObjectIds != null) {
flattenObjectIds = options.flattenObjectIds;
} else if (defaultOptions.flattenObjectIds != null) {
flattenObjectIds = defaultOptions.flattenObjectIds;
} else {
flattenObjectIds = schemaOptions.flattenObjectIds;
}

// The original options that will be passed to `clone()`. Important because
// `clone()` will recursively call `$toObject()` on embedded docs, so we
// need the original options the user passed in, plus `_isNested` and
Expand All @@ -3685,6 +3694,7 @@ Document.prototype.$toObject = function(options, json) {
json: json,
minimize: _minimize,
flattenMaps: flattenMaps,
flattenObjectIds: flattenObjectIds,
_seen: (options && options._seen) || new Map()
});

Expand Down Expand Up @@ -3900,6 +3910,7 @@ Document.prototype.$toObject = function(options, json) {
* @param {Boolean} [options.depopulate=false] if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths.
* @param {Boolean} [options.versionKey=true] if false, exclude the version key (`__v` by default) from the output
* @param {Boolean} [options.flattenMaps=false] if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`.
* @param {Boolean} [options.flattenObjectIds=false] if true, convert any ObjectIds in the result to 24 character hex strings.
* @param {Boolean} [options.useProjection=false] - If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema.
* @return {Object} js object (not a POJO)
* @see mongodb.Binary https://mongodb.github.io/node-mongodb-native/4.9/classes/Binary.html
Expand Down Expand Up @@ -3968,8 +3979,7 @@ function applyVirtuals(self, json, options, toObjectOptions) {
let virtualsToApply = null;
if (Array.isArray(options.virtuals)) {
virtualsToApply = new Set(options.virtuals);
}
else if (options.virtuals && options.virtuals.pathsToSkip) {
} else if (options.virtuals && options.virtuals.pathsToSkip) {
virtualsToApply = new Set(paths);
for (let i = 0; i < options.virtuals.pathsToSkip.length; i++) {
if (virtualsToApply.has(options.virtuals.pathsToSkip[i])) {
Expand Down Expand Up @@ -4181,6 +4191,7 @@ function omitDeselectedFields(self, json) {
*
* @param {Object} options
* @param {Boolean} [options.flattenMaps=true] if true, convert Maps to [POJOs](https://masteringjs.io/tutorials/fundamentals/pojo). Useful if you want to `JSON.stringify()` the result.
* @param {Boolean} [options.flattenObjectIds=false] if true, convert any ObjectIds in the result to 24 character hex strings.
* @return {Object}
* @see Document#toObject https://mongoosejs.com/docs/api/document.html#Document.prototype.toObject()
* @see JSON.stringify() in JavaScript https://thecodebarbarian.com/the-80-20-guide-to-json-stringify-in-javascript.html
Expand All @@ -4193,6 +4204,9 @@ Document.prototype.toJSON = function(options) {
return this.$toObject(options, true);
};

/*!
* ignore
*/

Document.prototype.ownerDocument = function() {
return this;
Expand Down
41 changes: 41 additions & 0 deletions lib/error/bulkWriteError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*!
* Module dependencies.
*/

'use strict';

const MongooseError = require('./');


/**
* If `bulkWrite()` or `insertMany()` has validation errors, but
* all valid operations succeed, and 'throwOnValidationError' is true,
* Mongoose will throw this error.
*
* @api private
*/

class MongooseBulkWriteError extends MongooseError {
constructor(validationErrors, results, rawResult, operation) {
let preview = validationErrors.map(e => e.message).join(', ');
if (preview.length > 200) {
preview = preview.slice(0, 200) + '...';
}
super(`${operation} failed with ${validationErrors.length} Mongoose validation errors: ${preview}`);

this.validationErrors = validationErrors;
this.results = results;
this.rawResult = rawResult;
this.operation = operation;
}
}

Object.defineProperty(MongooseBulkWriteError.prototype, 'name', {
value: 'MongooseBulkWriteError'
});

/*!
* exports
*/

module.exports = MongooseBulkWriteError;
30 changes: 30 additions & 0 deletions lib/error/invalidSchemaOption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

/*!
* Module dependencies.
*/

'use strict';

const MongooseError = require('./');

class InvalidSchemaOptionError extends MongooseError {
/**
* InvalidSchemaOption Error constructor.
* @param {String} name
* @api private
*/
constructor(name, option) {
const msg = `Cannot create use schema for property "${name}" because the schema has the ${option} option enabled.`;
super(msg);
}
}

Object.defineProperty(InvalidSchemaOptionError.prototype, 'name', {
value: 'InvalidSchemaOptionError'
});

/*!
* exports
*/

module.exports = InvalidSchemaOptionError;
3 changes: 3 additions & 0 deletions lib/helpers/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function clone(obj, options, isArrayChild) {
}

if (isBsonType(obj, 'ObjectId')) {
if (options && options.flattenObjectIds) {
return obj.toJSON();
}
return new ObjectId(obj.id);
}

Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ Mongoose.prototype.Number = SchemaTypes.Number;
*/

Mongoose.prototype.Error = require('./error/index');
Mongoose.prototype.MongooseError = require('./error/mongooseError');

/**
* Mongoose uses this function to get the current time when setting
Expand Down
Loading

0 comments on commit 71162ef

Please sign in to comment.