feat: 初始提交
This commit is contained in:
19
backend/app/repositories/usage_metric_repository.py
Normal file
19
backend/app/repositories/usage_metric_repository.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from .base import BaseRepository
|
||||
from ..models import UsageMetric
|
||||
|
||||
|
||||
class UsageMetricRepository(BaseRepository[UsageMetric]):
|
||||
model = UsageMetric
|
||||
|
||||
async def get_or_create(self, key: str) -> UsageMetric:
|
||||
result = await self.session.execute(select(UsageMetric).where(UsageMetric.key == key))
|
||||
instance = result.scalars().first()
|
||||
if instance is None:
|
||||
instance = UsageMetric(key=key, value=0)
|
||||
self.session.add(instance)
|
||||
await self.session.flush()
|
||||
return instance
|
||||
Reference in New Issue
Block a user