A minimal FastAPI CRUD service implementing a simple Item resource with REST endpoints, SQLite via SQLAlchemy, logging, a custom query, transaction demo, and tests.
- FastAPI + SQLAlchemy (SQLite)
- Item model:
id,title(unique, non-null),description,created_at - REST Endpoints: Create, List (with pagination/filter), Get, Update, Patch, Delete
- Custom query: case-insensitive title contains + total count
- Transaction demo endpoint: create and update in a single transaction
- Logging to rotating file under
logs/app.logand console - Tests: unit (business logic) + integration (API)
app/main.py(FastAPI app entrypoint)models.py(SQLAlchemy ORM models)schemas.py(Pydantic schemas in camelCase JSON)services.py(business logic with camelCase functions)routers.py(REST API routes)database.py(engine/session and Base)config.py(settings via pydantic-settings)logging_config.py(app logging setup)
requirements.txtREADME.mdtests/test_services.py(unit tests)test_api.py(integration tests)
Clone this repository using SSH:
git clone git@github.com:Anshulp21/CrudFastAPI.git
cd CrudFastAPI
- Create and activate a virtual environment (Windows PowerShell):
python -m venv .venv
.\.venv\Scripts\Activate.ps1
- Install dependencies:
pip install -r requirements.txt
-
Create .env file APP_APIKEY=supersecret123
-
Run the app (Uvicorn):
uvicorn app.main:app --reload --port 8000
- Health check:
GET http://127.0.0.1:8000/health - Items API base path:
http://127.0.0.1:8000/items - Interactive docs:
http://127.0.0.1:8000/docs
- Create
POST /items/
{
"title": "Book A",
"description": "First"
}
- List with filter and pagination
GET /items/?skip=0&limit=5&titleContains=book
- Transaction demo
POST /items/transaction-demo
{
"title": "Atomic",
"description": "demo"
}
Run all tests:
python -m pytest -q
Tests include:
- Unit test for
buildSearchPattern()and service logic - Integration test for creating and fetching an item through the API
- Defaults: SQLite database at
crud fastapi/data.db - Environment variables (optional) with prefix
APP_:APP_APPNAME(default: CrudFastAPI)APP_DEBUG(default: true)APP_DATABASEURL(default: sqlite path to data.db)APP_LOGDIR(default:crud fastapi/logs)