SARepo 0.1.5__tar.gz → 0.1.6__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.5
3
+ Version: 0.1.6
4
4
  Summary: Minimal, explicit Repository & Unit-of-Work layer over SQLAlchemy 2.x
5
5
  Author: nurbergenovv
6
6
  License: MIT
@@ -13,7 +13,7 @@ class CrudRepository(Protocol, Generic[T]):
13
13
  def try_get(self, id_: Any, *, include_deleted: bool = False) -> Optional[T]: ...
14
14
  def add(self, entity: T) -> T: ...
15
15
  def update(self, entity: T) -> T: ...
16
- def remove(self, entity: T) -> None: ...
16
+ def remove(self, entity: Optional[T] = None, id: Optional[Any] = None) -> bool: ...
17
17
  def delete_by_id(self, id_: Any) -> bool: ...
18
18
  def page(self, page: PageRequest, spec: Optional[Spec] = None, order_by=None, *, include_deleted: bool = False) -> Page[T]: ...
19
19
  def get_all_by_column(
@@ -71,7 +71,13 @@ class SARepository(Generic[T]):
71
71
  self.session.refresh(entity)
72
72
  return entity
73
73
 
74
- def remove(self, entity: T) -> None:
74
+ def remove(self, entity: Optional[T] = None, id: Optional[Any] = None) -> bool:
75
+ if entity is None and id is None:
76
+ raise ValueError("remove() requires either entity or id")
77
+
78
+ if id is not None:
79
+ return self._delete_by_id(id)
80
+
75
81
  insp = inspect(entity, raiseerr=False)
76
82
  if not (insp and (insp.persistent or insp.pending)):
77
83
  pk = getattr(entity, "id", None)
@@ -79,13 +85,15 @@ class SARepository(Generic[T]):
79
85
  raise ValueError("remove() needs a persistent entity or an entity with a primary key set")
80
86
  entity = self.session.get(self.model, pk)
81
87
  if entity is None:
82
- return
88
+ return False
83
89
  if hasattr(entity, "is_deleted"):
84
90
  setattr(entity, "is_deleted", True)
85
91
  else:
86
92
  self.session.delete(entity)
93
+
94
+ return True
87
95
 
88
- def delete_by_id(self, id_: Any) -> bool:
96
+ def _delete_by_id(self, id_: Any) -> bool:
89
97
  obj = self.session.get(self.model, id_)
90
98
  if not obj:
91
99
  return False
@@ -283,7 +291,13 @@ class SAAsyncRepository(Generic[T]):
283
291
  await self.session.refresh(entity)
284
292
  return entity
285
293
 
286
- async def remove(self, entity: T) -> None:
294
+ async def remove(self, entity: Optional[T] = None, id: Optional[Any] = None) -> bool:
295
+ if entity is None and id is None:
296
+ raise ValueError("remove() requires either entity or id")
297
+
298
+ if id is not None:
299
+ return await self._delete_by_id(id)
300
+
287
301
  insp = inspect(entity, raiseerr=False)
288
302
  if not (insp and (insp.persistent or insp.pending)):
289
303
  pk = getattr(entity, "id", None)
@@ -291,13 +305,16 @@ class SAAsyncRepository(Generic[T]):
291
305
  raise ValueError("remove() needs a persistent entity or an entity with a primary key set")
292
306
  entity = await self.session.get(self.model, pk)
293
307
  if entity is None:
294
- return
308
+ return False
295
309
  if hasattr(entity, "is_deleted"):
296
310
  setattr(entity, "is_deleted", True)
297
311
  else:
298
312
  await self.session.delete(entity)
313
+
314
+ return True
299
315
 
300
- async def delete_by_id(self, id_: Any) -> bool:
316
+
317
+ async def _delete_by_id(self, id_: Any) -> bool:
301
318
  obj = await self.session.get(self.model, id_)
302
319
  if not obj:
303
320
  return False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SARepo
3
- Version: 0.1.5
3
+ Version: 0.1.6
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.5"
4
+ version = "0.1.6"
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"
@@ -132,8 +132,11 @@ def test_restore(session, repo):
132
132
  seed(session, repo)
133
133
 
134
134
  beta = repo.find_all_by_column("title", "beta")[0]
135
- beta.is_deleted = True
135
+ beta.is_deleted = False
136
136
  session.commit()
137
+
138
+ deleted = repo.remove(id=beta.id)
139
+ print("DELETED ", deleted)
137
140
 
138
141
  ok = repo.restore(beta.id)
139
142
  assert ok is True
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes