MemoryService.create_relation implemented

This commit is contained in:
phernandez
2024-12-07 17:51:52 -06:00
parent b285b4619f
commit 122dbd7d31
6 changed files with 137 additions and 74 deletions
+10 -21
View File
@@ -1,12 +1,11 @@
"""Service for managing entities in the database."""
from datetime import datetime, UTC
from pathlib import Path
from typing import Optional
from basic_memory.models import Entity as DbEntity
from basic_memory.repository import EntityRepository
from basic_memory.schemas import Entity
from . import ServiceError, DatabaseSyncError
from basic_memory.models import Entity as EntityModel
from . import ServiceError
class EntityService:
"""
@@ -18,42 +17,32 @@ class EntityService:
self.project_path = project_path
self.entity_repo = entity_repo
async def create_entity(self, entity: Entity) -> Entity:
async def create_entity(self, entity: Entity) -> EntityModel:
"""Create a new entity in the database."""
# Create DB record
db_data = {
**entity.model_dump(),
"created_at": datetime.now(UTC),
"updated_at": datetime.now(UTC)
}
await self.entity_repo.create(db_data)
return entity
return await self.entity_repo.create(db_data)
async def get_entity(self, entity_id: str) -> Entity:
async def get_entity(self, entity_id: str) -> EntityModel:
"""Get entity by ID."""
db_entity = await self.entity_repo.find_by_id(entity_id)
if not db_entity:
raise ServiceError(f"Entity not found: {entity_id}")
return Entity(
id=db_entity.id,
name=db_entity.name,
entity_type=db_entity.entity_type
)
return db_entity
async def get_by_name(self, name: str) -> Entity:
# TODO name is not uniaue
async def get_by_name(self, name: str) -> EntityModel:
"""Get entity by name."""
db_entity = await self.entity_repo.find_by_name(name)
if not db_entity:
raise ServiceError(f"Entity not found: {name}")
return Entity(
id=db_entity.id,
name=db_entity.name,
entity_type=db_entity.entity_type
)
return db_entity
async def delete_entity(self, entity_id: str) -> bool:
"""Delete entity from database."""
await self.entity_repo.delete(entity_id)
return True
return await self.entity_repo.delete(entity_id)