feat: introduce ODMClient for simplified multi-database management#1260
Open
A-K-6 wants to merge 6 commits intoBeanieODM:mainfrom
Open
feat: introduce ODMClient for simplified multi-database management#1260A-K-6 wants to merge 6 commits intoBeanieODM:mainfrom
A-K-6 wants to merge 6 commits intoBeanieODM:mainfrom
Conversation
Introduces a new `ODMClient` class in `beanie.odm.client` to simplify asynchronous MongoDB connection management and database initialization. Key changes: - Implements `ODMClient` with support for multiple database registrations. - Integrates Beanie initialization and migration execution within the client. - Adds support for async context management (`__aenter__`/`__aexit__`) to ensure proper connection handling. - Exports `ODMClient` in the main `beanie` package. - Includes initial test suite for the new client functionality.
The `ODMClient.init_db` and `register_database` methods now support `allow_index_dropping`, `recreate_views`, and `skip_indexes` parameters. These are passed directly to Beanie's `init_beanie` to provide finer control over database initialization. Additionally, a documentation note and a corresponding test case were added to clarify that Beanie binds models globally, meaning the last database registered for a specific model class will prevail.
Introduces a new tutorial document for the `ODMClient` utility. The guide covers: - Initialization with MongoDB URIs - Registering single and multiple databases - Handling database migrations - Using the client as an async context manager - Important notes regarding global model binding in Beanie
Update the pydoc-markdown configuration to include the new ODM Client tutorial and API reference. - Added "ODM Client" to the tutorial navigation pointing to docs/tutorial/odm-client.md. - Added a "Client" section to the API documentation covering the beanie.odm.client module. These changes ensure that new client-related documentation is correctly indexed and rendered in the documentation site.
Member
|
Interesting idea, but this is a large feature addition that needs more discussion before merging. A few concerns:
Could you open a discussion issue first so we can align on the API design? I would rather get the interface right before reviewing the implementation details. |
07pepa
approved these changes
Mar 29, 2026
Author
|
Thank you for your patience. Since I opened this PR, there have been two major internet shutdowns in my country - Iran - , and we are currently in the middle of the second one. My internet access is extremely limited and unstable at the moment. I will address the remaining items as soon as I have a stable connection again. Thanks for understanding. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem Statement
Managing multiple MongoDB databases and their associated Beanie models often requires significant boilerplate code to handle connections, migrations, and initialization for each database individually.
Solution:
ODMClientThis PR introduces the
ODMClient, a high-level utility class that centralizes and simplifies the lifecycle of Beanie applications.Key Features
init_dbwith a configuration dictionary to initialize multiple databases and models in one call.AsyncMongoClientinternally and supports async context manager usage (async with ODMClient(...) as client:).allow_index_dropping,recreate_views,skip_indexes).Examples
Checklist