CRUD operations
In connection.py
, create a new Database
class that takes a model as an argument during initialization:
from pydantic import BaseSettings, BaseModel from typing import Any, List, Optional class Database: def __init__(self, model): self.model = model
The model passed during initialization is either the Event
or User
document model class.
Create
Let’s create a method under the Database
class to add a record to the database collection:
async def save(self, document) -> None: await document.create() return
In this code block, we have defined the save
method to take the document, which will be an instance of the document passed to the Database
instance at the point of instantiation.
Read
Let’s create the methods to retrieve...