Skip to content

Database class

edgy.Database

Database(url=None, *, force_rollback=False, config=None, **options)

Bases: Database

An abstraction on the top of the EncodeORM databases.Database object.

This object allows to pass also a configuration dictionary in the format of

DATABASEZ_CONFIG = {
    "connection": {
        "credentials": {
            "scheme": 'sqlite', "postgres"...
            "host": ...,
            "port": ...,
            "user": ...,
            "password": ...,
            "database": ...,
            "options": {
                "driver": ...
                "ssl": ...
            }
        }
    }
}
PARAMETER DESCRIPTION
url

TYPE: Optional[Union[str, DatabaseURL]] DEFAULT: None

force_rollback

TYPE: bool DEFAULT: False

config

TYPE: Optional[DictAny] DEFAULT: None

**options

TYPE: Any DEFAULT: {}

Source code in .venv/lib/python3.8/site-packages/databasez/core.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def __init__(
    self,
    url: typing.Optional[typing.Union[str, "DatabaseURL"]] = None,
    *,
    force_rollback: bool = False,
    config: typing.Optional["DictAny"] = None,
    **options: typing.Any,
):
    assert config is None or url is None, "Use either 'url' or 'config', not both."

    _url: typing.Optional[typing.Union[str, "DatabaseURL"]] = None
    if not config:
        _url = url
    else:
        _url = self._build_url(config)

    self.url = DatabaseURL(_url)  # type: ignore
    self.options = options
    self.is_connected = False
    self._connection_map = weakref.WeakKeyDictionary()

    self._force_rollback = force_rollback

    backend_str = self._get_backend()
    backend_cls = import_from_string(backend_str)
    assert issubclass(backend_cls, DatabaseBackend)
    self._backend = backend_cls(self.url, **self.options)

    # When `force_rollback=True` is used, we use a single global
    # connection, within a transaction that always rolls back.
    self._global_connection: typing.Optional[Connection] = None
    self._global_transaction: typing.Optional[Transaction] = None