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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook into bulkWrite #14263

Closed
2 tasks done
jaschaio opened this issue Jan 17, 2024 · 0 comments 路 Fixed by #14358
Closed
2 tasks done

Hook into bulkWrite #14263

jaschaio opened this issue Jan 17, 2024 · 0 comments 路 Fixed by #14358
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@jaschaio
Copy link
Contributor

jaschaio commented Jan 17, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

Mongoose already supports middlewares for the majority of operations. But by reviewing the documentation and code I couldn't find a way to hook into bulkWrite.

Which I found surprising, as castBulkWrite is called on each bulkWrite operation giving at least a perfect entry point for
pre modifying bulkWrite operations before they are executed:

const validations = ops.map(op => castBulkWrite(this, op, options));

This allows mongoose internally for example to add the discriminator:

https://github.com/Automattic/mongoose/blob/master/lib/helpers/model/castBulkWrite.js#L70

Or updatedAt and createdAt timestamps:

https://github.com/Automattic/mongoose/blob/master/lib/helpers/model/castBulkWrite.js#L75

Motivation

I am already using mongoose discriminators to clearly seperate tenants within a single database.

Mongoose only allows setting a single discriminatorKey per model, so if I would like to "discriminate" additionally on another key I can't use discriminators for it.

Now I want to shard data based on geolocation for which each query needs to include both the discriminatorKey and the location. This is fairly easy to accomplish via hooks:

// ... higher order method that calls each model with the location

// Modify mongoose queries to include location
Model.schema.pre( /find|update/, function () {

    this.setQuery( { ...this.getQuery(), location } );

} );

// Add a $match location stage to the beginning of each aggregation pipeline
Model.schema.pre( 'aggregate', function() {

    this.pipeline().unshift( { $match: { location } } );

} );

But this doesn't touch bulkWrite operations. Here I would have to remember to include the location within each filter key. I would prefer if I could just hook into the castBulkWrite method instead similar to how mongoose is doing it internally with discriminators.

@jaschaio jaschaio added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Jan 17, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.1.1, 8.1.2 Jan 17, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.1.2, 8.2 Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants