Giter Site home page Giter Site logo

code-specialist / database-setup-tools Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 323 KB

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

License: MIT License

Python 100.00%
database database-connector database-management fastapi fastapi-sqlalchemy sqlalchemy sqlalchemy-python sqlmodel

database-setup-tools's Introduction

Database Setup Tools for SQLModel / SQLAlchemy

CodeQL Test

Simplified database lifecycle and session management based on SQLModel / SQLAlchemy.

Any contributions are welcome! But we only accept pull requests that come with tests.

Installation

pip install database-setup-tools

Features

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

Usage

In order to use this library, you need some SQLModel or SQLAlchemy tables and a URI to connect to your database. With this in place, the DatabaseSetup instance can be created - which contains all provided tools and also automatically creates the database including all tables.

from sqlmodel import Field, SQLModel
from database_setup_tools import DatabaseSetup


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


# create database & tables, establish connection with session pool
database_setup = DatabaseSetup(
    model_metadata = SQLModel.metadata,
    database_uri="postgresql+psycopg2://postgres:postgres@localhost:5432/database",
)

Note: The DatabaseSetup class acts as singleton, so if you create multiple instances with the same set of parameters, you will always get the same instance back instead of creating a new one.

After the setup is created, you can get a scoped session via the provided session manager and use it for database transactions.

# get scoped session
session = next(database_setup.session_manager.get_session())

# do stuff in session
user = User(name="Alice")
session.add(user)
session.commit()

The DatabaseSetup instance also provides lifecycle methods allowing to manually control the database:

database_setup.create_database()
database_setup.drop_database()

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

database-setup-tools's People

Contributors

dependabot[bot] avatar jonasscholl avatar yannicschroeer avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

database-setup-tools's Issues

Add examples

          > Let's keep it simple. No hosted docs. People use it or they don't. We can't invest that much. At least as long as Tebuto isn't live ๐Ÿ˜„

hmm yes the repo is probably not big enough to invest the effort ๐Ÿ˜„ Should we then just have the examples in an own folder with README.md files and link them in the main readme? Like

README.md
examples/
    fastapi/
       README.md
       app.py
       ...
    pytest/
       README.md
       conftest.py
       ...

I think having the examples as real subprojects is more beneficial than having them in the readme - it's more readable and people can fork it more easily

Originally posted by @JonasScholl in #21 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.