trolldb.api.routes.common module

The module with common functions to be used in handling requests related to databases and collections.

async trolldb.api.routes.common.check_database(database_name: str | None = None) motor.motor_asyncio.AsyncIOMotorDatabase[source]

A dependency for route handlers to check for the existence of a database given its name.

Parameters:

database_name (Optional, default None) – The name of the database to check. In case of None, the main database will be picked.

Returns:

The database object if it exists.

Raises:

ResponseError – Check get_database() for more information.

async trolldb.api.routes.common.check_collection(database_name: str | None = None, collection_name: str | None = None) motor.motor_asyncio.AsyncIOMotorCollection[source]

A dependency for route handlers to check for the existence of a collection.

It performs the check given the collection name and the name of the database it resides in. It first checks for the existence of the database.

Parameters:
  • database_name (Optional, default None) – The name of the database to check. In case of None, the main database will be picked.

  • collection_name (Optional, default None) – The name of the collection to check. In case of None, the main collection will be picked.

Warning

Both of database_name and collection_name must be None so that the main database and collection will be picked. In case only one of them is None, this is treated as an unacceptable request.

Returns:

  • The collection object if it exists in the designated database.

Raises:

ResponseError – Check get_collection() for more information.

async trolldb.api.routes.common.get_distinct_items_in_collection(response_or_collection: fastapi.Response | motor.motor_asyncio.AsyncIOMotorCollection, field_name: str) fastapi.Response | list[str][source]

An auxiliary function to either return the given response; or return a list of distinct (unique) values.

Given the field_name it conducts a search in all documents of the given collection. The latter behaviour is equivalent to the distinct function from MongoDB. The former is the behaviour of an identity function.

Parameters:
  • response_or_collection – Either a response object, or a collection in which documents will be queried for the field_name.

  • field_name – The name of the target field in the documents

Returns:

  • In case of a response object as input, the same response will be returned as-is.

  • In case of a collection as input, all the documents of the collection will be searched for field_name, and the corresponding values will be retrieved. Finally, a list of all the distinct values is returned.

trolldb.api.routes.common.CheckCollectionDependency

Type annotation for the FastAPI dependency injection of checking a collection (function).

alias of Annotated[AsyncIOMotorCollection, fastapi.Depends]

trolldb.api.routes.common.CheckDataBaseDependency

Type annotation for the FastAPI dependency injection of checking a database (function).

alias of Annotated[AsyncIOMotorDatabase, fastapi.Depends]