SARepo 0.1.0__py3-none-any.whl → 0.1.3__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.
SARepo/repo.py CHANGED
@@ -1,14 +1,16 @@
1
1
 
2
- from typing import Generic, TypeVar, Type, Optional, Any, Protocol
2
+ from typing import Generic, List, TypeVar, Type, Optional, Any, Protocol
3
3
  from .base import Page, PageRequest
4
4
 
5
5
  T = TypeVar("T")
6
6
 
7
7
  class CrudRepository(Protocol, Generic[T]):
8
8
  model: Type[T]
9
+ def getAll(self, limit: Optional[int]) -> List[T]: ...
9
10
  def get(self, id_: Any) -> T: ...
10
11
  def try_get(self, id_: Any) -> Optional[T]: ...
11
12
  def add(self, entity: T) -> T: ...
12
13
  def update(self, entity: T) -> T: ...
13
14
  def remove(self, entity: T) -> None: ...
15
+ def delete_by_id(self, id_: Any) -> bool: ...
14
16
  def page(self, page: PageRequest, spec=None, order_by=None) -> Page[T]: ...
SARepo/sa_repo.py CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
- from typing import Type, Generic, TypeVar, Optional, Sequence, Any, Callable
2
+ from typing import List, Type, Generic, TypeVar, Optional, Sequence, Any, Callable
3
3
  from sqlalchemy.orm import Session
4
4
  from sqlalchemy.ext.asyncio import AsyncSession
5
- from sqlalchemy import select, func
5
+ from sqlalchemy import inspect, select, func
6
6
  from .base import PageRequest, Page, NotFoundError
7
7
 
8
8
  T = TypeVar("T")
@@ -17,6 +17,13 @@ class SARepository(Generic[T]):
17
17
  def _select(self):
18
18
  return select(self.model)
19
19
 
20
+ def getAll(self, limit: Optional[int] = None) -> List[T]:
21
+ stmt = select(self.model)
22
+ if limit is not None:
23
+ stmt = stmt.limit(limit)
24
+ result = self.session.execute(stmt)
25
+ return result.scalars().all()
26
+
20
27
  def get(self, id_: Any) -> T:
21
28
  obj = self.session.get(self.model, id_)
22
29
  if not obj:
@@ -38,11 +45,26 @@ class SARepository(Generic[T]):
38
45
  return entity
39
46
 
40
47
  def remove(self, entity: T) -> None:
48
+ insp = inspect(entity, raiseerr=False)
49
+ if not (insp and (insp.persistent or insp.pending)):
50
+ pk = getattr(entity, "id", None)
51
+ if pk is None:
52
+ raise ValueError("remove() needs a persistent entity or an entity with a primary key set")
53
+ entity = self.session.get(self.model, pk)
54
+ if entity is None:
55
+ return
41
56
  if hasattr(entity, "is_deleted"):
42
57
  setattr(entity, "is_deleted", True)
43
58
  else:
44
59
  self.session.delete(entity)
45
60
 
61
+ def delete_by_id(self, id_: Any) -> bool:
62
+ obj = self.session.get(self.model, id_)
63
+ if not obj:
64
+ return False
65
+ self.remove(obj)
66
+ return True
67
+
46
68
  def page(self, page: PageRequest, spec: Optional[Spec] = None, order_by=None) -> Page[T]:
47
69
  stmt = self._select()
48
70
  if spec:
@@ -62,6 +84,13 @@ class SAAsyncRepository(Generic[T]):
62
84
  def __init__(self, model: Type[T], session: AsyncSession):
63
85
  self.model = model
64
86
  self.session = session
87
+
88
+ async def getAll(self, limit: Optional[int] = None) -> List[T]:
89
+ stmt = select(self.model)
90
+ if limit is not None:
91
+ stmt = stmt.limit(limit)
92
+ result = await self.session.execute(stmt)
93
+ return result.scalars().all()
65
94
 
66
95
  def _select(self):
67
96
  return select(self.model)
@@ -87,11 +116,26 @@ class SAAsyncRepository(Generic[T]):
87
116
  return entity
88
117
 
89
118
  async def remove(self, entity: T) -> None:
119
+ insp = inspect(entity, raiseerr=False)
120
+ if not (insp and (insp.persistent or insp.pending)):
121
+ pk = getattr(entity, "id", None)
122
+ if pk is None:
123
+ raise ValueError("remove() needs a persistent entity or an entity with a primary key set")
124
+ entity = await self.session.get(self.model, pk)
125
+ if entity is None:
126
+ return
90
127
  if hasattr(entity, "is_deleted"):
