Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CRUD FastAPI (SQLite)

A minimal FastAPI CRUD service implementing a simple Item resource with REST endpoints, SQLite via SQLAlchemy, logging, a custom query, transaction demo, and tests.

Features

  • 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.log and console
  • Tests: unit (business logic) + integration (API)

Project Layout

  • 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.txt
  • README.md
  • tests/
    • test_services.py (unit tests)
    • test_api.py (integration tests)

Clone

Clone this repository using SSH:

git clone git@github.com:Anshulp21/CrudFastAPI.git
cd CrudFastAPI

Setup

  1. Create and activate a virtual environment (Windows PowerShell):
python -m venv .venv
.\.venv\Scripts\Activate.ps1
  1. Install dependencies:
pip install -r requirements.txt
  1. Create .env file APP_APIKEY=supersecret123

  2. 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

Example Requests

  • 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"
}

Tests

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

Configuration

  • 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)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages