architectonics 0.0.24__py3-none-any.whl → 0.0.26__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.
File without changes
@@ -0,0 +1,9 @@
1
+ from dataclasses import dataclass
2
+ from datetime import datetime
3
+
4
+
5
+ @dataclass
6
+ class BaseModel:
7
+ id: str
8
+ created_at: datetime
9
+ updated_at: datetime
@@ -3,12 +3,12 @@ import uuid
3
3
  from sqlalchemy import Column, DateTime, String
4
4
  from sqlalchemy.orm import as_declarative
5
5
 
6
- from architectonics.common.interfaces.base_entity_interface import BaseEntityInterface
7
6
  from architectonics.common.utils.utils import get_current_datetime
7
+ from architectonics.core.models.base_model import BaseModel
8
8
 
9
9
 
10
10
  @as_declarative()
11
- class BaseEntity(BaseEntityInterface):
11
+ class BaseEntity:
12
12
  __abstract__ = True
13
13
 
14
14
  id = Column(
@@ -29,3 +29,6 @@ class BaseEntity(BaseEntityInterface):
29
29
  )
30
30
 
31
31
  PK_FIELD = "id"
32
+
33
+ def to_model(self) -> BaseModel:
34
+ raise NotImplementedError()
File without changes
@@ -0,0 +1,10 @@
1
+ class ObjectNotFoundException(Exception):
2
+ """Exception raised when can't find a row."""
3
+
4
+
5
+ class ObjectAlreadyExistsException(Exception):
6
+ """Exception raised when unique constraint failes."""
7
+
8
+
9
+ class IntegrityErrorException(Exception):
10
+ """Exception raised when integrity constraint fails."""
@@ -0,0 +1,149 @@
1
+ from abc import ABC
2
+ from typing import Callable
3
+
4
+ from asyncpg.exceptions import ForeignKeyViolationError
5
+ from sqlalchemy import delete, select, update
6
+ from sqlalchemy.exc import IntegrityError
7
+ from sqlalchemy.ext.asyncio import AsyncSession
8
+
9
+ from architectonics.core.models.base_model import BaseModel
10
+ from infrastructure.entities.base_entity import BaseEntity
11
+ from infrastructure.repositories.base_exceptions import (
12
+ IntegrityErrorException,
13
+ ObjectAlreadyExistsException,
14
+ ObjectNotFoundException,
15
+ )
16
+
17
+
18
+ class AbstractBaseRepository(ABC):
19
+ _entity: type[BaseEntity] = NotImplemented
20
+ _session: Callable = NotImplemented
21
+ _integrity_error: type[IntegrityError] = IntegrityError
22
+
23
+ def get_session(self) -> AsyncSession:
24
+ return self._session()
25
+
26
+ @property
27
+ def model_fields(self):
28
+ return self._entity.__table__.columns
29
+
30
+ async def create_model(
31
+ self,
32
+ values: dict[str, any],
33
+ ) -> BaseModel:
34
+
35
+ entity = self._entity(**values)
36
+
37
+ async with self.get_session() as session:
38
+ session.add(entity)
39
+
40
+ try:
41
+ await session.commit()
42
+ except self._integrity_error as e:
43
+ raise ObjectAlreadyExistsException(e)
44
+
45
+ return entity.to_model()
46
+
47
+ async def get_model_by_id(
48
+ self,
49
+ model_id: str,
50
+ ) -> BaseModel:
51
+
52
+ statement = select(
53
+ self._entity,
54
+ ).where(
55
+ self._entity.id == model_id,
56
+ )
57
+
58
+ async with self.get_session() as session:
59
+
60
+ result = await session.execute(
61
+ statement=statement,
62
+ )
63
+
64
+ entity = result.scalars().first()
65
+
66
+ if entity is None:
67
+ raise ObjectNotFoundException()
68
+
69
+ return entity.to_model()
70
+
71
+ async def update_model(
72
+ self,
73
+ model_id: str,
74
+ values: dict[str, any],
75
+ ) -> BaseModel:
76
+
77
+ filtered_values = {k: v for k, v in values.items() if v is not None}
78
+
79
+ update_statement = (
80
+ update(
81
+ self._entity,
82
+ )
83
+ .where(
84
+ self._entity.id == model_id,
85
+ )
86
+ .values(
87
+ **filtered_values,
88
+ )
89
+ )
90
+
91
+ get_statement = select(
92
+ self._entity,
93
+ ).where(
94
+ self._entity.id == model_id,
95
+ )
96
+
97
+ async with self.get_session() as session:
98
+ try:
99
+ result = await session.execute(update_statement)
100
+
101
+ await session.commit()
102
+ except self._integrity_error as e:
103
+ orig = getattr(e.orig, "__cause__", None)
104
+
105
+ if isinstance(orig, ForeignKeyViolationError):
106
+ raise ObjectNotFoundException()
107
+
108
+ raise IntegrityErrorException(e)
109
+
110
+ result = await session.execute(get_statement)
111
+ model = result.scalars().first()
112
+
113
+ if model is None:
114
+ raise ObjectNotFoundException()
115
+
116
+ return model.to_model()
117
+
118
+ async def delete_model(
119
+ self,
120
+ model_id: str,
121
+ ) -> None:
122
+
123
+ statement = delete(
124
+ self._entity,
125
+ ).where(
126
+ self._entity.id == model_id,
127
+ )
128
+
129
+ async with self.get_session() as session:
130
+ await session.execute(statement)
131
+ await session.commit()
132
+
133
+ async def get_models_list(
134
+ self,
135
+ ) -> list[BaseModel]:
136
+
137
+ statement = select(
138
+ self._entity,
139
+ )
140
+
141
+ async with self.get_session() as session:
142
+
143
+ result = await session.execute(
144
+ statement=statement,
145
+ )
146
+
147
+ models = result.scalars().all()
148
+
149
+ return [model.to_model() for model in models]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: architectonics
3
- Version: 0.0.24
3
+ Version: 0.0.26
4
4
  Summary:
5
5
  Author: Your Name
6
6
  Author-email: you@example.com
@@ -1,17 +1,21 @@
1
1
  architectonics/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  architectonics/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- architectonics/common/interfaces/base_entity_interface.py,sha256=pc83rhpKIod82rLUiJO4si7f-RuQe8YrY7dfgZiZSPs,293
4
3
  architectonics/common/utils/utils.py,sha256=a5dAvgfwYKQ5MwYaDxmy_YLHNHQxNgEH94YFuVrD4N0,84
5
4
  architectonics/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
5
  architectonics/core/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
6
  architectonics/core/config/application_settings.py,sha256=DmCjQCbm4BsxMoZuQ212Pjjl_3spQ2vK5TUyZdDcklM,239
8
7
  architectonics/core/factory/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
8
  architectonics/core/factory/factory.py,sha256=C9TDhkhq5lcdcBnICy4DgJj0yjeCIhq-UVSwaf-aTmM,748
9
+ architectonics/core/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ architectonics/core/models/base_model.py,sha256=4ZDJstiH20W6s2420lz-324vlTjj_FkUSdfDh5xaBI0,156
10
11
  architectonics/infrastructure/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
12
  architectonics/infrastructure/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
13
  architectonics/infrastructure/config/database.py,sha256=ZhV7UJRJGvhblzs9lS11cqlGZU8M-9y1pkjJl48vd1Q,545
13
14
  architectonics/infrastructure/config/database_settings.py,sha256=d1HA8rQj3LMACEvrC2-uxzs6vyoQB1Qw1_wSAdSmN_g,676
14
- architectonics/infrastructure/entities/base_entity.py,sha256=mT9GWK3hOeBFHeGwTWF12PJwirgx3CLKsYVW3_F0DzU,686
15
- architectonics-0.0.24.dist-info/WHEEL,sha256=y3eDiaFVSNTPbgzfNn0nYn5tEn1cX6WrdetDlQM4xWw,83
16
- architectonics-0.0.24.dist-info/METADATA,sha256=qZjN1vE0EVZY7vFSZzOsp1DtZ4o4WtTDPq4PTDu6rO4,469
17
- architectonics-0.0.24.dist-info/RECORD,,
15
+ architectonics/infrastructure/entities/base_entity.py,sha256=ksDB9IFm-pO8MM8awc2QFc9e5Gol1q6l2E4UhTWV1rA,712
16
+ architectonics/infrastructure/repositories/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
+ architectonics/infrastructure/repositories/base_exceptions.py,sha256=kOeu3p90LHMxCcp2cCL_Anrsz4CTLtELWqma_RCQSqM,303
18
+ architectonics/infrastructure/repositories/base_repository.py,sha256=6nnISsDmqhUwYLnHfV_XJ29m0MrHgdjnZx1IVO90mfE,3763
19
+ architectonics-0.0.26.dist-info/WHEEL,sha256=y3eDiaFVSNTPbgzfNn0nYn5tEn1cX6WrdetDlQM4xWw,83
20
+ architectonics-0.0.26.dist-info/METADATA,sha256=7l9VspJ3FkdAHhI27Y8H4H6FxgF1YP6q7TW3XVBRobI,469
21
+ architectonics-0.0.26.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- from datetime import datetime
2
- from typing import ClassVar, Protocol
3
-
4
-
5
- class BaseEntityInterface(Protocol):
6
- PK_FIELD: ClassVar[str]
7
-
8
- @property
9
- def id(self) -> str: ...
10
-
11
- @property
12
- def created_at(self) -> datetime: ...
13
-
14
- @property
15
- def updated_at(self) -> datetime: ...