SARepo 0.1.2__tar.gz → 0.1.3__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SARepo
3
- Version: 0.1.2
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
@@ -12,4 +12,5 @@ class CrudRepository(Protocol, Generic[T]):
12
12
  def add(self, entity: T) -> T: ...
13
13
  def update(self, entity: T) -> T: ...
14
14
  def remove(self, entity: T) -> None: ...
15
+ def delete_by_id(self, id_: Any) -> bool: ...
15
16
  def page(self, page: PageRequest, spec=None, order_by=None) -> Page[T]: ...
@@ -2,7 +2,7 @@
2
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")
@@ -45,11 +45,26 @@ class SARepository(Generic[T]):
45
45
  return entity
46
46
 
47
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
48
56
  if hasattr(entity, "is_deleted"):
49
57
  setattr(entity, "is_deleted", True)
50
58
  else:
51
59
  self.session.delete(entity)
52
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
+
53
68
  def page(self, page: PageRequest, spec: Optional[Spec] = None, order_by=None) -> Page[T]:
54
69
  stmt = self._select()
55
70
  if spec:
@@ -101,11 +116,26 @@ class SAAsyncRepository(Generic[T]):
101
116
  return entity
102
117
 
103
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
104
127
  if hasattr(entity, "is_deleted"):
105
128
  setattr(entity, "is_deleted", True)
106
129
  else:
107
130
  await self.session.delete(entity)
108
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
+
109
139
  async def page(self, page: PageRequest, spec: Optional[Spec] = None, order_by=None) -> Page[T]:
110
140
  stmt = self._select()
111
141
  if spec:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SARepo
3
- Version: 0.1.2
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
@@ -1,7 +1,7 @@
1
1
 
2
2
  [project]
3
3
  name = "SARepo"
4
- version = "0.1.2"
4
+ version = "0.1.3"
5
5
  description = "Minimal, explicit Repository & Unit-of-Work layer over SQLAlchemy 2.x"
6
6
  readme = "README.md"
7
7
  requires-python = ">=3.11"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes