Skip to content

database

DatabaseResource

Bases: ConfigurableResource

Database connection.

Source code in bag3d/common/resources/database.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class DatabaseResource(ConfigurableResource):
    """
    Database connection.
    """

    host: Optional[str] = None
    user: Optional[str] = None
    password: Optional[str] = None
    dbname: Optional[str] = None
    port: Optional[str] = None
    other_params: Optional[Permissive()] = None

    def __init__(
        self,
        host: Optional[str] = None,
        user: Optional[str] = None,
        password: Optional[str] = None,
        dbname: Optional[str] = None,
        port: Optional[str] = None,
        other_params: Optional[Permissive()] = None,
    ):
        super().__init__(
            host=host or "data-postgresql",
            user=user or "baseregisters_test_user",
            password=password or "baseregisters_test_pswd",
            dbname=dbname or "baseregisters_test",
            port=port or "5432",
            other_params=other_params or {"sslmode": "allow"},
        )
        conn = DatabaseConnection(
            user=self.user,
            password=self.password,
            host=self.host,
            port=self.port,
            dbname=self.dbname,
            **self.other_params,
        )
        # Create the utility Postgres functions
        PostgresFunctions(conn)

    @property
    def connect(self):
        conn = DatabaseConnection(
            user=self.user,
            password=self.password,
            host=self.host,
            port=self.port,
            dbname=self.dbname,
            **self.other_params,
        )
        return conn