Skip to content

Commit

Permalink
Merge pull request #14362 from Automattic/8.2
Browse files Browse the repository at this point in the history
8.2
  • Loading branch information
vkarpov15 authored Feb 22, 2024
2 parents 7732ce2 + b10fc72 commit 3508f5f
Show file tree
Hide file tree
Showing 23 changed files with 571 additions and 210 deletions.
4 changes: 4 additions & 0 deletions docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ Model middleware is supported for the following model functions.
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.
In model middleware functions, `this` refers to the model.

* [bulkWrite](api/model.html#model_Model-bulkWrite)
* [createCollection](api/model.html#model_Model-createCollection)
* [insertMany](api/model.html#model_Model-insertMany)

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

* aggregate
* bulkWrite
* count
* countDocuments
* createCollection
* deleteOne
* deleteMany
* estimatedDocumentCount
Expand Down
4 changes: 2 additions & 2 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,8 @@ Aggregate.prototype.exec = async function exec() {
const model = this._model;
const collection = this._model.collection;

applyGlobalMaxTimeMS(this.options, model);
applyGlobalDiskUse(this.options, model);
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
applyGlobalDiskUse(this.options, model.db.options, model.base.options);

if (this.options && this.options.cursor) {
return new AggregationCursor(this);
Expand Down
22 changes: 22 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,28 @@ Connection.prototype.createCollections = async function createCollections(option
return result;
};

/**
* A convenience wrapper for `connection.client.withSession()`.
*
* #### Example:
*
* await conn.withSession(async session => {
* const doc = await TestModel.findOne().session(session);
* });
*
* @method withSession
* @param {Function} executor called with 1 argument: a `ClientSession` instance
* @return {Promise} resolves to the return value of the executor function
* @api public
*/

Connection.prototype.withSession = async function withSession(executor) {
if (arguments.length === 0) {
throw new Error('Please provide an executor function');
}
return await this.client.withSession(executor);
};

/**
* _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions)
* for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),
Expand Down
36 changes: 36 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

/*!
* ignore
*/

const queryOperations = Object.freeze([
// Read
'countDocuments',
'distinct',
'estimatedDocumentCount',
'find',
'findOne',
// Update
'findOneAndReplace',
'findOneAndUpdate',
'replaceOne',
'updateMany',
'updateOne',
// Delete
'deleteMany',
'deleteOne',
'findOneAndDelete'
]);

exports.queryOperations = queryOperations;

/*!
* ignore
*/

const queryMiddlewareFunctions = queryOperations.concat([
'validate'
]);

exports.queryMiddlewareFunctions = queryMiddlewareFunctions;
8 changes: 2 additions & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ Document.prototype.$__init = function(doc, opts) {
init(this, doc, this._doc, opts);

markArraySubdocsPopulated(this, opts.populated);

this.$emit('init', this);
this.constructor.emit('init', this);

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

applyDefaults(this, this.$__.selected, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);

return this;
};

Expand Down Expand Up @@ -746,7 +744,6 @@ function init(self, obj, doc, opts, prefix) {
}
path = prefix + i;
schemaType = docSchema.path(path);

// Should still work if not a model-level discriminator, but should not be
// necessary. This is *only* to catch the case where we queried using the
// base model and the discriminated model has a projection
Expand All @@ -770,15 +767,14 @@ function init(self, obj, doc, opts, prefix) {
}
} else {
// Retain order when overwriting defaults
if (doc.hasOwnProperty(i) && obj[i] !== void 0) {
if (doc.hasOwnProperty(i) && obj[i] !== void 0 && !opts.hydratedPopulatedDocs) {
delete doc[i];
}
if (obj[i] === null) {
doc[i] = schemaType._castNullish(null);
} else if (obj[i] !== undefined) {
const wasPopulated = obj[i].$__ == null ? null : obj[i].$__.wasPopulated;

if (schemaType && !wasPopulated) {
if ((schemaType && !wasPopulated) && !opts.hydratedPopulatedDocs) {
try {
if (opts && opts.setters) {
// Call applySetters with `init = false` because otherwise setters are a noop
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/model/applyStaticHooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const middlewareFunctions = require('../query/applyQueryMiddleware').middlewareFunctions;
const middlewareFunctions = require('../../constants').queryMiddlewareFunctions;
const promiseOrCallback = require('../promiseOrCallback');

module.exports = function applyStaticHooks(model, hooks, statics) {
Expand Down
18 changes: 9 additions & 9 deletions lib/helpers/query/applyGlobalOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

function applyGlobalMaxTimeMS(options, model) {
applyGlobalOption(options, model, 'maxTimeMS');
function applyGlobalMaxTimeMS(options, connectionOptions, baseOptions) {
applyGlobalOption(options, connectionOptions, baseOptions, 'maxTimeMS');
}

function applyGlobalDiskUse(options, model) {
applyGlobalOption(options, model, 'allowDiskUse');
function applyGlobalDiskUse(options, connectionOptions, baseOptions) {
applyGlobalOption(options, connectionOptions, baseOptions, 'allowDiskUse');
}

module.exports = {
Expand All @@ -16,14 +16,14 @@ module.exports = {
};


function applyGlobalOption(options, model, optionName) {
function applyGlobalOption(options, connectionOptions, baseOptions, optionName) {
if (utils.hasUserDefinedProperty(options, optionName)) {
return;
}

if (utils.hasUserDefinedProperty(model.db.options, optionName)) {
options[optionName] = model.db.options[optionName];
} else if (utils.hasUserDefinedProperty(model.base.options, optionName)) {
options[optionName] = model.base.options[optionName];
if (utils.hasUserDefinedProperty(connectionOptions, optionName)) {
options[optionName] = connectionOptions[optionName];
} else if (utils.hasUserDefinedProperty(baseOptions, optionName)) {
options[optionName] = baseOptions[optionName];
}
}
54 changes: 0 additions & 54 deletions lib/helpers/query/applyQueryMiddleware.js

This file was deleted.

3 changes: 1 addition & 2 deletions lib/helpers/query/castFilterPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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

module.exports = function castFilterPath(query, schematype, val) {
const ctx = query;
module.exports = function castFilterPath(ctx, schematype, val) {
const any$conditionals = Object.keys(val).some(isOperator);

if (!any$conditionals) {
Expand Down
36 changes: 0 additions & 36 deletions lib/helpers/query/completeMany.js

This file was deleted.

19 changes: 1 addition & 18 deletions lib/helpers/query/validOps.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
'use strict';

module.exports = Object.freeze([
// Read
'countDocuments',
'distinct',
'estimatedDocumentCount',
'find',
'findOne',
// Update
'findOneAndReplace',
'findOneAndUpdate',
'replaceOne',
'updateMany',
'updateOne',
// Delete
'deleteMany',
'deleteOne',
'findOneAndDelete'
]);
module.exports = require('../../constants').queryMiddlewareFunctions;
Loading

0 comments on commit 3508f5f

Please sign in to comment.