Skip to content

Commit

Permalink
Merge branch '8.0' into vkarpov15/gh-4292-2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Oct 4, 2023
2 parents d808a54 + b8d40f1 commit 6e8e06b
Show file tree
Hide file tree
Showing 76 changed files with 350 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-20.04
name: Benchmark TypeScript Types
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
with:
fetch-depth: 0
- name: Setup node
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest
name: Lint Markdown files
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
Expand All @@ -48,7 +48,7 @@ jobs:
runs-on: ubuntu-20.04
name: Test Generating Docs
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- run: git fetch --depth=1 --tags # download all tags for documentation

- name: Setup node
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
name: Lint JS-Files
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
MONGOMS_PREFER_GLOBAL_PATH: 1
FORCE_COLOR: true
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
MONGOMS_PREFER_GLOBAL_PATH: 1
FORCE_COLOR: true
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
Expand Down Expand Up @@ -122,7 +122,7 @@ jobs:
env:
FORCE_COLOR: true
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
Expand All @@ -139,6 +139,6 @@ jobs:
contents: read
steps:
- name: Check out repo
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Dependency review
uses: actions/dependency-review-action@v3
2 changes: 1 addition & 1 deletion .github/workflows/tidelift-alignment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
if: github.repository == 'Automattic/mongoose'
steps:
- name: Checkout
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tsd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
name: Lint TS-Files
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
Expand All @@ -40,7 +40,7 @@ jobs:
runs-on: ubuntu-latest
name: Test Typescript Types
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Setup node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
7.5.4 / 2023-10-04
==================
* fix: avoid stripping out `id` property when `_id` is set #13933 #13892 #13867
* fix(QueryCursor): avoid double-applying schema paths so you can include select: false fields with + projection using cursors #13932 #13773
* fix(query): allow deselecting discriminator key using - syntax #13929 #13760
* fix(query): handle $round in $expr as array #13928 #13881
* fix(document): call pre('validate') hooks when modifying a path underneath triply nested subdoc #13912 #13876
* fix(mongoose): correctly handle global applyPluginsToChildSchemas option #13911 #13887
* types: add insertMany array overload with options #13931 [t1bb4r](https://github.com/t1bb4r)
* docs(compatibility): add Mongoose 7 support to compatibility matrix #13875
* docs: amend some awkward FAQ wording #13925 [peteboere](https://github.com/peteboere)

