eegdash.mongodb module#
MongoDB connection and operations management.
This module provides a thread-safe singleton manager for MongoDB connections, ensuring that connections to the database are handled efficiently and consistently across the application.
- class eegdash.mongodb.MongoConnectionManager[source]
Bases:
object
A thread-safe singleton to manage MongoDB client connections.
This class ensures that only one connection instance is created for each unique combination of a connection string and staging flag. It provides class methods to get a client and to close all active connections.
- _instances
A dictionary to store singleton instances, mapping a (connection_string, is_staging) tuple to a (client, db, collection) tuple.
- Type:
dict
- _lock
A lock to ensure thread-safe instantiation of clients.
- Type:
threading.Lock
- classmethod close_all() None [source]
Close all managed MongoDB client connections.
This method iterates through all cached client instances and closes their connections. It also clears the instance cache.
- classmethod get_client(connection_string: str, is_staging: bool = False) tuple[MongoClient, Database, Collection] [source]
Get or create a MongoDB client for the given connection parameters.
This method returns a cached client if one already exists for the given connection string and staging flag. Otherwise, it creates a new client, connects to the appropriate database (“eegdash” or “eegdashstaging”), and returns the client, database, and “records” collection.
- Parameters:
connection_string (str) – The MongoDB connection string.
is_staging (bool, default False) – If True, connect to the staging database (“eegdashstaging”). Otherwise, connect to the production database (“eegdash”).
- Returns:
A tuple containing the connected MongoClient instance, the Database object, and the Collection object for the “records” collection.
- Return type:
tuple[MongoClient, Database, Collection]