https://github.com/mfkimbell/python-orm-setup
https://github.com/mfkimbell/python-orm-setup
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mfkimbell/python-orm-setup
- Owner: mfkimbell
- Created: 2024-11-18T21:18:55.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-02-20T23:29:47.000Z (5 months ago)
- Last Synced: 2025-02-21T00:43:55.278Z (5 months ago)
- Language: Python
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python-orm-setup
### Overview
A Python project demonstrating the use of modern ORM (Object Relational Mapping) techniques with SQLAlchemy to manage a relational database. The project leverages the repository pattern for clean and maintainable data access logic and includes a CRUD interface for managing movie data in a PostgreSQL database.### Tools Used
* `PDM` For python package management
* `SQLAlchemy` For object relational mapping to database
* `Postgres` For relational data storage```
project_root/
├── crud/
│ └── movies.py # CRUD operations for the "movies" table
├── models/
│ └── __init__.py # Base and engine setup
│ └── movies.py # Movies table definition
├── db.py # Database session management
├── main.py # Entry point to demonstrate the functionality
├── pyproject.toml # PDM project file
```### repository pattern
The repository pattern is a design approach to abstract data access logic and provide a clean interface for interacting with the underlying database. In your case, the crud.movies module acts as the repository.
```python
from crud.movies import (
create_movie,
get_movies,
get_movie_by_id,
update_movie,
delete_movie,
)
from sqlalchemy.orm import Sessionclass Movie:
def __init__(self, db: Session):
self.db = dbdef create(self, title: str, director: str, year: int):
new_movie = create_movie(self.db, title=title, director=director, year=year)
return {
"id": new_movie.id,
"title": new_movie.title,
"director": new_movie.director,
"year": new_movie.year,
}```
We leave the creation logic to the repository `crud.movies` and we can keep business logic in this class (think LLD questions)### Running the program
### Database end state
### Adding an API
```
project_root/
├── api/
│ └── movies.py # API endpoints for movie operations
│ └── __init__.py # To define the FastAPI app
├── crud/
│ └── movies.py # Existing CRUD operations
├── models/
│ └── __init__.py # Base and engine setup
│ └── movies.py # Movies table definition
├── db.py # Database session management
├── main.py # Start FastAPI app here
├── pyproject.toml # PDM project file
```Add a movie router
```python
from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from crud.movies import create_movie, get_movies, get_movie_by_id, update_movie, delete_movie
from db import get_dbrouter = APIRouter(prefix="/movies", tags=["movies"])
@router.post("/", response_model=dict)
def create_movie_api(title: str, director: str, year: int, db: Session = Depends(get_db)):
movie = create_movie(db, title, director, year)
return {"id": movie.id, "title": movie.title, "director": movie.director, "year": movie.year}@router.get("/", response_model=list)
def read_movies(db: Session = Depends(get_db)):
return get_movies(db)
```Add movie router to main
```python
from fastapi import FastAPI
from api.movies import router as movie_routerapp = FastAPI(title="Movie Database API", version="1.0")
app.include_router(movie_router)
# Root endpoint
@app.get("/")
def read_root():
return {"message": "Welcome to the Movie Database API!"}
```