archipy-postgres 5.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ """PostgreSQL adapter package."""
@@ -0,0 +1 @@
1
+ """PostgreSQL SQLAlchemy adapters for ArchiPy."""
@@ -0,0 +1,75 @@
1
+ """PostgreSQL SQLAlchemy adapter implementations."""
2
+
3
+ from typing import override
4
+
5
+ from archipy.adapters.base.sqlalchemy.adapters import AsyncBaseSQLAlchemyAdapter, BaseSQLAlchemyAdapter
6
+ from archipy.adapters.postgres.sqlalchemy.session_managers import (
7
+ AsyncPostgresSQlAlchemySessionManager,
8
+ PostgresSQlAlchemySessionManager,
9
+ )
10
+ from archipy.configs.base_config import BaseConfig
11
+ from archipy.configs.config_template import PostgresSQLAlchemyConfig
12
+
13
+
14
+ class PostgresSQLAlchemyAdapter(BaseSQLAlchemyAdapter[PostgresSQLAlchemyConfig]):
15
+ """Synchronous SQLAlchemy adapter for PostgreSQL.
16
+
17
+ Inherits from BaseSQLAlchemyAdapter to provide PostgreSQL-specific session management
18
+ and database operations.
19
+
20
+ Args:
21
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
22
+ """
23
+
24
+ def __init__(self, orm_config: PostgresSQLAlchemyConfig | None = None) -> None:
25
+ """Initialize the PostgreSQL adapter with a session manager.
26
+
27
+ Args:
28
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
29
+ """
30
+ configs = BaseConfig.global_config().POSTGRES_SQLALCHEMY if orm_config is None else orm_config
31
+ super().__init__(configs)
32
+
33
+ @override
34
+ def _create_session_manager(self, configs: PostgresSQLAlchemyConfig) -> PostgresSQlAlchemySessionManager:
35
+ """Create a PostgreSQL-specific session manager.
36
+
37
+ Args:
38
+ configs: PostgreSQL configuration.
39
+
40
+ Returns:
41
+ A PostgreSQL session manager instance.
42
+ """
43
+ return PostgresSQlAlchemySessionManager(configs)
44
+
45
+
46
+ class AsyncPostgresSQLAlchemyAdapter(AsyncBaseSQLAlchemyAdapter[PostgresSQLAlchemyConfig]):
47
+ """Asynchronous SQLAlchemy adapter for PostgreSQL.
48
+
49
+ Inherits from AsyncBaseSQLAlchemyAdapter to provide async PostgreSQL-specific session
50
+ management and database operations.
51
+
52
+ Args:
53
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
54
+ """
55
+
56
+ def __init__(self, orm_config: PostgresSQLAlchemyConfig | None = None) -> None:
57
+ """Initialize the async PostgreSQL adapter with a session manager.
58
+
59
+ Args:
60
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
61
+ """
62
+ configs = BaseConfig.global_config().POSTGRES_SQLALCHEMY if orm_config is None else orm_config
63
+ super().__init__(configs)
64
+
65
+ @override
66
+ def _create_async_session_manager(self, configs: PostgresSQLAlchemyConfig) -> AsyncPostgresSQlAlchemySessionManager:
67
+ """Create an async PostgreSQL-specific session manager.
68
+
69
+ Args:
70
+ configs: PostgreSQL configuration.
71
+
72
+ Returns:
73
+ An async PostgreSQL session manager instance.
74
+ """
75
+ return AsyncPostgresSQlAlchemySessionManager(configs)
@@ -0,0 +1,116 @@
1
+ """Registry for PostgreSQL SQLAlchemy session managers."""
2
+
3
+ from archipy.adapters.base.sqlalchemy.session_manager_ports import AsyncSessionManagerPort, SessionManagerPort
4
+ from archipy.adapters.base.sqlalchemy.session_manager_registry import SessionManagerRegistry
5
+ from archipy.adapters.postgres.sqlalchemy.session_managers import (
6
+ AsyncPostgresSQlAlchemySessionManager,
7
+ PostgresSQlAlchemySessionManager,
8
+ )
9
+ from archipy.helpers.metaclasses.singleton import Singleton
10
+ from archipy.models.errors import DatabaseConnectionError, InvalidArgumentError, InvalidEntityTypeError
11
+
12
+
13
+ class PostgresSessionManagerRegistry(SessionManagerRegistry, metaclass=Singleton):
14
+ """Registry for PostgreSQL SQLAlchemy session managers.
15
+
16
+ This registry provides a centralized access point for both synchronous and
17
+ asynchronous PostgreSQL session managers, implementing the Service Locator pattern.
18
+ It lazily initializes the appropriate session manager when first requested.
19
+
20
+ The registry maintains singleton instances of:
21
+ - A synchronous session manager (PostgresSQlAlchemySessionManager)
22
+ - An asynchronous session manager (AsyncPostgresSQlAlchemySessionManager)
23
+ """
24
+
25
+ @classmethod
26
+ def get_sync_manager(cls) -> SessionManagerPort:
27
+ """Get the synchronous PostgreSQL session manager instance.
28
+
29
+ Lazily initializes a default PostgresSQlAlchemySessionManager if none has been set.
30
+
31
+ Returns:
32
+ SessionManagerPort: The registered synchronous session manager
33
+
34
+ Raises:
35
+ DatabaseConnectionError: If there's an error initializing the session manager
36
+ """
37
+ if cls._sync_instance is None:
38
+ try:
39
+ cls._sync_instance = PostgresSQlAlchemySessionManager()
40
+ except Exception as e:
41
+ raise DatabaseConnectionError(
42
+ database="postgresql",
43
+ ) from e
44
+ return cls._sync_instance
45
+
46
+ @classmethod
47
+ def set_sync_manager(cls, manager: SessionManagerPort) -> None:
48
+ """Set a custom synchronous session manager.
49
+
50
+ Args:
51
+ manager: An instance implementing SessionManagerPort
52
+
53
+ Raises:
54
+ InvalidArgumentError: If the manager is None
55
+ InvalidEntityTypeError: If the manager doesn't implement SessionManagerPort
56
+ """
57
+ if manager is None:
58
+ raise InvalidArgumentError(argument_name="postgres_session_manager")
59
+
60
+ if not isinstance(manager, SessionManagerPort):
61
+ raise InvalidEntityTypeError(
62
+ expected_type="SessionManagerPort",
63
+ actual_type=type(manager).__name__,
64
+ )
65
+ cls._sync_instance = manager
66
+
67
+ @classmethod
68
+ def get_async_manager(cls) -> AsyncSessionManagerPort:
69
+ """Get the asynchronous PostgreSQL session manager instance.
70
+
71
+ Lazily initializes a default AsyncPostgresSQlAlchemySessionManager if none has been set.
72
+
73
+ Returns:
74
+ AsyncSessionManagerPort: The registered asynchronous session manager
75
+
76
+ Raises:
77
+ DatabaseConnectionError: If there's an error initializing the session manager
78
+ """
79
+ if cls._async_instance is None:
80
+ try:
81
+ cls._async_instance = AsyncPostgresSQlAlchemySessionManager()
82
+ except Exception as e:
83
+ raise DatabaseConnectionError(
84
+ database="postgresql",
85
+ ) from e
86
+ return cls._async_instance
87
+
88
+ @classmethod
89
+ def set_async_manager(cls, manager: AsyncSessionManagerPort) -> None:
90
+ """Set a custom asynchronous session manager.
91
+
92
+ Args:
93
+ manager: An instance implementing AsyncSessionManagerPort
94
+
95
+ Raises:
96
+ InvalidArgumentError: If the manager is None
97
+ InvalidEntityTypeError: If the manager doesn't implement AsyncSessionManagerPort
98
+ """
99
+ if manager is None:
100
+ raise InvalidArgumentError(argument_name="postgres_async_session_manager")
101
+
102
+ if not isinstance(manager, AsyncSessionManagerPort):
103
+ raise InvalidEntityTypeError(
104
+ expected_type="AsyncSessionManagerPort",
105
+ actual_type=type(manager).__name__,
106
+ )
107
+ cls._async_instance = manager
108
+
109
+ @classmethod
110
+ def reset(cls) -> None:
111
+ """Reset the registry to its initial state.
112
+
113
+ This method clears both registered managers, useful for testing.
114
+ """
115
+ cls._sync_instance = None
116
+ cls._async_instance = None
@@ -0,0 +1,148 @@
1
+ """PostgreSQL SQLAlchemy session manager implementations."""
2
+
3
+ from typing import override
4
+
5
+ from sqlalchemy import URL
6
+ from sqlalchemy.exc import SQLAlchemyError
7
+
8
+ from archipy.adapters.base.sqlalchemy.session_managers import (
9
+ AsyncBaseSQLAlchemySessionManager,
10
+ BaseSQLAlchemySessionManager,
11
+ )
12
+ from archipy.configs.base_config import BaseConfig
13
+ from archipy.configs.config_template import PostgresSQLAlchemyConfig
14
+ from archipy.helpers.metaclasses.singleton import Singleton
15
+ from archipy.models.errors import DatabaseConnectionError
16
+
17
+
18
+ class PostgresSQlAlchemySessionManager(BaseSQLAlchemySessionManager[PostgresSQLAlchemyConfig], metaclass=Singleton):
19
+ """Synchronous SQLAlchemy session manager for PostgreSQL.
20
+
21
+ Inherits from BaseSQLAlchemySessionManager to provide PostgreSQL-specific session
22
+ management, including connection URL creation and engine configuration.
23
+
24
+ Args:
25
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
26
+ """
27
+
28
+ def __init__(self, orm_config: PostgresSQLAlchemyConfig | None = None) -> None:
29
+ """Initialize the PostgreSQL session manager.
30
+
31
+ Args:
32
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
33
+ """
34
+ configs = BaseConfig.global_config().POSTGRES_SQLALCHEMY if orm_config is None else orm_config
35
+ super().__init__(configs)
36
+
37
+ @override
38
+ def _expected_config_type(self) -> type[PostgresSQLAlchemyConfig]:
39
+ """Return the expected configuration type for PostgreSQL.
40
+
41
+ Returns:
42
+ The PostgresSQLAlchemyConfig class.
43
+ """
44
+ return PostgresSQLAlchemyConfig
45
+
46
+ @override
47
+ def _get_database_name(self) -> str:
48
+ """Return the name of the database being used.
49
+
50
+ Returns:
51
+ str: The name of the database ('postgresql').
52
+ """
53
+ return "postgresql"
54
+
55
+ @override
56
+ def _create_url(self, configs: PostgresSQLAlchemyConfig) -> URL:
57
+ """Create a PostgreSQL connection URL.
58
+
59
+ Args:
60
+ configs: PostgreSQL configuration.
61
+
62
+ Returns:
63
+ A SQLAlchemy URL object for PostgreSQL.
64
+
65
+ Raises:
66
+ DatabaseConnectionError: If there's an error creating the URL.
67
+ """
68
+ try:
69
+ return URL.create(
70
+ drivername=configs.DRIVER_NAME,
71
+ username=configs.USERNAME,
72
+ password=configs.PASSWORD,
73
+ host=configs.HOST,
74
+ port=configs.PORT,
75
+ database=configs.DATABASE,
76
+ )
77
+ except SQLAlchemyError as e:
78
+ raise DatabaseConnectionError(
79
+ database=self._get_database_name(),
80
+ ) from e
81
+
82
+
83
+ class AsyncPostgresSQlAlchemySessionManager(
84
+ AsyncBaseSQLAlchemySessionManager[PostgresSQLAlchemyConfig],
85
+ metaclass=Singleton,
86
+ ):
87
+ """Asynchronous SQLAlchemy session manager for PostgreSQL.
88
+
89
+ Inherits from AsyncBaseSQLAlchemySessionManager to provide async PostgreSQL-specific
90
+ session management, including connection URL creation and async engine configuration.
91
+
92
+ Args:
93
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
94
+ """
95
+
96
+ def __init__(self, orm_config: PostgresSQLAlchemyConfig | None = None) -> None:
97
+ """Initialize the async PostgreSQL session manager.
98
+
99
+ Args:
100
+ orm_config: PostgreSQL-specific configuration. If None, uses global config.
101
+ """
102
+ configs = BaseConfig.global_config().POSTGRES_SQLALCHEMY if orm_config is None else orm_config
103
+ super().__init__(configs)
104
+
105
+ @override
106
+ def _expected_config_type(self) -> type[PostgresSQLAlchemyConfig]:
107
+ """Return the expected configuration type for PostgreSQL.
108
+
109
+ Returns:
110
+ The PostgresSQLAlchemyConfig class.
111
+ """
112
+ return PostgresSQLAlchemyConfig
113
+
114
+ @override
115
+ def _get_database_name(self) -> str:
116
+ """Return the name of the database being used.
117
+
118
+ Returns:
119
+ str: The name of the database ('postgresql').
120
+ """
121
+ return "postgresql"
122
+
123
+ @override
124
+ def _create_url(self, configs: PostgresSQLAlchemyConfig) -> URL:
125
+ """Create an async PostgreSQL connection URL.
126
+
127
+ Args:
128
+ configs: PostgreSQL configuration.
129
+
130
+ Returns:
131
+ A SQLAlchemy URL object for PostgreSQL.
132
+
133
+ Raises:
134
+ DatabaseConnectionError: If there's an error creating the URL.
135
+ """
136
+ try:
137
+ return URL.create(
138
+ drivername=configs.DRIVER_NAME,
139
+ username=configs.USERNAME,
140
+ password=configs.PASSWORD,
141
+ host=configs.HOST,
142
+ port=configs.PORT,
143
+ database=configs.DATABASE,
144
+ )
145
+ except SQLAlchemyError as e:
146
+ raise DatabaseConnectionError(
147
+ database=self._get_database_name(),
148
+ ) from e
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.5
2
+ Name: archipy-postgres
3
+ Version: 5.9.0
4
+ Summary: ArchiPy postgres adapter.
5
+ Author-email: Hossein Nejati <hosseinnejati14@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Requires-Python: <4,>=3.14
8
+ Requires-Dist: archipy==5.9.0
9
+ Requires-Dist: psycopg[binary,pool]>=3.3.6
10
+ Description-Content-Type: text/markdown
11
+
12
+ # archipy-postgres
13
+
14
+ postgres adapter for [ArchiPy](https://syntaxarc.github.io/ArchiPy/). Install via the core extra:
15
+
16
+ ```bash
17
+ uv add "archipy[postgres]"
18
+ ```
@@ -0,0 +1,8 @@
1
+ archipy/adapters/postgres/__init__.py,sha256=xH2wL-PO2aj_OQlpnZHTpk43sdbEFyegNtdE7N5gesQ,34
2
+ archipy/adapters/postgres/sqlalchemy/__init__.py,sha256=-5KufKq14FxK4wbmpSEq8fc-S7mopM60Zi0aIKcKuEM,50
3
+ archipy/adapters/postgres/sqlalchemy/adapters.py,sha256=ASP3wgnFGN4n9XP4vhdX_h9xJe8Uw-AT8FYvoGjIxb0,2814
4
+ archipy/adapters/postgres/sqlalchemy/session_manager_registry.py,sha256=I1_Je0A8Gayc_eIdbxWU2RZaf1O_5Tl1HBhQeZQaoOk,4518
5
+ archipy/adapters/postgres/sqlalchemy/session_managers.py,sha256=ddq0KpwvrBZIWLF6CW6pdK_imEgKXnmN4A2eAFWKKW4,4911
6
+ archipy_postgres-5.9.0.dist-info/METADATA,sha256=NzPe49YWSeAOL-WJNAr4FCHpVB7hy_kikb0h7aiLaRk,482
7
+ archipy_postgres-5.9.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
8
+ archipy_postgres-5.9.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.4
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any