Implementing CRUD operations
CRUD operations with a RESTful API can be implemented using HTTP methods (POST
, GET
, PUT
, and DELETE
) for web services. This recipe demonstrates how to use SQLAlchemy and asyncio
to build CRUD operations asynchronously on an SQL database with the corresponding endpoints.
Getting ready
Before you start with the recipe, you need to have a database connection and a table in the dataset, as well as a matching class in the code base. If you completed the previous recipe, you should have them ready.
How to do it…
We’ll begin by making an operations.py
module under the app
folder to contain our database operations by following these steps.
- First, we can set up the operation to add a new ticket to the database as follows:
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select from app.database import Ticket async def create_ticket( db_session: AsyncSession, ...