Skip to content

Commit 7b0dba0

Browse files
authored
Merge branch 'master' into vkarpov15/Automatticgh-15071
2 parents 00d0064 + d2a70e3 commit 7b0dba0

File tree

15 files changed

+296
-89
lines changed

15 files changed

+296
-89
lines changed

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ module.exports.Decimal128 = mongoose.Decimal128;
4646
module.exports.Mixed = mongoose.Mixed;
4747
module.exports.Date = mongoose.Date;
4848
module.exports.Number = mongoose.Number;
49-
module.exports.Double = mongoose.Double;
5049
module.exports.Error = mongoose.Error;
5150
module.exports.MongooseError = mongoose.MongooseError;
5251
module.exports.now = mongoose.now;

lib/connection.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Object.defineProperty(Connection.prototype, 'readyState', {
158158
* @api public
159159
*/
160160

161-
Connection.prototype.get = function(key) {
161+
Connection.prototype.get = function getOption(key) {
162162
if (this.config.hasOwnProperty(key)) {
163163
return this.config[key];
164164
}
@@ -186,7 +186,7 @@ Connection.prototype.get = function(key) {
186186
* @api public
187187
*/
188188

189-
Connection.prototype.set = function(key, val) {
189+
Connection.prototype.set = function setOption(key, val) {
190190
if (this.config.hasOwnProperty(key)) {
191191
this.config[key] = val;
192192
return val;
@@ -925,7 +925,7 @@ Connection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
925925
* @api private
926926
*/
927927

928-
Connection.prototype.error = function(err, callback) {
928+
Connection.prototype.error = function error(err, callback) {
929929
if (callback) {
930930
callback(err);
931931
return null;
@@ -939,6 +939,7 @@ Connection.prototype.error = function(err, callback) {
939939
/**
940940
* Called when the connection is opened
941941
*
942+
* @emits "open"
942943
* @api private
943944
*/
944945

@@ -1043,23 +1044,43 @@ Connection.prototype.openUri = async function openUri(uri, options) {
10431044
return this;
10441045
};
10451046

1046-
/*!
1047-
* Treat `on('error')` handlers as handling the initialConnection promise
1048-
* to avoid uncaught exceptions when using `on('error')`. See gh-14377.
1047+
/**
1048+
* Listen to events in the Connection
1049+
*
1050+
* @param {String} event The event to listen on
1051+
* @param {Function} callback
1052+
* @see Connection#readyState https://mongoosejs.com/docs/api/connection.html#Connection.prototype.readyState
1053+
*
1054+
* @method on
1055+
* @instance
1056+
* @memberOf Connection
1057+
* @api public
10491058
*/
10501059

1060+
// Treat `on('error')` handlers as handling the initialConnection promise
1061+
// to avoid uncaught exceptions when using `on('error')`. See gh-14377.
10511062
Connection.prototype.on = function on(event, callback) {
10521063
if (event === 'error' && this.$initialConnection) {
10531064
this.$initialConnection.catch(() => {});
10541065
}
10551066
return EventEmitter.prototype.on.call(this, event, callback);
10561067
};
10571068

1058-
/*!
1059-
* Treat `once('error')` handlers as handling the initialConnection promise
1060-
* to avoid uncaught exceptions when using `on('error')`. See gh-14377.
1069+
/**
1070+
* Listen to a event once in the Connection
1071+
*
1072+
* @param {String} event The event to listen on
1073+
* @param {Function} callback
1074+
* @see Connection#readyState https://mongoosejs.com/docs/api/connection.html#Connection.prototype.readyState
1075+
*
1076+
* @method once
1077+
* @instance
1078+
* @memberOf Connection
1079+
* @api public
10611080
*/
10621081

1082+
// Treat `on('error')` handlers as handling the initialConnection promise
1083+
// to avoid uncaught exceptions when using `on('error')`. See gh-14377.
10631084
Connection.prototype.once = function on(event, callback) {
10641085
if (event === 'error' && this.$initialConnection) {
10651086
this.$initialConnection.catch(() => {});
@@ -1220,17 +1241,18 @@ Connection.prototype._close = async function _close(force, destroy) {
12201241
* @api private
12211242
*/
12221243

1223-
Connection.prototype.doClose = function() {
1244+
Connection.prototype.doClose = function doClose() {
12241245
throw new Error('Connection#doClose unimplemented by driver');
12251246
};
12261247

12271248
/**
12281249
* Called when the connection closes
12291250
*
1251+
* @emits "close"
12301252
* @api private
12311253
*/
12321254

1233-
Connection.prototype.onClose = function(force) {
1255+
Connection.prototype.onClose = function onClose(force) {
12341256
this.readyState = STATES.disconnected;
12351257

12361258
// avoid having the collection subscribe to our event emitter
@@ -1334,7 +1356,7 @@ Connection.prototype.plugin = function(fn, opts) {
13341356
* @api public
13351357
*/
13361358

1337-
Connection.prototype.model = function(name, schema, collection, options) {
1359+
Connection.prototype.model = function model(name, schema, collection, options) {
13381360
if (!(this instanceof Connection)) {
13391361
throw new MongooseError('`connection.model()` should not be run with ' +
13401362
'`new`. If you are doing `new db.model(foo)(bar)`, use ' +
@@ -1454,7 +1476,7 @@ Connection.prototype.model = function(name, schema, collection, options) {
14541476
* @return {Connection} this
14551477
*/
14561478

1457-
Connection.prototype.deleteModel = function(name) {
1479+
Connection.prototype.deleteModel = function deleteModel(name) {
14581480
if (typeof name === 'string') {
14591481
const model = this.model(name);
14601482
if (model == null) {
@@ -1510,7 +1532,7 @@ Connection.prototype.deleteModel = function(name) {
15101532
* @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
15111533
*/
15121534

1513-
Connection.prototype.watch = function(pipeline, options) {
1535+
Connection.prototype.watch = function watch(pipeline, options) {
15141536
const changeStreamThunk = cb => {
15151537
immediate(() => {
15161538
if (this.readyState === STATES.connecting) {
@@ -1559,7 +1581,7 @@ Connection.prototype.asPromise = async function asPromise() {
15591581
* @return {String[]}
15601582
*/
15611583

1562-
Connection.prototype.modelNames = function() {
1584+
Connection.prototype.modelNames = function modelNames() {
15631585
return Object.keys(this.models);
15641586
};
15651587

@@ -1571,7 +1593,7 @@ Connection.prototype.modelNames = function() {
15711593
* @api private
15721594
* @return {Boolean} true if the connection should be authenticated after it is opened, otherwise false.
15731595
*/
1574-
Connection.prototype.shouldAuthenticate = function() {
1596+
Connection.prototype.shouldAuthenticate = function shouldAuthenticate() {
15751597
return this.user != null &&
15761598
(this.pass != null || this.authMechanismDoesNotRequirePassword());
15771599
};
@@ -1584,7 +1606,7 @@ Connection.prototype.shouldAuthenticate = function() {
15841606
* @return {Boolean} true if the authentication mechanism specified in the options object requires
15851607
* a password, otherwise false.
15861608
*/
1587-
Connection.prototype.authMechanismDoesNotRequirePassword = function() {
1609+
Connection.prototype.authMechanismDoesNotRequirePassword = function authMechanismDoesNotRequirePassword() {
15881610
if (this.options && this.options.auth) {
15891611
return noPasswordAuthMechanisms.indexOf(this.options.auth.authMechanism) >= 0;
15901612
}
@@ -1602,7 +1624,7 @@ Connection.prototype.authMechanismDoesNotRequirePassword = function() {
16021624
* @return {Boolean} true if the provided options object provides enough data to authenticate with,
16031625
* otherwise false.
16041626
*/
1605-
Connection.prototype.optionsProvideAuthenticationData = function(options) {
1627+
Connection.prototype.optionsProvideAuthenticationData = function optionsProvideAuthenticationData(options) {
16061628
return (options) &&
16071629
(options.user) &&
16081630
((options.pass) || this.authMechanismDoesNotRequirePassword());

lib/cursor/aggregationCursor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ AggregationCursor.prototype._markError = function(error) {
175175
* Marks this cursor as closed. Will stop streaming and subsequent calls to
176176
* `next()` will error.
177177
*
178-
* @param {Function} callback
179178
* @return {Promise}
180179
* @api public
181180
* @method close
182-
* @emits close
181+
* @emits "close"
183182
* @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
184183
*/
185184

lib/document.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ function init(self, obj, doc, opts, prefix) {
831831
*
832832
* #### Example:
833833
*
834-
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 }, callback);
834+
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 });
835835
*
836836
* #### Valid options:
837837
*
@@ -843,7 +843,6 @@ function init(self, obj, doc, opts, prefix) {
843843
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.lean()) and the [Mongoose lean tutorial](https://mongoosejs.com/docs/tutorials/lean.html).
844844
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
845845
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
846-
* @param {Function} [callback]
847846
* @return {Query}
848847
* @api public
849848
* @memberOf Document
@@ -3444,12 +3443,11 @@ function _checkImmutableSubpaths(subdoc, schematype, priorVal) {
34443443
* @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](https://mongoosejs.com/docs/guide.html#writeConcern).
34453444
* @param {Boolean} [options.checkKeys=true] the MongoDB driver prevents you from saving keys that start with '$' or contain '.' by default. Set this option to `false` to skip that check. See [restrictions on field names](https://www.mongodb.com/docs/manual/reference/limits/#Restrictions-on-Field-Names)
34463445
* @param {Boolean} [options.timestamps=true] if `false` and [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this `save()`.
3447-
* @param {Function} [fn] optional callback
34483446
* @method save
34493447
* @memberOf Document
34503448
* @instance
34513449
* @throws {DocumentNotFoundError} if this [save updates an existing document](https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew()) but the document doesn't exist in the database. For example, you will get this error if the document is [deleted between when you retrieved the document and when you saved it](documents.html#updating).
3452-
* @return {Promise|undefined} Returns undefined if used with callback or a Promise otherwise.
3450+
* @return {Promise}
34533451
* @api public
34543452
* @see middleware https://mongoosejs.com/docs/middleware.html
34553453
*/

lib/drivers/node-mongodb-native/collection.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ function format(obj, sub, color, shell) {
454454
/**
455455
* Retrieves information about this collections indexes.
456456
*
457-
* @param {Function} callback
458457
* @method getIndexes
459458
* @api public
460459
*/

lib/helpers/model/discriminator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
119119
// schema. `Schema.prototype.clone()` copies `obj` by reference, no cloning.
120120
schema.obj = { ...schema.obj };
121121
mergeDiscriminatorSchema(schema, baseSchema);
122+
schema._gatherChildSchemas();
122123

123124
// Clean up conflicting paths _after_ merging re: gh-6076
124125
for (const conflictingPath of conflictingPaths) {

lib/model.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ Model.$where = function $where() {
22312231
/**
22322232
* Issues a mongodb findOneAndUpdate command.
22332233
*
2234-
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes if `callback` is passed else a Query object is returned.
2234+
* Finds a matching document, updates it according to the `update` arg, passing any `options`. A Query object is returned.
22352235
*
22362236
* #### Example:
22372237
*
@@ -3643,7 +3643,11 @@ Model.castObject = function castObject(obj, options) {
36433643
options = options || {};
36443644
const ret = {};
36453645

3646-
const schema = this.schema;
3646+
let schema = this.schema;
3647+
const discriminatorKey = schema.options.discriminatorKey;
3648+
if (schema.discriminators != null && obj != null && obj[discriminatorKey] != null) {
3649+
schema = getSchemaDiscriminatorByValue(schema, obj[discriminatorKey]) || schema;
3650+
}
36473651
const paths = Object.keys(schema.paths);
36483652

36493653
for (const path of paths) {
@@ -3992,7 +3996,7 @@ function _update(model, op, conditions, doc, options) {
39923996
/**
39933997
* Performs [aggregations](https://www.mongodb.com/docs/manual/aggregation/) on the models collection.
39943998
*
3995-
* If a `callback` is passed, the `aggregate` is executed and a `Promise` is returned. If a callback is not passed, the `aggregate` itself is returned.
3999+
* The `aggregate` itself is returned.
39964000
*
39974001
* This function triggers the following middleware.
39984002
*
@@ -4047,10 +4051,6 @@ Model.aggregate = function aggregate(pipeline, options) {
40474051
aggregate.option(options);
40484052
}
40494053

4050-
if (typeof callback === 'undefined') {
4051-
return aggregate;
4052-
}
4053-
40544054
return aggregate;
40554055
};
40564056

@@ -4238,7 +4238,6 @@ Model.validate = async function validate(obj, pathsOrOptions, context) {
42384238
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
42394239
* @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
42404240
* @param {Boolean} [options.forceRepopulate=true] Set to `false` to prevent Mongoose from repopulating paths that are already populated
4241-
* @param {Function} [callback(err,doc)] Optional callback, executed upon completion. Receives `err` and the `doc(s)`.
42424241
* @return {Promise}
42434242
* @api public
42444243
*/

0 commit comments

Comments
 (0)