Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.2 #14362

Merged
merged 32 commits into from
Feb 22, 2024
Merged

8.2 #14362

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c480bd6
feat(model): add `recompileSchema()` function to models to allow appl…
vkarpov15 Jan 29, 2024
574cca0
Update lib/model.js
vkarpov15 Feb 5, 2024
6bd5c07
docs(model): correct return type for `recompileSchema()` re: code rev…
vkarpov15 Feb 5, 2024
d30f557
Merge branch 'vkarpov15/gh-14296' of github.com:Automattic/mongoose i…
vkarpov15 Feb 5, 2024
d18878b
types(model): add recompileSchema to TypeScript types
vkarpov15 Feb 5, 2024
e332479
docs: add tsdoc for recompileSchema()
vkarpov15 Feb 6, 2024
f696543
Merge pull request #14306 from Automattic/vkarpov15/gh-14296
vkarpov15 Feb 6, 2024
dc9fd61
begin `withSession`
IslandRhythms Feb 7, 2024
8e1f5ef
fix: lint
IslandRhythms Feb 7, 2024
9f0b13e
Update connection.test.js
IslandRhythms Feb 7, 2024
a1a5c5a
remove unused var
IslandRhythms Feb 7, 2024
579ca48
make opts optional
IslandRhythms Feb 7, 2024
99ad38e
Merge branch 'master' into 8.2
vkarpov15 Feb 8, 2024
12e1224
fix: quick fixes for withSession
vkarpov15 Feb 8, 2024
ef5e09e
types(connection): add withSession()
vkarpov15 Feb 8, 2024
47fad95
Merge pull request #14339 from Automattic/IslandRhythms/gh-14330
vkarpov15 Feb 8, 2024
491be54
Update model.hydrate.test.js
IslandRhythms Feb 12, 2024
eb449c4
fix id generation
IslandRhythms Feb 12, 2024
e80cff1
progress
IslandRhythms Feb 12, 2024
57541cb
refactor: make query helpers more consistently fit our loose definiti…
vkarpov15 Feb 13, 2024
e6769c9
`hydratedPopulatedDocs` option
IslandRhythms Feb 13, 2024
7c696a3
fix: lint
IslandRhythms Feb 13, 2024
648239d
add typescript types, begin typescript test
IslandRhythms Feb 14, 2024
4675671
Update models.test.ts
IslandRhythms Feb 14, 2024
6dafa54
fix: lint
IslandRhythms Feb 14, 2024
db247da
Merge pull request #14352 from Automattic/IslandRhythms/deeply-hydrat…
vkarpov15 Feb 14, 2024
aa65645
feat: add middleware for bulkWrite() and createCollection()
vkarpov15 Feb 19, 2024
c164d63
Update lib/query.js
vkarpov15 Feb 19, 2024
4876d1e
Update lib/query.js
vkarpov15 Feb 19, 2024
5d7178c
Merge branch 'master' into 8.2
vkarpov15 Feb 20, 2024
2277ad4
Merge pull request #14358 from Automattic/vkarpov15/bulkwrite-middleware
vkarpov15 Feb 20, 2024
b10fc72
Merge pull request #14350 from Automattic/vkarpov15/helpers-refactor
vkarpov15 Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading