25 lines
794 B
Python
25 lines
794 B
Python
import pymongo
|
|
from typing import List
|
|
from pydantic import TypeAdapter
|
|
|
|
from chain_service.database.database import Database
|
|
from chain_service.database.models.chain import Chain
|
|
|
|
|
|
class ChainRepository:
|
|
|
|
def __init__(self, database: Database):
|
|
self.collection = database.get_collection("chains")
|
|
|
|
async def upsert(self, chain: Chain) -> Chain:
|
|
query = {"_id": chain.id}
|
|
payload = chain.model_dump(by_alias=True)
|
|
|
|
await self.collection.replace_one(query, payload, upsert=True)
|
|
return chain
|
|
|
|
async def get_list(self) -> List[Chain]:
|
|
sort_order = [("last_modified", pymongo.DESCENDING)]
|
|
chains = [chain async for chain in self.collection.find().sort(sort_order)]
|
|
return TypeAdapter(List[Chain]).validate_python(chains)
|