Skip to content

Commit 3508f5f

Browse files
authored
Merge pull request #14362 from Automattic/8.2
8.2
2 parents 7732ce2 + b10fc72 commit 3508f5f

23 files changed

+571
-210
lines changed

docs/middleware.md

+4
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ Model middleware is supported for the following model functions.
6767
Don't confuse model middleware and document middleware: model middleware hooks into *static* functions on a `Model` class, document middleware hooks into *methods* on a `Model` class.
6868
In model middleware functions, `this` refers to the model.
6969

70+
* [bulkWrite](api/model.html#model_Model-bulkWrite)
71+
* [createCollection](api/model.html#model_Model-createCollection)
7072
* [insertMany](api/model.html#model_Model-insertMany)
7173

7274
Here are the possible strings that can be passed to `pre()`
7375

7476
* aggregate
77+
* bulkWrite
7578
* count
7679
* countDocuments
80+
* createCollection
7781
* deleteOne
7882
* deleteMany
7983
* estimatedDocumentCount

lib/aggregate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1019,8 +1019,8 @@ Aggregate.prototype.exec = async function exec() {
10191019
const model = this._model;
10201020
const collection = this._model.collection;
10211021

1022-
applyGlobalMaxTimeMS(this.options, model);
1023-
applyGlobalDiskUse(this.options, model);
1022+
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
1023+
applyGlobalDiskUse(this.options, model.db.options, model.base.options);
10241024

10251025
if (this.options && this.options.cursor) {
10261026
return new AggregationCursor(this);

lib/connection.js

+22
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,28 @@ Connection.prototype.createCollections = async function createCollections(option
443443
return result;
444444
};
445445

446+
/**
447+
* A convenience wrapper for `connection.client.withSession()`.
448+
*
449+
* #### Example:
450+
*
451+
* await conn.withSession(async session => {
452+
* const doc = await TestModel.findOne().session(session);
453+
* });
454+
*
455+
* @method withSession
456+
* @param {Function} executor called with 1 argument: a `ClientSession` instance
457+
* @return {Promise} resolves to the return value of the executor function
458+
* @api public
459+
*/
460+
461+
Connection.prototype.withSession = async function withSession(executor) {
462+
if (arguments.length === 0) {
463+
throw new Error('Please provide an executor function');
464+
}
465+
return await this.client.withSession(executor);
466+
};
467+
446468
/**
447469
* _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions)
448470
* for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),

lib/constants.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
/*!
4+
* ignore
5+
*/
6+
7+
const queryOperations = Object.freeze([
8+
// Read
9+
'countDocuments',
10+
'distinct',
11+
'estimatedDocumentCount',
12+
'find',
13+
'findOne',
14+
// Update
15+
'findOneAndReplace',
16+
'findOneAndUpdate',
17+
'replaceOne',
18+
'updateMany',
19+
'updateOne',
20+
// Delete
21+
'deleteMany',
22+
'deleteOne',
23+
'findOneAndDelete'
24+
]);
25+
26+
exports.queryOperations = queryOperations;
27+
28+
/*!
29+
* ignore
30+
*/
31+
32+
const queryMiddlewareFunctions = queryOperations.concat([
33+
'validate'
34+
]);
35+
36+
exports.queryMiddlewareFunctions = queryMiddlewareFunctions;

lib/document.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ Document.prototype.$__init = function(doc, opts) {
694694
init(this, doc, this._doc, opts);
695695

696696
markArraySubdocsPopulated(this, opts.populated);
697-
698697
this.$emit('init', this);
699698
this.constructor.emit('init', this);
700699

@@ -703,7 +702,6 @@ Document.prototype.$__init = function(doc, opts) {
703702
null;
704703

705704
applyDefaults(this, this.$__.selected, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);
706-
707705
return this;
708706
};
709707

@@ -746,7 +744,6 @@ function init(self, obj, doc, opts, prefix) {
746744
}
747745
path = prefix + i;
748746
schemaType = docSchema.path(path);
749-
750747
// Should still work if not a model-level discriminator, but should not be
751748
// necessary. This is *only* to catch the case where we queried using the
752749
// base model and the discriminated model has a projection
@@ -770,15 +767,14 @@ function init(self, obj, doc, opts, prefix) {
770767
}
771768
} else {
772769
// Retain order when overwriting defaults
773-
if (doc.hasOwnProperty(i) && obj[i] !== void 0) {
770+
if (doc.hasOwnProperty(i) && obj[i] !== void 0 && !opts.hydratedPopulatedDocs) {
774771
delete doc[i];
775772
}
776773
if (obj[i] === null) {
777774
doc[i] = schemaType._castNullish(null);
778775
} else if (obj[i] !== undefined) {
779776
const wasPopulated = obj[i].$__ == null ? null : obj[i].$__.wasPopulated;
780-
781-
if (schemaType && !wasPopulated) {
777+
if ((schemaType && !wasPopulated) && !opts.hydratedPopulatedDocs) {
782778
try {
783779
if (opts && opts.setters) {
784780
// Call applySetters with `init = false` because otherwise setters are a noop

lib/helpers/model/applyStaticHooks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const middlewareFunctions = require('../query/applyQueryMiddleware').middlewareFunctions;
3+
const middlewareFunctions = require('../../constants').queryMiddlewareFunctions;
44
const promiseOrCallback = require('../promiseOrCallback');
55

66
module.exports = function applyStaticHooks(model, hooks, statics) {

lib/helpers/query/applyGlobalOption.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
const utils = require('../../utils');
44

5-
function applyGlobalMaxTimeMS(options, model) {
6-
applyGlobalOption(options, model, 'maxTimeMS');
5+
function applyGlobalMaxTimeMS(options, connectionOptions, baseOptions) {
6+
applyGlobalOption(options, connectionOptions, baseOptions, 'maxTimeMS');
77
}
88

9-
function applyGlobalDiskUse(options, model) {
10-
applyGlobalOption(options, model, 'allowDiskUse');
9+
function applyGlobalDiskUse(options, connectionOptions, baseOptions) {
10+
applyGlobalOption(options, connectionOptions, baseOptions, 'allowDiskUse');
1111
}
1212

1313
module.exports = {
@@ -16,14 +16,14 @@ module.exports = {
1616
};
1717

1818

19-
function applyGlobalOption(options, model, optionName) {
19+
function applyGlobalOption(options, connectionOptions, baseOptions, optionName) {
2020
if (utils.hasUserDefinedProperty(options, optionName)) {
2121
return;
2222
}
2323

24-
if (utils.hasUserDefinedProperty(model.db.options, optionName)) {
25-
options[optionName] = model.db.options[optionName];
26-
} else if (utils.hasUserDefinedProperty(model.base.options, optionName)) {
27-
options[optionName] = model.base.options[optionName];
24+
if (utils.hasUserDefinedProperty(connectionOptions, optionName)) {
25+
options[optionName] = connectionOptions[optionName];
26+
} else if (utils.hasUserDefinedProperty(baseOptions, optionName)) {
27+
options[optionName] = baseOptions[optionName];
2828
}
2929
}

lib/helpers/query/applyQueryMiddleware.js

-54
This file was deleted.

lib/helpers/query/castFilterPath.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
const isOperator = require('./isOperator');
44

5-
module.exports = function castFilterPath(query, schematype, val) {
6-
const ctx = query;
5+
module.exports = function castFilterPath(ctx, schematype, val) {
76
const any$conditionals = Object.keys(val).some(isOperator);
87

98
if (!any$conditionals) {

lib/helpers/query/completeMany.js

-36
This file was deleted.

lib/helpers/query/validOps.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
11
'use strict';
22

3-
module.exports = Object.freeze([
4-
// Read
5-
'countDocuments',
6-
'distinct',
7-
'estimatedDocumentCount',
8-
'find',
9-
'findOne',
10-
// Update
11-
'findOneAndReplace',
12-
'findOneAndUpdate',
13-
'replaceOne',
14-
'updateMany',
15-
'updateOne',
16-
// Delete
17-
'deleteMany',
18-
'deleteOne',
19-
'findOneAndDelete'
20-
]);
3+
module.exports = require('../../constants').queryMiddlewareFunctions;

0 commit comments

Comments
 (0)