Skip to content

Commit

Permalink
Merge branch '8.0' into vkarpov15/gh-13782
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Sep 26, 2023
2 parents 6ddbcb8 + 2348b7a commit 8527c8d
Show file tree
Hide file tree
Showing 86 changed files with 734 additions and 1,286 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Expand Up @@ -22,6 +22,9 @@ module.exports = {
'**/*.md/*.ts',
'**/*.md/*.typescript'
],
parserOptions: {
project: './tsconfig.json'
},
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended'
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
node-version: 14
node-version: 18

- run: npm install

Expand All @@ -39,7 +39,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 16, 18, 20]
node: [16, 18, 20]
os: [ubuntu-20.04, ubuntu-22.04]
mongodb: [4.4.18, 5.0.14, 6.0.4]
include:
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.34.x
deno-version: v1.36.x
- run: deno --version
- run: npm install
- name: Run Deno tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tsd.yml
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
node-version: 14
node-version: 18

- run: npm install

Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,12 @@
7.5.1 / 2023-09-11
==================
* fix: set default value for _update when no update object is provided and versionKey is set to false #13795 #13783 [MohOraby](https://github.com/MohOraby)
* fix: avoid unexpected error when accessing null array element on discriminator array when populating #13716 [ZSabakh](https://github.com/ZSabakh)
* types(schematypes): use DocType for instance method this #13822 #13800 [pshaddel](https://github.com/pshaddel)
* types: remove duplicated 'exists' method in Model interface in models.d.ts #13818 [ohzeno](https://github.com/ohzeno)
* docs(model): replace outdated docs on deprecated findOneAndUpdate() overwrite option #13821 #13715
* docs: add example of using `virtuals.pathsToSkip` option for `toObject()` and `toJSON()` #13798 [RobertHunter-Pluto](https://github.com/RobertHunter-Pluto)