91
128
  setattr(entity, "is_deleted", True)
92
129
  else:
93
130
  await self.session.delete(entity)
94
131
 
132
+ async def delete_by_id(self, id_: Any) -> bool:
133
+ obj = await self.session.get(self.model, id_)
134
+ if not obj:
135
+ return False
136
+ await self.remove(obj)
137
+ return True
138
+
95
139
  async def page(self, page: PageRequest, spec: Optional[Spec] = None, order_by=None) -> Page[T]:
96
140
  stmt = self._select()
97
141
  if spec:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SARepo
3
- Version: 0.1.0
3
+ Version: 0.1.3
4
4
  Summary: Minimal, explicit Repository & Unit-of-Work layer over SQLAlchemy 2.x
5
5
  Author: nurbergenovv
6
6
  License: MIT
@@ -0,0 +1,12 @@
1
+ SARepo/__init__.py,sha256=tNYbuUDloC1qVnXq7uomS3jRcaPUvy0VEZiMdYR6x1M,183
2
+ SARepo/base.py,sha256=UbAdZ9WYh_o93mrCVL3D8Q0tY_8mvm0HspO_L5m0GTQ,874
3
+ SARepo/models.py,sha256=ypSmbKAijvNH4WjSeJBgyNT8mKa_e_7-I5kiNisjGAI,570
4
+ SARepo/repo.py,sha256=5Af4SLfbKOMJdX7MOev4OcGIGQ0HzN6QDUktURjasyU,597
5
+ SARepo/sa_repo.py,sha256=feuwBHAEdW_STJ6fHxYlSO0KvSruoqiB_v1sTCRy6JE,5332
6
+ SARepo/specs.py,sha256=e-X1cCkva3e71M37hcfbjNDMezZAaOkkRaPToUzhEeU,687
7
+ SARepo/uow.py,sha256=yPlvi7PoH9x2pLNt6hEin9zT5qG9axUKn_TkZcIuz1s,859
8
+ sarepo-0.1.3.dist-info/licenses/LICENSE,sha256=px90NOQCrnZ77qoFT8IvAiNS1CXQ2OU27uVBKbki9DM,131
9
+ sarepo-0.1.3.dist-info/METADATA,sha256=FXf9N-SsghCIQGucviE8GVVxh1viF0BV0FhKkMeg2HQ,2691
10
+ sarepo-0.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ sarepo-0.1.3.dist-info/top_level.txt,sha256=0k952UYVZLIIv3kZzFlxI0yzBMusZo3-XCQtxms_kS0,7
12
+ sarepo-0.1.3.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- SARepo/__init__.py,sha256=tNYbuUDloC1qVnXq7uomS3jRcaPUvy0VEZiMdYR6x1M,183
2
- SARepo/base.py,sha256=UbAdZ9WYh_o93mrCVL3D8Q0tY_8mvm0HspO_L5m0GTQ,874
3
- SARepo/models.py,sha256=ypSmbKAijvNH4WjSeJBgyNT8mKa_e_7-I5kiNisjGAI,570
4
- SARepo/repo.py,sha256=vCVQmjYifzlO_ggOFwj3Xdyqt4_7ILDoUtHUAFnFyZU,482
5
- SARepo/sa_repo.py,sha256=mdTe3RyrGklG3z50jr4pOv4aidWxOZ_6dtdLgr83Osk,3617
6
- SARepo/specs.py,sha256=e-X1cCkva3e71M37hcfbjNDMezZAaOkkRaPToUzhEeU,687
7
- SARepo/uow.py,sha256=yPlvi7PoH9x2pLNt6hEin9zT5qG9axUKn_TkZcIuz1s,859
8
- sarepo-0.1.0.dist-info/licenses/LICENSE,sha256=px90NOQCrnZ77qoFT8IvAiNS1CXQ2OU27uVBKbki9DM,131
9
- sarepo-0.1.0.dist-info/METADATA,sha256=lHz7KBK2U2CAvoxwUB1ytK9d1LeNoiV0ODlvM7BFjgE,2691
10
- sarepo-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
- sarepo-0.1.0.dist-info/top_level.txt,sha256=0k952UYVZLIIv3kZzFlxI0yzBMusZo3-XCQtxms_kS0,7
12
- sarepo-0.1.0.dist-info/RECORD,,
File without changes