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

mongoose.syncIndexes() and Connection#syncIndexes(): run syncIndexes() on all registered models #10893

Closed
AbdelrahmanHafez opened this issue Oct 17, 2021 · 3 comments · Fixed by #10997
Assignees
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@AbdelrahmanHafez
Copy link
Collaborator

So, in mongoose 5.2.0 we introduced Model.syncIndexes(), and I've found that feature quite useful, I'd like to make it easier for people to utilize it.

Following are a few ideas on how we can do that:

  • Create a new option: mongoose.set('syncIndexes', true);, which would automatically attempt to syncIndexes on connecting to the database. I assume that most people would expect that changing an index in the schema would reflect on the database, so I'd argue that this option should be set to true by default on mongoose v7.
  • Create a new convenience static mongoose.syncIndexes(); which would internally loop over all the models and sync their indexes. The reasoning behind doing that is because I run multiple instances of node, and would like to use mongoose.syncIndexes(); only on one of the node instances in order to avoid multiple instances trying to sync at once, avoiding possible race conditions.

What do you think?
@vkarpov15 @IslandRhythms @ahmedelshenawy25

@IslandRhythms IslandRhythms added the discussion If you have any thoughts or comments on this issue, please share them! label Oct 18, 2021
@IslandRhythms
Copy link
Collaborator

Both have their benefits, I think option 2 gives more control to the user when to invoke it as opposed to option 1 where it will always be invoked.

@AbdelrahmanHafez
Copy link
Collaborator Author

@IslandRhythms
I've been thinking of introducing both options. I agree that mongoose.syncIndexes(); gives more control and I would probably use it over mongoose.set('syncIndexes', true); in my apps.

@vkarpov15
Copy link
Collaborator

I like both options. I think adding mongoose.syncIndexes() in v6.1 would be a nice new feature to add.

@vkarpov15 vkarpov15 added this to the 6.1.0 milestone Oct 19, 2021
@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class and removed discussion If you have any thoughts or comments on this issue, please share them! labels Oct 19, 2021
@vkarpov15 vkarpov15 changed the title Make syncing indexes convenient mongoose.syncIndexes() and Connection#syncIndexes(): run syncIndexes() on all registered models Oct 19, 2021
@AbdelrahmanHafez AbdelrahmanHafez self-assigned this Oct 20, 2021
@IslandRhythms IslandRhythms linked a pull request Nov 19, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants