Database¶
-
exception
DatabaseError
(msg: str)¶ Bases:
Exception
Raised when an error occur when interacting with the database.
- Parameters
msg – Error message.
-
init
(config: dict)¶ Initialize the MongoClient and create a dictionary of available collections.
- Parameters
config – Dictionary containing database config. Check
modules.config.database
.
-
get_all_elements
(init_class_method: Callable, collection: str)¶ Get all elements of a given collection.
- Parameters
init_class_method – The data will be passed to this method.
collection – Collection name.
- Raises
DatabaseError – If an error occurs while passing data.
-
async
async_db_call
(call: Callable, *args)¶ Call a db function asynchronously.
- Parameters
call – Function to call.
args – Args to pass to the called function.
- Returns
Return the result of the call.
-
force_update
(collection: str, elements)¶ This is typically called from external scripts for db maintenance. Replace the whole collection by the provided elements.
- Parameters
collection – Collection name.
elements – Elements to insert.
-
set_field
(collection: str, e_id: int, doc: dict)¶ Set the field of an element. In other words, update an element.
- Parameters
collection – Collection name.
e_id – Element id.
doc – Data to set.
- Raises
DatabaseError – If the element is not in the collection.
-
unset_field
(collection: str, e_id: int, doc: dict)¶ Unset (remove) the field of an element. In other words, update an element.
- Parameters
collection – Collection name.
e_id – Element id.
doc – Data to unset.
- Raises
DatabaseError – If the element is not in the collection.
-
push_element
(collection: str, e_id: int, doc: dict)¶ Push data in the field of an element.
- Parameters
collection – Collection name.
e_id – Element id.
doc – Data to push. The key should be the field to push to.
- Raises
DatabaseError – If the element is not in the collection.
-
get_element
(collection: str, item_id: int) -> (<class 'dict'>, None)¶ Get a single element.
- Parameters
collection – Collection name.
item_id – Element id.
- Returns
Element found, or None if not found.
-
get_field
(collection: str, e_id: int, specific: str)¶ Get one field of a single element.
- Parameters
collection – Collection name.
e_id – Element id.
specific – Field name.
- Returns
Element found, or None if not found.
-
set_element
(collection: str, e_id: id, data: dict)¶ Set a whole element (with all its field). Replace if the element already exists.
- Parameters
collection – Collection name.
e_id – Element id.
data – Element data.
-
remove_element
(collection: str, e_id: int)¶ Remove an element from the database.
- Parameters
collection – Collection name
e_id – Element id.
- Raises
DatabaseError – If the element is not in the collection.