Skip to main content

Tools to easily setup databases opinionated towards FastAPI and SQLModel

Project description

Database tools

Easy to understand and use tools that help you to create databases and interact with them.

Installation

pip install database-setup-tools

Features

  • Database creation on app startup
  • Thread-safe database session manager
  • Opinionated towards FastAPI and SQLModel but feasible with any other framework or pure sqlalchemy
  • Easily use a local database in your tests

Planned features

  • Database migrations with Alembic

Example

import random

import uvicorn
from fastapi import FastAPI, Depends
from sqlmodel import Session, SQLModel, Field

from database_setup_tools.session_manager import SessionManager
from database_setup_tools.setup import DatabaseSetup

DATABASE_URI = "sqlite:///test.db"

app = FastAPI()
session_manager = SessionManager(database_uri=DATABASE_URI)


class User(SQLModel, table=True):
    """ User model """
    id: int = Field(index=True, primary_key=True)
    name: str


model_metadata = SQLModel.metadata


@app.post('/users/', response_model=User)
def add_random_user(session: Session = Depends(session_manager.get_session)):
    """ Endpoint to add a user with a random name """
    user = User(name=f'User {random.randint(0, 100)}')
    session.add(user)
    session.commit()
    return user


@app.get('/users/', response_model=list[User])
def get_all_users(session: Session = Depends(session_manager.get_session)):
    """ Endpoint to get all users """
    return session.query(User).all()


if __name__ == '__main__':
    database_setup = DatabaseSetup(model_metadata=model_metadata, database_uri=DATABASE_URI)
    uvicorn.run(app, host='0.0.0.0', port=8080)

Example for pytest

conftest.py

database_setup = DatabaseSetup(model_metadata=model_metadata, database_uri=DATABASE_URI)


def pytest_sessionstart(session):
    database_setup.drop_database()
    database_setup.create_database()

test_users.py

session_manager = SessionManager(database_uri=DATABASE_URI)


@pytest.fixture
def session():
    with session_manager.get_session() as session:
        yield session


def test_create_user(session: Session):
    user = User(name='Test User')
    session.add(user)
    session.commit()
    assert session.query(User).count() == 1
    assert session.query(User).first().name == 'Test User'

Development

Testing

  1. Spin up databases for local integration tests: docker-compose -f tests/docker-compose.yaml up -d
  2. Create virtual environment & install dependencies: poetry install
  3. Run tests: poetry run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

database_setup_tools-1.3.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

database_setup_tools-1.3.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file database_setup_tools-1.3.1.tar.gz.

File metadata

  • Download URL: database_setup_tools-1.3.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1035-azure

File hashes

Hashes for database_setup_tools-1.3.1.tar.gz
Algorithm Hash digest
SHA256 16a94eafe2ea39051a8f4be108544fb9eebcffe1562033d607468b5772ad5e66
MD5 1876b716f301a99b646a42449753425b
BLAKE2b-256 1a2ab636cd37e2fd77df83d38f8b6624a8a6a2ffa69f338da49cd90e3e304bdb

See more details on using hashes here.

File details

Details for the file database_setup_tools-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for database_setup_tools-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02b2bcb9f4240406baf89d1e929313ab442571ed4a56cc19f51166089f688ebb
MD5 9e76b9363366e664b3ccd41ff5f64b48
BLAKE2b-256 2f272c1924b73427de9437edb8e238f833f4d83e7d037c89c1b013feeb74680a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page