Skip to content

BaseProvider

Database provider interface. All persistence adapters (SQLAlchemy, Elasticsearch, etc.) implement this contract.

See Database Adapters for concrete adapter configuration.

Bases: RegisterLookupMixin

Provider Implementation for each database that acts as a gateway to configure the database, retrieve connections and perform commits

Initialize Provider with Connection/Adapter details

Source code in src/protean/port/provider.py
14
15
16
17
18
def __init__(self, name, domain, conn_info: dict):
    """Initialize Provider with Connection/Adapter details"""
    self.name = name
    self.domain = domain
    self.conn_info = conn_info

get_session abstractmethod

get_session()

Establish a new session with the database.

Typically the session factory should be created once per application. Which is then held on to and passed to different transactions.

In Protean's case, the session scope and the transaction scope match. Which means that a new session is created when a transaction needs to be initiated (at the beginning of request handling, for example) and terminated (after committing or rolling back) at the end of the process. The session will be used as a component in Unit of Work Pattern, to handle transactions reliably.

Sessions are made available to requests as part of a Context Manager.

Source code in src/protean/port/provider.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@abstractmethod
def get_session(self):
    """Establish a new session with the database.

    Typically the session factory should be created once per application. Which is then
    held on to and passed to different transactions.

    In Protean's case, the session scope and the transaction scope match. Which means that a
    new session is created when a transaction needs to be initiated (at the beginning of
    request handling, for example) and terminated (after committing or rolling back) at the end
    of the process. The session will be used as a component in Unit of Work Pattern, to handle
    transactions reliably.

    Sessions are made available to requests as part of a Context Manager.
    """

get_connection abstractmethod

get_connection()

Get the connection object for the repository

Source code in src/protean/port/provider.py
47
48
49
@abstractmethod
def get_connection(self):
    """Get the connection object for the repository"""

is_alive abstractmethod

is_alive() -> bool

Check if the connection is alive

Source code in src/protean/port/provider.py
51
52
53
@abstractmethod
def is_alive(self) -> bool:
    """Check if the connection is alive"""

close abstractmethod

close()

Close the provider and clean up any persistent connections or resources.

This method should be called to properly dispose of connections and free up resources when the provider is no longer needed. Implementations should: - Close any connection pools - Dispose of any persistent connections - Clean up any other resources (engines, clients, etc.)

Source code in src/protean/port/provider.py
55
56
57
58
59
60
61
62
63
64
@abstractmethod
def close(self):
    """Close the provider and clean up any persistent connections or resources.

    This method should be called to properly dispose of connections and free up
    resources when the provider is no longer needed. Implementations should:
    - Close any connection pools
    - Dispose of any persistent connections
    - Clean up any other resources (engines, clients, etc.)
    """

get_dao abstractmethod

get_dao(entity_cls, database_model_cls)

Return a DAO object configured with a live connection

Source code in src/protean/port/provider.py
66
67
68
@abstractmethod
def get_dao(self, entity_cls, database_model_cls):
    """Return a DAO object configured with a live connection"""

decorate_database_model_class abstractmethod

decorate_database_model_class(entity_cls, database_model_cls)

Return decorated Model Class for custom-defined models

Source code in src/protean/port/provider.py
70
71
72
@abstractmethod
def decorate_database_model_class(self, entity_cls, database_model_cls):
    """Return decorated Model Class for custom-defined models"""

construct_database_model_class abstractmethod

construct_database_model_class(entity_cls)

Return dynamically constructed Model Class

Source code in src/protean/port/provider.py
74
75
76
@abstractmethod
def construct_database_model_class(self, entity_cls):
    """Return dynamically constructed Model Class"""

raw abstractmethod

raw(query: Any, data: Any = None)

Run raw query directly on the database

Query should be executed immediately on the database as a separate unit of work (in a different transaction context). The results should be returned as returned by the database without any intervention. It is left to the consumer to interpret and organize the results correctly.

Source code in src/protean/port/provider.py
78
79
80
81
82
83
84
85
86
@abstractmethod
def raw(self, query: Any, data: Any = None):
    """Run raw query directly on the database

    Query should be executed immediately on the database as a separate unit of work
        (in a different transaction context). The results should be returned as returned by
        the database without any intervention. It is left to the consumer to interpret and
        organize the results correctly.
    """