db module

For all database connection needs.

This module provides DatabaseConnection:

The base connection class with three methods: DatabaseConnection.connect(), DatabaseConnection.close() and DatabaseConnection.is_connected().

class db.DatabaseConnection(logger_name: str)

Bases: object

The base class to represent a connection to a PostgreSQL database.

Parameters

logger_name (str) – The name of the logger to be used by the connection.

async connect(host: str, user: str, password: str, database: str, loop: Optional[asyncio.events.AbstractEventLoop] = None, port: Optional[str] = None) asyncpg.connection.Connection

Connect to a database using the credentials provided. Stores connection in self.conn.

Parameters
  • host (str) – The hostname, usually ‘localhost’.

  • user (str) – A user with access to the database.

  • password (str) – The user’s password.

  • database (str) – The name of the database to connect to.

  • loop (asyncio.AbstractEventLoop) – The asyncio loop to use. If None, asyncpg uses the default event loop. None by default.

  • port (str) – The port to use, usually 5432. None by default.

async close() bool

Closes the connection to the database.

Returns

True if the connection was closed, False if there was no connection to close.

Return type

bool

async is_connected() bool

Check if the connection is active.

Returns

True if there’s a connection, False otherwise.

Return type

bool

exception db.DbInsertError

Bases: Exception

Raised when an error related to INSERT occurs.

exception db.NotFoundError

Bases: Exception

Raised when an error related to a missing result occurs.