Spaces:
Runtime error
Runtime error
Commit ·
709ca98
1
Parent(s): 7d6dd79
Add base, user, and session repository classes for MongoDB integration
Browse files
src/repositories/__init__.py
CHANGED
|
@@ -1,3 +1,12 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
__version__ = "0.1.0"
|
| 3 |
__author__ = "Ramanjit Singh"
|
|
|
|
| 1 |
+
from ._user_repository import UserRepository
|
| 2 |
+
from ._base_repository import BaseRepository
|
| 3 |
+
from ._session_repository import SessionRepository
|
| 4 |
+
|
| 5 |
+
__all__ = [
|
| 6 |
+
"UserRepository",
|
| 7 |
+
"BaseRepository",
|
| 8 |
+
"SessionRepository",
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
__version__ = "0.1.0"
|
| 12 |
__author__ = "Ramanjit Singh"
|
src/repositories/_base_repository.py
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from beanie import Document
|
| 2 |
+
from typing import Type, TypeVar, List, Dict
|
| 3 |
+
|
| 4 |
+
T = TypeVar("T", bound=Document)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BaseRepository:
|
| 8 |
+
def __init__(self, model: Type[T]):
|
| 9 |
+
self.model = model
|
| 10 |
+
|
| 11 |
+
async def insert_one(self, data: T):
|
| 12 |
+
try:
|
| 13 |
+
return await data.insert()
|
| 14 |
+
except Exception as e:
|
| 15 |
+
raise ValueError(f"Failed to insert data: {str(e)}")
|
| 16 |
+
|
| 17 |
+
async def get_all(self, page: int = 1, page_size: int = None, filter_by: Dict = None, order_by: Dict = None):
|
| 18 |
+
query = filter_by or {}
|
| 19 |
+
cursor = self.model.find(query)
|
| 20 |
+
|
| 21 |
+
if order_by:
|
| 22 |
+
cursor = cursor.sort(order_by)
|
| 23 |
+
|
| 24 |
+
if page_size is not None:
|
| 25 |
+
skip = (page - 1) * page_size
|
| 26 |
+
cursor = cursor.skip(skip).limit(page_size)
|
| 27 |
+
|
| 28 |
+
return await cursor.to_list(length=page_size if page_size else None)
|
| 29 |
+
|
| 30 |
+
async def get_by_id(self, _id: str):
|
| 31 |
+
return await self.model.get(_id)
|
| 32 |
+
|
| 33 |
+
async def update(self, _id: str, update_data: dict):
|
| 34 |
+
document = await self.model.get(_id)
|
| 35 |
+
if not document:
|
| 36 |
+
raise ValueError(f"Document with id {_id} not found")
|
| 37 |
+
|
| 38 |
+
updated_fields = {**document.model_dump(), **update_data.model_dump()}
|
| 39 |
+
return await document.set(updated_fields)
|
| 40 |
+
async def delete(self, _id: str):
|
| 41 |
+
|
| 42 |
+
document = await self.model.get(_id)
|
| 43 |
+
if not document:
|
| 44 |
+
raise ValueError(f"Document with id {_id} not found")
|
| 45 |
+
return await document.delete()
|
| 46 |
+
|
| 47 |
+
async def count_documents(self):
|
| 48 |
+
return await self.model.count()
|
src/repositories/_session_repository.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.models import Session
|
| 2 |
+
|
| 3 |
+
from ._base_repository import BaseRepository
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class SessionRepository(BaseRepository):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super().__init__(model=Session)
|
src/repositories/_user_repository.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.models import User
|
| 2 |
+
|
| 3 |
+
from ._base_repository import BaseRepository
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class UserRepository(BaseRepository):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super().__init__(model=User)
|