7.5.3 / 2023-09-25
==================
* fix(document): handle MongoDB Long when casting BigInts #13869 #13791
Expand Down
1 change: 1 addition & 0 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Below are the [semver](http://semver.org/) ranges representing which versions of

| MongoDB Server | Mongoose |
| :------------: | :---------------------------: |
| `7.x` | `^7.4.0` |
| `6.x` | `^6.5.0 \| ^7.0.0` |
| `5.x` | `^6.0.0 \| ^7.0.0` |
| `4.4.x` | `^5.10.0 \| ^6.0.0 \| ^7.0.0` |
Expand Down
2 changes: 1 addition & 1 deletion docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ is undefined on the underlying [POJO](guide.html#minimize).

<a class="anchor" href="#arrow-functions">**Q**</a>. I'm using an arrow function for a [virtual](guide.html#virtuals), [middleware](middleware.html), [getter](api/schematype.html#schematype_SchemaType-get)/[setter](api/schematype.html#schematype_SchemaType-set), or [method](guide.html#methods) and the value of `this` is wrong.

**A**. Arrow functions [handle the `this` keyword much differently than conventional functions](https://masteringjs.io/tutorials/fundamentals/arrow#why-not-arrow-functions).
**A**. Arrow functions [handle the `this` keyword differently than conventional functions](https://masteringjs.io/tutorials/fundamentals/arrow#why-not-arrow-functions).
Mongoose getters/setters depend on `this` to give you access to the document that you're writing to, but this functionality does not work with arrow functions. Do **not** use arrow functions for mongoose getters/setters unless do not intend to access the document in the getter/setter.

```javascript
Expand Down
19 changes: 18 additions & 1 deletion docs/migrating_to_8.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ If you're still on Mongoose 6.x or earlier, please read the [Mongoose 6.x to 7.x
* [MongoDB Node Driver 6.0](#mongodb-node-driver-6)
* [Removed `findOneAndRemove()`](#removed-findoneandremove)
* [Removed id Setter](#removed-id-setter)
* [Allow `null` For Optional Fields in TypeScript](#allow-null-for-optional-fields-in-typescript)

<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>

Expand Down Expand Up @@ -59,4 +60,20 @@ 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.
In Mongoose 8, that setter is now removed.

<h2 id="allow-null-for-optional-fields-in-typescript"><a href="#allow-null-for-optional-fields-in-typescript">Allow <code>null</code> For Optional Fields in TypeScript</a></h2>

In Mongoose 8, automatically inferred schema types in TypeScript allow `null` for optional fields.
In Mongoose 7, optional fields only allowed `undefined`, not `null`.

```typescript
const schema = new Schema({ name: String });
const TestModel = model('Test', schema);

const doc = new TestModel();

// In Mongoose 8, this type is `string | null | undefined`.
// In Mongoose 7, this type is `string | undefined`
doc.name;
```
22 changes: 11 additions & 11 deletions docs/source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const files = [
'lib/document.js',
'lib/model.js',
'lib/query.js',
'lib/cursor/QueryCursor.js',
'lib/cursor/queryCursor.js',
'lib/aggregate.js',
'lib/cursor/AggregationCursor.js',
'lib/cursor/aggregationCursor.js',
'lib/schemaType.js',
'lib/virtualType.js',
'lib/error/index.js',
Expand All @@ -29,16 +29,16 @@ const files = [
'lib/schema/number.js',
'lib/schema/objectId.js',
'lib/schema/string.js',
'lib/options/SchemaTypeOptions.js',
'lib/options/SchemaArrayOptions.js',
'lib/options/SchemaBufferOptions.js',
'lib/options/SchemaDateOptions.js',
'lib/options/SchemaNumberOptions.js',
'lib/options/SchemaObjectIdOptions.js',
'lib/options/SchemaStringOptions.js',
'lib/types/DocumentArray/methods/index.js',
'lib/options/schemaTypeOptions.js',
'lib/options/schemaArrayOptions.js',
'lib/options/schemaBufferOptions.js',
'lib/options/schemaDateOptions.js',
'lib/options/schemaNumberOptions.js',
'lib/options/schemaObjectIdOptions.js',
'lib/options/schemaStringOptions.js',
'lib/types/documentArray/methods/index.js',
'lib/types/subdocument.js',
'lib/types/ArraySubdocument.js',
'lib/types/arraySubdocument.js',
'lib/types/buffer.js',
'lib/types/decimal128.js',
'lib/types/map.js',
Expand Down
2 changes: 1 addition & 1 deletion lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies
*/

const AggregationCursor = require('./cursor/AggregationCursor');
const AggregationCursor = require('./cursor/aggregationCursor');
const MongooseError = require('./error/mongooseError');
const Query = require('./query');
const { applyGlobalMaxTimeMS, applyGlobalDiskUse } = require('./helpers/query/applyGlobalOption');
Expand Down
2 changes: 1 addition & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const ChangeStream = require('./cursor/ChangeStream');
const ChangeStream = require('./cursor/changeStream');
const EventEmitter = require('events').EventEmitter;
const Schema = require('./schema');
const STATES = require('./connectionState');
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 5 additions & 7 deletions lib/cursor/QueryCursor.js → lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const util = require('util');
* @api public
*/

function QueryCursor(query, options) {
function QueryCursor(query) {
// set autoDestroy=true because on node 12 it's by default false
// gh-10902 need autoDestroy to destroy correctly and emit 'close' event
Readable.call(this, { autoDestroy: true, objectMode: true });
Expand All @@ -46,7 +46,7 @@ function QueryCursor(query, options) {
this._mongooseOptions = {};
this._transforms = [];
this.model = model;
this.options = options || {};
this.options = {};
model.hooks.execPre('find', query, (err) => {
if (err != null) {
if (err instanceof kareem.skipWrappedFunction) {
Expand All @@ -70,20 +70,18 @@ function QueryCursor(query, options) {
this.listeners('error').length > 0 && this.emit('error', err);
return;
}
Object.assign(this.options, query._optionsForExec());
this._transforms = this._transforms.concat(query._transforms.slice());
if (this.options.transform) {
this._transforms.push(options.transform);
this._transforms.push(this.options.transform);
}
// Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
// `batchSize` option doesn't work.
if (this.options.batchSize) {
this.options.cursor = options.cursor || {};
this.options.cursor.batchSize = options.batchSize;

// Max out the number of documents we'll populate in parallel at 5000.
this.options._populateBatchSize = Math.min(this.options.batchSize, 5000);
}
Object.assign(this.options, query._optionsForExec());

if (model.collection._shouldBufferCommands() && model.collection.buffer) {
model.collection.queue.push([
() => _getRawCursor(query, this)
Expand Down
17 changes: 12 additions & 5 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru
*/

Document.prototype.$__set = function(pathToMark, path, options, constructing, parts, schema, val, priorVal) {
Embedded = Embedded || require('./types/ArraySubdocument');
Embedded = Embedded || require('./types/arraySubdocument');

const shouldModify = this.$__shouldModify(pathToMark, path, options, constructing, parts,
schema, val, priorVal);
Expand Down Expand Up @@ -1691,6 +1691,13 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
if (last) {
if (obj instanceof Map) {
obj.set(parts[i], val);
} else if (obj.$isSingleNested) {
if (!(parts[i] in obj)) {
obj[parts[i]] = val;
obj._doc[parts[i]] = val;
} else {
obj._doc[parts[i]] = val;
}
} else {
obj[parts[i]] = val;
}
Expand All @@ -1702,7 +1709,7 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
} else if (value && value instanceof Embedded) {
obj = value;
} else if (value && !Array.isArray(value) && value.$isSingleNested) {
obj = value._doc;
obj = value;
} else if (value && Array.isArray(value)) {
obj = value;
} else if (value == null) {
Expand Down Expand Up @@ -2647,7 +2654,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
const modifiedPaths = doc.modifiedPaths();
for (const subdoc of subdocs) {
if (subdoc.$basePath) {
const fullPathToSubdoc = subdoc.$__fullPathWithIndexes();
const fullPathToSubdoc = subdoc.$isSingleNested ? subdoc.$__pathRelativeToParent() : subdoc.$__fullPathWithIndexes();

// Remove child paths for now, because we'll be validating the whole
// subdoc.
Expand Down Expand Up @@ -3533,7 +3540,7 @@ Document.prototype.$__setSchema = function(schema) {
*/

Document.prototype.$__getArrayPathsToValidate = function() {
DocumentArray || (DocumentArray = require('./types/DocumentArray'));
DocumentArray || (DocumentArray = require('./types/documentArray'));

// validate all document arrays.
return this.$__.activePaths
Expand Down Expand Up @@ -3562,7 +3569,7 @@ Document.prototype.$__getArrayPathsToValidate = function() {
*/

Document.prototype.$getAllSubdocs = function() {
DocumentArray || (DocumentArray = require('./types/DocumentArray'));
DocumentArray || (DocumentArray = require('./types/documentArray'));
Embedded = Embedded || require('./types/ArraySubdocument');

function docReducer(doc, seed, path) {
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/populate/assignVals.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const MongooseMap = require('../../types/map');
const SkipPopulateValue = require('./SkipPopulateValue');
const SkipPopulateValue = require('./skipPopulateValue');
const assignRawDocsToIdStructure = require('./assignRawDocsToIdStructure');
const get = require('../get');
const getVirtual = require('./getVirtual');
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/populate/createPopulateQueryFilter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SkipPopulateValue = require('./SkipPopulateValue');
const SkipPopulateValue = require('./skipPopulateValue');
const parentPaths = require('../path/parentPaths');
const { trusted } = require('../query/trusted');
const hasDollarKeys = require('../query/hasDollarKeys');
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const MongooseError = require('../../error/index');
const SkipPopulateValue = require('./SkipPopulateValue');
const SkipPopulateValue = require('./skipPopulateValue');
const clone = require('../clone');
const get = require('../get');
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
Expand Down
File renamed without changes.
8 changes: 7 additions & 1 deletion lib/helpers/query/cast$expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const arithmeticOperatorNumber = new Set([
'$floor',
'$ln',
'$log10',
'$round',
'$sqrt',
'$sin',
'$cos',
Expand Down Expand Up @@ -118,6 +117,13 @@ function _castExpression(val, schema, strictQuery) {
if (val.$size) {
val.$size = castNumberOperator(val.$size, schema, strictQuery);
}
if (val.$round) {
const $round = val.$round;
if (!Array.isArray($round) || $round.length < 1 || $round.length > 2) {
throw new CastError('Array', $round, '$round');
}
val.$round = $round.map(v => castNumberOperator(v, schema, strictQuery));
}

_omitUndefined(val);

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const mongoose = require('./mongoose');

mongoose.Mongoose.prototype.mongo = require('mongodb');

module.exports = mongoose;
module.exports = mongoose;
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const Aggregate = require('./aggregate');
const ChangeStream = require('./cursor/ChangeStream');
const ChangeStream = require('./cursor/changeStream');
const Document = require('./document');
const DocumentNotFoundError = require('./error/notFound');
const DivergentArrayError = require('./error/divergentArray');
Expand Down
Loading

0 comments on commit 6e8e06b

Please sign in to comment.