Skip to content

Commit

Permalink
Merge branch '8.0' into vkarpov15/gh-12748
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored Oct 4, 2023
2 parents 9aa8cdf + 73f9412 commit 0cc0ac3
Show file tree
Hide file tree
Showing 65 changed files with 352 additions and 95 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
'*.min.js',
'**/docs/js/native.js',
'!.*',
'node_modules'
'node_modules',
'.git'
],
overrides: [
{
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
7.5.3 / 2023-09-25
==================
* fix(document): handle MongoDB Long when casting BigInts #13869 #13791
* fix(model): make bulkSave() persist changes that happen in pre('save') middleware #13885 #13799
* fix: handle casting $elemMatch underneath $not underneath another $elemMatch #13893 #13880
* fix(model): make bulkWrite casting respect global setDefaultsOnInsert #13870 #13823
* fix(document): handle default values for discriminator key with embedded discriminators #13891 #13835
* fix: account for null values when assigning isNew property within document array #13883
* types: avoid "interface can only extend object types with statically known members" error in TypeScript 4 #13871
* docs(deprecations): fix typo in includeResultMetadata deprecation docs #13884 #13844
* docs: fix pre element overflow in home page #13868 [ghoshRitesh12](https://github.com/ghoshRitesh12)

7.5.2 / 2023-09-15
==================
* fix(schema): handle number discriminator keys when using Schema.prototype.discriminator() #13858 #13788
* fix: ignore `id` property when calling `set()` with both `id` and `_id` specified to avoid `id` setter overwriting #13762
* types: pass correct document type to required and default function #13851 #13797
* docs(model): add examples of using diffIndexes() to syncIndexes()and diffIndexes() api docs #13850 #13771

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)
Expand Down
1 change: 1 addition & 0 deletions docs/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pre {
background: #eee;
padding: 5px;
border-radius: 3px;
overflow-x: auto;
}
code {
color: #333;
Expand Down
2 changes: 1 addition & 1 deletion docs/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cause any problems for your application. Please [report any issues on GitHub](ht

To fix all deprecation warnings, follow the below steps:

* Replace `rawResult: true` with `includeResultMetadata: false` in `findOneAndUpdate()`, `findOneAndReplace()`, `findOneAndDelete()` calls.
* Replace `rawResult: true` with `includeResultMetadata: true` in `findOneAndUpdate()`, `findOneAndReplace()`, `findOneAndDelete()` calls.

Read below for more a more detailed description of each deprecation warning.

Expand Down
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
18 changes: 16 additions & 2 deletions lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,21 @@ module.exports = function cast(schema, obj, options, context) {
while (k--) {
$cond = ks[k];
nested = val[$cond];

if ($cond === '$not') {
if ($cond === '$elemMatch') {
if (nested && schematype != null && schematype.schema != null) {
cast(schematype.schema, nested, options, context);
} else if (nested && schematype != null && schematype.$isMongooseArray) {
if (utils.isPOJO(nested) && nested.$not != null) {
cast(schema, nested, options, context);
} else {
val[$cond] = schematype.castForQuery(
$cond,
nested,
context
);
}
}
} else if ($cond === '$not') {
if (nested && schematype) {
_keys = Object.keys(nested);
if (_keys.length && isOperator(_keys[0])) {
Expand All @@ -337,6 +350,7 @@ module.exports = function cast(schema, obj, options, context) {
context
);
}

}
}
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
Expand Down
5 changes: 5 additions & 0 deletions lib/cast/bigint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const assert = require('assert');
const { Long } = require('bson');

/**
* Given a value, cast it to a BigInt, or throw an `Error` if the value
Expand All @@ -23,6 +24,10 @@ module.exports = function castBigInt(val) {
return val;
}

if (val instanceof Long) {
return val.toBigInt();
}

if (typeof val === 'string' || typeof val === 'number') {
return BigInt(val);
}
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.
File renamed without changes.
12 changes: 8 additions & 4 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 @@ -1667,7 +1667,11 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
val[arrayAtomicsSymbol] = priorVal[arrayAtomicsSymbol];
val[arrayAtomicsBackupSymbol] = priorVal[arrayAtomicsBackupSymbol];
if (utils.isMongooseDocumentArray(val)) {
val.forEach(doc => { doc.isNew = false; });
val.forEach(doc => {
if (doc != null) {
doc.$isNew = false;
}
});
}
}

Expand Down Expand Up @@ -3529,7 +3533,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 @@ -3558,7 +3562,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/drivers/node-mongodb-native/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function _setClient(conn, client, options, dbName) {
client.s.options.hosts &&
client.s.options.hosts[0] &&
client.s.options.hosts[0].port || void 0;
conn.name = dbName != null ? dbName : client && client.s && client.s.options && client.s.options.dbName || void 0;
conn.name = dbName != null ? dbName : db.databaseName;
conn._closeCalled = client._closeCalled;

const _handleReconnect = () => {
Expand Down
17 changes: 10 additions & 7 deletions lib/helpers/discriminator/getConstructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ const getDiscriminatorByValue = require('./getDiscriminatorByValue');
* @api private
*/

module.exports = function getConstructor(Constructor, value) {
module.exports = function getConstructor(Constructor, value, defaultDiscriminatorValue) {
const discriminatorKey = Constructor.schema.options.discriminatorKey;
if (value != null &&
Constructor.discriminators &&
value[discriminatorKey] != null) {
if (Constructor.discriminators[value[discriminatorKey]]) {
Constructor = Constructor.discriminators[value[discriminatorKey]];
let discriminatorValue = (value != null && value[discriminatorKey]);
if (discriminatorValue == null) {
discriminatorValue = defaultDiscriminatorValue;
}
if (Constructor.discriminators &&
discriminatorValue != null) {
if (Constructor.discriminators[discriminatorValue]) {
Constructor = Constructor.discriminators[discriminatorValue];
} else {
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, value[discriminatorKey]);
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, discriminatorValue);
if (constructorByValue) {
Constructor = constructorByValue;
}
Expand Down
13 changes: 11 additions & 2 deletions lib/helpers/model/castBulkWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const setDefaultsOnInsert = require('../setDefaultsOnInsert');

module.exports = function castBulkWrite(originalModel, op, options) {
const now = originalModel.base.now();

const globalSetDefaultsOnInsert = originalModel.base.options.setDefaultsOnInsert;
if (op['insertOne']) {
return (callback) => {
const model = decideModelByObject(originalModel, op['insertOne']['document']);
Expand Down Expand Up @@ -69,7 +71,10 @@ module.exports = function castBulkWrite(originalModel, op, options) {
applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
}

if (op['updateOne'].setDefaultsOnInsert !== false) {
const shouldSetDefaultsOnInsert = op['updateOne'].setDefaultsOnInsert == null ?
globalSetDefaultsOnInsert :
op['updateOne'].setDefaultsOnInsert;
if (shouldSetDefaultsOnInsert !== false) {
setDefaultsOnInsert(op['updateOne']['filter'], model.schema, op['updateOne']['update'], {
setDefaultsOnInsert: true,
upsert: op['updateOne'].upsert
Expand Down Expand Up @@ -106,7 +111,11 @@ module.exports = function castBulkWrite(originalModel, op, options) {
const schema = model.schema;
const strict = options.strict != null ? options.strict : model.schema.options.strict;

if (op['updateMany'].setDefaultsOnInsert !== false) {
const shouldSetDefaultsOnInsert = op['updateMany'].setDefaultsOnInsert == null ?
globalSetDefaultsOnInsert :
op['updateMany'].setDefaultsOnInsert;

if (shouldSetDefaultsOnInsert !== false) {
setDefaultsOnInsert(op['updateMany']['filter'], model.schema, op['updateMany']['update'], {
setDefaultsOnInsert: true,
upsert: op['updateMany'].upsert
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.
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,8 @@ Mongoose.prototype._model = function(name, schema, collection, options) {
connection.emit('model', model);

if (schema._applyDiscriminators != null) {
for (const disc of Object.keys(schema._applyDiscriminators)) {
model.discriminator(disc, schema._applyDiscriminators[disc]);
for (const disc of schema._applyDiscriminators.keys()) {
model.discriminator(disc, schema._applyDiscriminators.get(disc));
}
}

Expand Down Expand Up @@ -1172,7 +1172,7 @@ Mongoose.prototype.CastError = require('./error/cast');
* @api public
*/

Mongoose.prototype.SchemaTypeOptions = require('./options/SchemaTypeOptions');
Mongoose.prototype.SchemaTypeOptions = require('./options/schemaTypeOptions');

/**
* The [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver Mongoose uses.
Expand Down
8 changes: 4 additions & 4 deletions 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 Expand Up @@ -3464,11 +3464,9 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
* @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
*
*/
Model.bulkSave = async function(documents, options) {
Model.bulkSave = async function bulkSave(documents, options) {
options = options || {};

const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps });

if (options.timestamps != null) {
for (const document of documents) {
document.$__.saveOptions = document.$__.saveOptions || {};
Expand All @@ -3485,6 +3483,8 @@ Model.bulkSave = async function(documents, options) {

await Promise.all(documents.map(buildPreSavePromise));

const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps });

const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, options).then(
(res) => ({ bulkWriteResult: res, bulkWriteError: null }),
(err) => ({ bulkWriteResult: null, bulkWriteError: err })
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on an Array schematype.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on a Buffer schematype.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on a Date schematype.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on an Document Array schematype.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on a Map schematype.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on a Number schematype.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');
const SchemaTypeOptions = require('./schemaTypeOptions');

/**
* The options defined on an ObjectId schematype.
Expand Down
Loading

0 comments on commit 0cc0ac3

Please sign in to comment.