cbh-test / cbh /api /chat /db_requests.py
brestok's picture
init
4cd2145
raw
history blame contribute delete
663 Bytes
import asyncio
from cbh.api.chat.model import ChatModel
from cbh.core.config import settings
async def create_chat_obj() -> ChatModel:
chat = ChatModel()
await settings.DB_CLIENT.chats.insert_one(chat.to_mongo())
return chat
async def get_all_chats_obj(page_size: int, page_index: int) -> tuple[list[dict], int]:
skip = page_size * page_index
objects, total_count = await asyncio.gather(
settings.DB_CLIENT.chats
.find({})
.sort("_id", -1)
.skip(skip)
.limit(page_size)
.to_list(length=page_size),
settings.DB_CLIENT.chats.count_documents({}),
)
return objects, total_count