Skip to main content

A reusable SQLAlchemy extension for FastAPI

Project description

fastapi-sqlalchemy-ease

fastapi-sqlalchemy-ease is a singleton-based SQLAlchemy extension for FastAPI. It provides a centralized interface for database initialization, model declaration, and session management, inspired by the simplicity of Flask-SQLAlchemy.

◽ Key Features

  • Singleton Design: Ensures a single database instance across your entire FastAPI application.

  • Consolidated API: Access all SQLAlchemy types (Integer, String, ForeignKey, etc.) directly from the db object.

  • Auto-Cleanup: Includes a generator-based session handler designed for FastAPI's Depends to ensure sessions are always closed.

  • Built-in Lifecycle Management: Simple methods for create_all() and drop_all().

◽ Installation

pip install fastapi-sqlalchemy-ease

◽ Usage Guide

1. Basic setup

Initialize your database instance in a centralized file (e.g. database.py).

from fastapi_sqlalchemy_ease import SQLAlchemy  

'''Create the singleton instance''' 
db = SQLAlchemy()  

'''Initialize with your URI'''  
DATABASE_URI = "sqlite:///./test.db"  

'''Use connect_args={"check_same_thread": False} for SQLite'''  
db.init_app(DATABASE_URI, connect_args={"check_same_thread": False})  

2. Defining Models

No need to import types from SQLAlchemy; use the db instance directly.

class User(db.Model):  
    __tablename__ = "users"  

    id = db.Column(db.Integer, primary_key=True)  
    username = db.Column(db.String, unique=True, nullable=False)  
    created_at = db.Column(db.DateTime)  

3. Creating Tables

You can trigger table creation easily.

db.create_all()  

3. Database Operations in Routes

Use db.Session with FastAPI's Depends to get a clean session for every request.

from fastapi import FastAPI, Depends  
from .. import db  

app = FastAPI()  

@app.get("/users")  
def read_users(session: Session = Depends(db.Session)):  
    users = session.query(User).all()  
    return users  

◽ Available Attributes

Your db instance provides easy access to standard SQLAlchemy types and constraints:

Category - Available Attributes

Data Types - String, Integer, Float, Boolean, Text, Date, DateTime, JSON, Numeric
Constraints - ForeignKey, UniqueConstraint, CheckConstraint, Index
ORM - relationship, Table, Column

◽ Error Handling

The library includes a DatabaseNotInitializedError. If you attempt to access db.Model, db.Session, or lifecycle methods before calling db.init_app(), a clear exception will be raised to help you debug quickly.

📄 License

Distributed under the MIT License.

--

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

fastapi_sqlalchemy_ease-0.1.6.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_sqlalchemy_ease-0.1.6-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_sqlalchemy_ease-0.1.6.tar.gz.

File metadata

  • Download URL: fastapi_sqlalchemy_ease-0.1.6.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_sqlalchemy_ease-0.1.6.tar.gz
Algorithm Hash digest
SHA256 a7d1648b7c266af6b293dc4e71d4814222d5ed4e7b963c90f9d387b99fc2b9e4
MD5 72c749bffabfb126a0be070f2f8b7092
BLAKE2b-256 5f4686489c293fd8c9de7f874e6286967de04545d1a0fb132a5a555ed6e4e1e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_sqlalchemy_ease-0.1.6.tar.gz:

Publisher: release.yml on hardik-soni12/fastapi-sqlalchemy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_sqlalchemy_ease-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_sqlalchemy_ease-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c728d4e0863e8d6c9c9c2260c363a6b9c5a34978f9f33124f63e7b99e1d91bc0
MD5 314e038c01a05cbe8a2fcc951fe0a135
BLAKE2b-256 57e8b6f08ee5ce12390e367a89530169526712d206eeb6bd905d6286db8e9188

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_sqlalchemy_ease-0.1.6-py3-none-any.whl:

Publisher: release.yml on hardik-soni12/fastapi-sqlalchemy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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