7.5.0 / 2023-08-29
==================
* feat: use mongodb driver v5.18.1
Expand Down
2 changes: 0 additions & 2 deletions docs/middleware.md
Expand Up @@ -48,7 +48,6 @@ In query middleware functions, `this` refers to the query.
* [find](api/query.html#query_Query-find)
* [findOne](api/query.html#query_Query-findOne)
* [findOneAndDelete](api/query.html#query_Query-findOneAndDelete)
* [findOneAndRemove](api/query.html#query_Query-findOneAndRemove)
* [findOneAndReplace](api/query.html#query_Query-findOneAndReplace)
* [findOneAndUpdate](api/query.html#query_Query-findOneAndUpdate)
* [remove](api/model.html#model_Model-remove)
Expand Down Expand Up @@ -81,7 +80,6 @@ Here are the possible strings that can be passed to `pre()`
* find
* findOne
* findOneAndDelete
* findOneAndRemove
* findOneAndReplace
* findOneAndUpdate
* init
Expand Down
62 changes: 62 additions & 0 deletions docs/migrating_to_8.md
@@ -0,0 +1,62 @@
# Migrating from 7.x to 8.x

<style>
ul > li {
padding: 4px 0px;
}
</style>

There are several backwards-breaking changes
you should be aware of when migrating from Mongoose 7.x to Mongoose 8.x.

If you're still on Mongoose 6.x or earlier, please read the [Mongoose 6.x to 7.x migration guide](migrating_to_7.html) and upgrade to Mongoose 7.x first before upgrading to Mongoose 8.

* [Removed `rawResult` option for `findOneAndUpdate()`](#removed-rawresult-option-for-findoneandupdate)
* [Changed behavior for `findOneAndUpdate()` with `orFail()` and upsert](#changed-behavior-for-findoneandupdate-with-orfail-and-upsert)
* [MongoDB Node Driver 6.0](#mongodb-node-driver-6)
* [Removed `findOneAndRemove()`](#removed-findoneandremove)
* [Removed id Setter](#removed-id-setter)

<h2 id="removed-rawresult-option-for-findoneandupdate"><a href="#removed-rawresult-option-for-findoneandupdate">Removed <code>rawResult</code> option for <code>findOneAndUpdate()</code></a></h2>

The `rawResult` option for `findOneAndUpdate()`, `findOneAndReplace()`, and `findOneAndDelete()` has been replaced by the `includeResultMetadata` option.

```javascript
const filter = { name: 'Will Riker' };
const update = { age: 29 };

const res = await Character.findOneAndUpdate(filter, update, {
new: true,
upsert: true,
// Replace `rawResult: true` with `includeResultMetadata: true`
includeResultMetadata: true
});
```

`includeResultMetadata` in Mongoose 8 behaves identically to `rawResult`.

<h2 id="changed-behavior-for-findoneandupdate-with-orfail-and-upsert"><a href="#changed-behavior-for-findoneandupdate-with-orfail-and-upsert">Changed behavior for <code>findOneAndUpdate()</code> with <code>orFail()</code> and upsert</a></h2>

In Mongoose 7, `findOneAndUpdate(filter, update, { upsert: true }).orFail()` would throw a `DocumentNotFoundError` if a new document was upserted.
In other words, `findOneAndUpdate().orFail()` always threw an error if no document was found, even if a new document was upserted.

In Mongoose 8, `findOneAndUpdate(filter, update, { upsert: true }).orFail()` always succeeds.
`findOneAndUpdate().orFail()` now throws a `DocumentNotFoundError` if there's no document returned, rather than if no document was found.

<h2 id="mongodb-node-driver-6"><a href="#mongodb-node-driver-6">MongoDB Node Driver 6</a></h2>

Mongoose 8 uses [v6.x of the MongoDB Node driver](https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md#600-2023-08-28).
There's a few noteable changes in MongoDB Node driver v6 that affect Mongoose:

1. The `ObjectId` constructor no longer accepts strings of length 12. In Mongoose 7, `new mongoose.Types.ObjectId('12charstring')` was perfectly valid. In Mongoose 8, `new mongoose.Types.ObjectId('12charstring')` throws an error.

<h2 id="removed-findoneandremove"><a href="#removed-findoneandremove">Removed <code>findOneAndRemove()</code></a></h2>

In Mongoose 7, `findOneAndRemove()` was an alias for `findOneAndDelete()` that Mongoose supported for backwards compatibility.
Mongoose 8 no longer supports `findOneAndRemove()`.
Use `findOneAndDelete()` instead.

<h2 id="removed-id-setter"><a href="#removed-id-setter">Removed id Setter</a></h2>

In Mongoose 7.4, Mongoose introduced an `id` setter that made `doc.id = '0'.repeat(24)` equivalent to `doc._id = '0'.repeat(24)`.
In Mongoose 8, that setter is now removed.
2 changes: 1 addition & 1 deletion docs/models.md
Expand Up @@ -193,7 +193,7 @@ If you attempt to `save()` a document from a View, you will get an error from th

## Yet more

The [API docs](api/model.html#model_Model) cover many additional methods available like [count](api/model.html#model_Model-count), [mapReduce](api/model.html#model_Model-mapReduce), [aggregate](api/model.html#model_Model-aggregate), and [more](api/model.html#model_Model-findOneAndRemove).
The [API docs](api/model.html#model_Model) cover many additional methods available like [count](api/model.html#model_Model-count), [mapReduce](api/model.html#model_Model-mapReduce), [aggregate](api/model.html#model_Model-aggregate), and more.

## Next Up

Expand Down
1 change: 0 additions & 1 deletion docs/queries.md
Expand Up @@ -14,7 +14,6 @@ Each of these functions returns a
* [`Model.findByIdAndUpdate()`](api.html#model_Model-findByIdAndUpdate)
* [`Model.findOne()`](api.html#model_Model-findOne)
* [`Model.findOneAndDelete()`](api.html#model_Model-findOneAndDelete)
* [`Model.findOneAndRemove()`](api.html#model_Model-findOneAndRemove)
* [`Model.findOneAndReplace()`](api.html#model_Model-findOneAndReplace)
* [`Model.findOneAndUpdate()`](api.html#model_Model-findOneAndUpdate)
* [`Model.replaceOne()`](api.html#model_Model-replaceOne)
Expand Down
10 changes: 5 additions & 5 deletions docs/source/api.js
Expand Up @@ -18,16 +18,16 @@ const files = [
'lib/cursor/QueryCursor.js',
'lib/aggregate.js',
'lib/cursor/AggregationCursor.js',
'lib/schematype.js',
'lib/virtualtype.js',
'lib/schemaType.js',
'lib/virtualType.js',
'lib/error/index.js',
'lib/schema/array.js',
'lib/schema/documentarray.js',
'lib/schema/SubdocumentPath.js',
'lib/schema/documentArray.js',
'lib/schema/subdocument.js',
'lib/schema/boolean.js',
'lib/schema/buffer.js',
'lib/schema/number.js',
'lib/schema/objectid.js',
'lib/schema/objectId.js',
'lib/schema/string.js',
'lib/options/SchemaTypeOptions.js',
'lib/options/SchemaArrayOptions.js',
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.js
Expand Up @@ -65,6 +65,7 @@ docs['docs/migration.md'] = { guide: true, title: 'Migration Guide', markdown: t
docs['docs/migrating_to_5.md'] = { guide: true, title: 'Migrating to Mongoose 5', markdown: true };
docs['docs/migrating_to_6.md'] = { guide: true, title: 'Migrating to Mongoose 6', markdown: true };
docs['docs/migrating_to_7.md'] = { guide: true, title: 'Migrating to Mongoose 7', markdown: true };
docs['docs/migrating_to_8.md'] = { guide: true, title: 'Migrating to Mongoose 8', markdown: true };
docs['docs/connections.md'] = { guide: true, title: 'Connecting to MongoDB', markdown: true };
docs['docs/lambda.md'] = { guide: true, title: 'Using Mongoose With AWS Lambda', markdown: true };
docs['docs/geojson.md'] = { guide: true, title: 'Using GeoJSON', acquit: true, markdown: true };
Expand Down
6 changes: 3 additions & 3 deletions lib/browser.js
Expand Up @@ -4,7 +4,7 @@

require('./driver').set(require('./drivers/browser'));

const DocumentProvider = require('./document_provider.js');
const DocumentProvider = require('./documentProvider.js');

DocumentProvider.setBrowser(true);

Expand Down Expand Up @@ -67,7 +67,7 @@ exports.Types = require('./types');
* @method VirtualType
* @api public
*/
exports.VirtualType = require('./virtualtype');
exports.VirtualType = require('./virtualType');

/**
* The various Mongoose SchemaTypes.
Expand All @@ -81,7 +81,7 @@ exports.VirtualType = require('./virtualtype');
* @api public
*/

exports.SchemaType = require('./schematype.js');
exports.SchemaType = require('./schemaType.js');

/**
* Internal utils
Expand Down
2 changes: 1 addition & 1 deletion lib/collection.js
Expand Up @@ -5,7 +5,7 @@
*/

const EventEmitter = require('events').EventEmitter;
const STATES = require('./connectionstate');
const STATES = require('./connectionState');
const immediate = require('./helpers/immediate');

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/connection.js
Expand Up @@ -7,7 +7,7 @@
const ChangeStream = require('./cursor/ChangeStream');
const EventEmitter = require('events').EventEmitter;
const Schema = require('./schema');
const STATES = require('./connectionstate');
const STATES = require('./connectionState');
const MongooseError = require('./error/index');
const ServerSelectionError = require('./error/serverSelection');
const SyncIndexesError = require('./error/syncIndexes');
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/cursor/QueryCursor.js
Expand Up @@ -7,7 +7,7 @@
const MongooseError = require('../error/mongooseError');
const Readable = require('stream').Readable;
const eachAsync = require('../helpers/cursor/eachAsync');
const helpers = require('../queryhelpers');
const helpers = require('../queryHelpers');
const kareem = require('kareem');
const immediate = require('../helpers/immediate');
const util = require('util');
Expand Down
2 changes: 1 addition & 1 deletion lib/document.js
Expand Up @@ -35,7 +35,7 @@ const internalToObjectOptions = require('./options').internalToObjectOptions;
const markArraySubdocsPopulated = require('./helpers/populate/markArraySubdocsPopulated');
const minimize = require('./helpers/minimize');
const mpath = require('mpath');
const queryhelpers = require('./queryhelpers');
const queryhelpers = require('./queryHelpers');
const utils = require('./utils');
const isPromise = require('./helpers/isPromise');

Expand Down
2 changes: 1 addition & 1 deletion lib/document_provider.js → lib/documentProvider.js
Expand Up @@ -15,7 +15,7 @@ let isBrowser = false;
*
* @api private
*/
module.exports = function() {
module.exports = function documentProvider() {
if (isBrowser) {
return BrowserDocument;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/drivers/node-mongodb-native/connection.js
Expand Up @@ -6,7 +6,7 @@

const MongooseConnection = require('../../connection');
const MongooseError = require('../../error/index');
const STATES = require('../../connectionstate');
const STATES = require('../../connectionState');
const mongodb = require('mongodb');
const pkg = require('../../../package.json');
const processConnectionOptions = require('../../helpers/processConnectionOptions');
Expand Down
7 changes: 6 additions & 1 deletion lib/helpers/discriminator/mergeDiscriminatorSchema.js
Expand Up @@ -34,7 +34,8 @@ module.exports = function mergeDiscriminatorSchema(to, from, path, seen) {
key === 'base' ||
key === '_applyDiscriminators' ||
key === '_userProvidedOptions' ||
key === 'options') {
key === 'options' ||
key === 'tree') {
continue;
}
}
Expand Down Expand Up @@ -73,4 +74,8 @@ module.exports = function mergeDiscriminatorSchema(to, from, path, seen) {
mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key, seen);
}
}

if (from != null && from.instanceOfSchema) {
to.tree = Object.assign({}, from.tree, to.tree);
}
};
5 changes: 3 additions & 2 deletions lib/helpers/model/applyHooks.js
Expand Up @@ -64,7 +64,7 @@ function applyHooks(model, schema, options) {
continue;
}

applyHooks(childModel, type.schema, options);
applyHooks(childModel, type.schema, { ...options, isChildSchema: true });
if (childModel.discriminators != null) {
const keys = Object.keys(childModel.discriminators);
for (const key of keys) {
Expand Down Expand Up @@ -104,7 +104,8 @@ function applyHooks(model, schema, options) {

objToDecorate.$__originalValidate = objToDecorate.$__originalValidate || objToDecorate.$__validate;

for (const method of ['save', 'validate', 'remove', 'deleteOne']) {
const internalMethodsToWrap = options && options.isChildSchema ? ['save', 'validate', 'deleteOne'] : ['save', 'validate'];
for (const method of internalMethodsToWrap) {
const toWrap = method === 'validate' ? '$__originalValidate' : `$__${method}`;
const wrapped = middleware.
createWrapper(method, objToDecorate[toWrap], null, kareemOptions);
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/populate/assignRawDocsToIdStructure.js
Expand Up @@ -7,7 +7,7 @@ const utils = require('../../utils');

module.exports = assignRawDocsToIdStructure;

const kHasArray = Symbol('assignRawDocsToIdStructure.hasArray');
const kHasArray = Symbol('mongoose#assignRawDocsToIdStructure#hasArray');

/**
* Assign `vals` returned by mongo query to the `rawIds`
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/promiseOrCallback.js
Expand Up @@ -2,7 +2,7 @@

const immediate = require('./immediate');

const emittedSymbol = Symbol('mongoose:emitted');
const emittedSymbol = Symbol('mongoose#emitted');

module.exports = function promiseOrCallback(callback, fn, ee, Promise) {
if (typeof callback === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/query/completeMany.js
@@ -1,6 +1,6 @@
'use strict';

const helpers = require('../../queryhelpers');
const helpers = require('../../queryHelpers');

module.exports = completeMany;

Expand Down
4 changes: 1 addition & 3 deletions lib/helpers/query/validOps.js
Expand Up @@ -2,7 +2,6 @@

module.exports = Object.freeze([
// Read
'count',
'countDocuments',
'distinct',
'estimatedDocumentCount',
Expand All @@ -17,6 +16,5 @@ module.exports = Object.freeze([
// Delete
'deleteMany',
'deleteOne',
'findOneAndDelete',
'findOneAndRemove'
'findOneAndDelete'
]);
12 changes: 0 additions & 12 deletions lib/helpers/schema/idGetter.js
Expand Up @@ -16,7 +16,6 @@ module.exports = function addIdGetter(schema) {
return schema;
}
schema.virtual('id').get(idGetter);
schema.virtual('id').set(idSetter);

return schema;
};
Expand All @@ -33,14 +32,3 @@ function idGetter() {

return null;
}

/**
*
* @param {String} v the id to set
* @api private
*/

function idSetter(v) {
this._id = v;
return v;
}
8 changes: 4 additions & 4 deletions lib/helpers/symbols.js
Expand Up @@ -5,16 +5,16 @@ exports.arrayAtomicsSymbol = Symbol('mongoose#Array#_atomics');
exports.arrayParentSymbol = Symbol('mongoose#Array#_parent');
exports.arrayPathSymbol = Symbol('mongoose#Array#_path');
exports.arraySchemaSymbol = Symbol('mongoose#Array#_schema');
exports.documentArrayParent = Symbol('mongoose:documentArrayParent');
exports.documentArrayParent = Symbol('mongoose#documentArrayParent');
exports.documentIsSelected = Symbol('mongoose#Document#isSelected');
exports.documentIsModified = Symbol('mongoose#Document#isModified');
exports.documentModifiedPaths = Symbol('mongoose#Document#modifiedPaths');
exports.documentSchemaSymbol = Symbol('mongoose#Document#schema');
exports.getSymbol = Symbol('mongoose#Document#get');
exports.modelSymbol = Symbol('mongoose#Model');
exports.objectIdSymbol = Symbol('mongoose#ObjectId');
exports.populateModelSymbol = Symbol('mongoose.PopulateOptions#Model');
exports.populateModelSymbol = Symbol('mongoose#PopulateOptions#Model');
exports.schemaTypeSymbol = Symbol('mongoose#schemaType');
exports.sessionNewDocuments = Symbol('mongoose:ClientSession#newDocuments');
exports.sessionNewDocuments = Symbol('mongoose#ClientSession#newDocuments');
exports.scopeSymbol = Symbol('mongoose#Document#scope');
exports.validatorErrorSymbol = Symbol('mongoose:validatorError');
exports.validatorErrorSymbol = Symbol('mongoose#validatorError');

0 comments on commit 8527c8d

Please sign in to comment.