Skip to main content

fastapi-sa provides a simple integration between FastAPI and SQLAlchemy in your application

Project description

fastapi-sa

GitHub PyPI

fastapi-sa provides a simple integration between FastAPI and SQLAlchemy in your application. you can use decorators or middleware to transaction management.

Installing

install and update using pip:

$ pip install fastapi-sa

Examples

Create models for examples, models.py

from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base

Base = declarative_base()


class Item(Base):
    """ItemModel"""
    __tablename__ = 'item'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))

Usage fastapi middleware

from fastapi import FastAPI
from fastapi_sa.database import db
from fastapi_sa.middleware import DBSessionMiddleware

from models import Item

app = FastAPI()
db.init(url="sqlite+aiosqlite://")
app.add_middleware(DBSessionMiddleware)


@app.put("/items")
async def get_users(name: str):
    async with db() as session:
        item = Item(name=name)
        session.add(item)
    return {'msg': 'ok'}

Usage other asynchronous database operations

import asyncio
from fastapi_sa.database import db, session_ctx

from models import Item

db.init(url="sqlite+aiosqlite://")


@session_ctx
async def add_data(name: str):
    async with db() as session:
        item = Item(name=name)
        session.add(item)


asyncio.run(add_data('item_test'))

Similar design

Based on

Develop

You may need to read the develop document to use SRC Layout in your IDE.

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_sa-0.0.1.dev0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

fastapi_sa-0.0.1.dev0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_sa-0.0.1.dev0.tar.gz.

File metadata

  • Download URL: fastapi_sa-0.0.1.dev0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for fastapi_sa-0.0.1.dev0.tar.gz
Algorithm Hash digest
SHA256 f452df1b5378fc1be048d92878084c63c8344362ecae5c543801d14a37316ebb
MD5 34024ad833396636333ce44bf89496ed
BLAKE2b-256 7ee9a97eba24ac4a8a04dfc4ee0a7366b0fe4665b33eddf1f20a24c2953f54e3

See more details on using hashes here.

File details

Details for the file fastapi_sa-0.0.1.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_sa-0.0.1.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f46043eebe9fafbc90b102a2704aa950559bbe62dff228aa5bd6b996d37214c
MD5 d0dadf52cf86b3887b66743ce3fa1583
BLAKE2b-256 c4965fd82ca87c83dcfdc64518e9a6aa9b52cba07ea9a48b365533e8578b176e

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