Skip to content

SQLAlchemy ORM implementation repository needed 🗡️ Provided draft implementation #5

Closed
@rafsaf

Description

@rafsaf

Hi!

Thanks for your great work!. As a short background, I made small minimal repo with template for async starter for FastAPI (based on official template) and got a question about including FastAPI Users. I like async style of 1.4 SQLAlchemy ORM and prefer it over whatsoever databases.

But in the docs:
image

Here's the working adapter for SQLAlchemy ORM: https://github.com/rafsaf/fastapi_users_db_sqlalchemy_orm

I tried to implement it first before creating an issue, seems to be working just fine, it's not very much different from this repo implementation, I of course changed pytest fixtures (sqlalchemy orm + aiosqlite instead of databases) but the tests logic is the same. Disclaimer: I didn't spend a lot of time on it, review is a must (It's only a piece of code, not a full repo setup too)

The docs like here for new adapter may look like that

import asyncio

from fastapi_users.db import SQLAlchemyORMBaseUserTable, SQLAlchemyORMUserDatabase
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from sqlalchemy.orm.session import sessionmaker

from .models import UserDB

DATABASE_URL = "sqlite+aiosqlite:///./test.db"

Base: DeclarativeMeta = declarative_base()

class UserTable(Base, SQLAlchemyORMBaseUserTable):
    pass

async_engine = create_async_engine(DATABASE_URL, pool_pre_ping=True)
async_session = sessionmaker(async_engine, expire_on_commit=False, class_=AsyncSession)


async def setup():
    async with async_engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

asyncio.run(setup())


async def get_user_db():
    async with async_session() as session:
        yield SQLAlchemyORMUserDatabase(UserDB, session, UserTable)

I'm waiting for your opinion about moving/creating new adapter in another repository so this topic can be continued :) Thanks a lot 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions