A simple CRUD application built with FastAPI, SQLModel, and MySQL for managing items.
- Create, Read, Update, and Delete (CRUD) operations
- Uses SQLModel for database models
- Environment variables managed through
.env
file - FastAPI Lifespan context for managing application lifecycle
- RESTful API with auto-generated documentation with OpenAI
- Python 3.8 or higher
- MySQL server installed
- Virtual environment
-
Clone the repository:
git clone https://github.com/rahulkumar-fullstack/awesome-fastapi-mysql-crud.git cd awesome-fastapi-mysql-crud
-
Install dependencies:
pip install -r requirements.txt
-
Create and configure
.env
file:DATABASE_URL=mysql+mysqlconnector://username:password@localhost:3306/mydatabase
crud_fastapi/
├── .env # Environment file
├── app/
│ ├── __init__.py # Make the folder a Python package
│ ├── main.py # FastAPI main file
│ ├── database.py # Database connection setup
│ ├── models.py # SQLModel models
│ ├── crud.py # CRUD operations
│ ├── routers/
│ │ ├── items.py # API routes
├── requirements.txt # Dependencies
-
Run the application:
uvicorn app.main:app --reload
-
Access the API documentation at:
GET /items/
- Retrieve all itemsPOST /items/
- Create a new itemGET /items/{item_id}
- Retrieve an item by IDPUT /items/{item_id}
- Update an existing itemDELETE /items/{item_id}
- Delete an item by ID
This project is licensed under the MIT License. See the LICENSE file for details.