Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

7.2 #13420

Merged
merged 26 commits into from
May 19, 2023
Merged

7.2 #13420

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
469d68d
chore: update node engine version to match mongodb's minimal
hasezoey May 2, 2023
9c37908
Merge pull request #13366 from hasezoey/updateNodeVer
vkarpov15 May 2, 2023
5d93dff
feat(document): add flattenObjectIds option to toObject() and toJSON()
vkarpov15 May 6, 2023
916e18d
Merge branch '7.2' of github.com:Automattic/mongoose into 7.2
vkarpov15 May 8, 2023
3da4992
Merge pull request #13383 from Automattic/vkarpov15/gh-13341
vkarpov15 May 8, 2023
83a7176
throw error if using `timeseries` on subschema
IslandRhythms May 12, 2023
efa040d
Merge branch 'master' into 7.2
vkarpov15 May 12, 2023
1931120
enhancement: propagate `toObject` and `toJSON`
IslandRhythms May 12, 2023
13bf560
feat(query): add `translateAliases` option to automatically call tran…
vkarpov15 May 12, 2023
2563c52
test: clean up tests for #13325
vkarpov15 May 13, 2023
cb0f2d6
test: some quick fixes and error message cleanup
vkarpov15 May 13, 2023
77d1378
Merge pull request #13395 from Automattic/IslandRhythms/error-timeseries
vkarpov15 May 13, 2023
2ca8b6a
test: cover toObject and toJSON for document array with populated vir…
vkarpov15 May 13, 2023
c4977c0
Merge pull request #13396 from Automattic/IslandRhythms/gh-13325
vkarpov15 May 14, 2023
2e83b8b
Update lib/query.js
vkarpov15 May 14, 2023
cd95cd8
fix: make translateAliases throw error on conflicting properties
vkarpov15 May 14, 2023
053b22a
Update lib/query.js
vkarpov15 May 14, 2023
198aec9
fix: handle translateAlias with update operators
vkarpov15 May 14, 2023
ca84162
Merge pull request #13397 from Automattic/vkarpov15/gh-7511
vkarpov15 May 15, 2023
77d0643
feat: upgrade mongodb -> 5.5.0
vkarpov15 May 15, 2023
0193f6c
Export MongooseError
May 16, 2023
bb9bc35
feat(model): add throwOnValidationError option for opting into gettin…
vkarpov15 May 17, 2023
88ba969
Merge pull request #13403 from ramos-ph/feat/13387
vkarpov15 May 18, 2023
cc2adec
test: fix some code review comments
vkarpov15 May 18, 2023
8b41e8a
Merge pull request #13410 from Automattic/vkarpov15/gh-13256
vkarpov15 May 19, 2023
3a333f8
Merge branch 'master' into 7.2
vkarpov15 May 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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