cwubs-shared 0.1.1__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.
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: cwubs-shared
3
+ Version: 0.1.1
4
+ Summary: Shared utilities for cwubs services
5
+ Author: Duncan Scuffell
6
+ Author-email: duncan.scuffell@btinternet.com
7
+ Requires-Python: >=3.12,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Classifier: Programming Language :: Python :: 3.14
12
+ Requires-Dist: sqlalchemy (>=2.0.46,<3.0.0)
13
+ Description-Content-Type: text/markdown
14
+
15
+ # cwubs-shared
@@ -0,0 +1 @@
1
+ # cwubs-shared
@@ -0,0 +1,21 @@
1
+ [tool.poetry]
2
+ name = "cwubs-shared"
3
+ version = "0.1.1" # placeholder
4
+ description = "Shared utilities for cwubs services"
5
+ authors = ["Duncan Scuffell <duncan.scuffell@btinternet.com>"]
6
+ readme = "README.md"
7
+ packages = [{ include = "shared" }]
8
+
9
+ [tool.poetry.dependencies]
10
+ python = ">=3.12,<4.0"
11
+ sqlalchemy = "^2.0.46"
12
+
13
+ [tool.poetry-dynamic-versioning]
14
+ enable = false
15
+
16
+ [build-system]
17
+ requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
18
+ build-backend = "poetry_dynamic_versioning.backend"
19
+
20
+ [tool.poetry.requires-plugins]
21
+ poetry-dynamic-versioning = { version = ">=1.0.0,<2.0.0", extras = ["plugin"] }
@@ -0,0 +1,3 @@
1
+ from .base import BaseRepository
2
+
3
+ __all__ = ["BaseRepository"]
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Generic, Optional, TypeVar
4
+
5
+ from sqlalchemy import select
6
+ from sqlalchemy.orm import Session
7
+
8
+ T = TypeVar("T")
9
+ ID = TypeVar("ID")
10
+
11
+
12
+ class BaseRepository(Generic[T, ID]):
13
+ def __init__(self, db: Session, model: type[T]) -> None:
14
+ self._db = db
15
+ self._model = model
16
+
17
+ def create(self, obj: T) -> T:
18
+ self._db.add(obj)
19
+ self._db.commit()
20
+ self._db.refresh(obj)
21
+ return obj
22
+
23
+ def get(self, obj_id: ID) -> Optional[T]:
24
+ return self._db.get(self._model, obj_id)
25
+
26
+ def exists(self, obj_id: ID) -> bool:
27
+ return (
28
+ self._db.execute(select(self._model.id).where(self._model.id == obj_id)).first()
29
+ is not None
30
+ )
31
+
32
+ def save(self, obj: T) -> T:
33
+ self._db.commit()
34
+ self._db.refresh(obj)
35
+ return obj
36
+
37
+ def delete(self, obj: T) -> None:
38
+ self._db.delete(obj)
39
+ self._db.commit()