Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fastapi To Robyn #821

Open
Xingsandesu opened this issue May 25, 2024 · 2 comments
Open

Fastapi To Robyn #821

Xingsandesu opened this issue May 25, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Xingsandesu
Copy link

I want to migrate my fastapi application to robyn. I read the documentation of robyn. Its usage is indeed very similar to fastapi, but it does not support pydantic. I think it would be better if robyn could support pydantic for data verification, so that my fastapi application can basically be seamlessly migrated to robyn. Secondly, I am still concerned about whether robyn can fully support sqlalchemy's async connection. I read the official document and the example above uses synchronous pymysql, not asynchronous aiomysql. I really like this project, thank you for your efforts

@Xingsandesu Xingsandesu added the enhancement New feature or request label May 25, 2024
@sansyrox
Copy link
Member

Hey @Xingsandesu 👋

Thank you for opening the issue 😄 .

I think it would be better if robyn could support pydantic for data verification, so that my fastapi application can basically be seamlessly migrated to robyn.

This is a top priority issue for us! It will be definitely implemented within the next few weeks 😄

Secondly, I am still concerned about whether robyn can fully support sqlalchemy's async connection. I read the official document and the example above uses synchronous pymysql, not asynchronous aiomysql.

Robyn supports all sorts of async tasks. But if you have something in mind that you'd like to clarify, I would be happy to have a look

@Xingsandesu
Copy link
Author

Robyn supports all sorts of async tasks. But if you have something in mind that you'd like to clarify, I would be happy to have a look

thank you very much for your work😄.
This is part of my database code.This example uses sqlalchemy.ext.asyncio and aiomysql to implement an asynchronous database session, and uses the fastapi dependency to use an asynchronous database session. This will prevent various database operations from forming performance bottlenecks due to the bottleneck of pymysql. I think I should be able to use a similar method if I migrate from fastapi to Robyn. Thank you very much for your work. When pydantic is supported, I will cooperate with you to test this version as soon as possible if possible.

setting.py

....
DATABASE_URL = f"mysql+aiomysql://{DATABASES['USER']}:{DATABASES['PASSWORD']}@{DATABASES['HOST']}:{DATABASES['PORT']}/{DATABASES['NAME']}"
....

models.py

from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker, declarative_base
from settings import DATABASE_URL, DATABASE_ENGINE_ECHO, DATABASE_ENGINE_POOL_SIZE, DATABASE_ENGINE_MAX_OVERFLOW, DATABASE_ENGINE_POOL_TIMEOUT, DATABASE_SESSION_EXPIRE_ON_COMMIT


engine = create_async_engine(
    DATABASE_URL, 
    echo=DATABASE_ENGINE_ECHO, 
    pool_size=DATABASE_ENGINE_POOL_SIZE, 
    max_overflow=DATABASE_ENGINE_MAX_OVERFLOW, 
    pool_timeout=DATABASE_ENGINE_POOL_TIMEOUT
)

SessionLocal = sessionmaker(
    engine, 
    expire_on_commit=DATABASE_SESSION_EXPIRE_ON_COMMIT, 
    class_=AsyncSession
)

Base = declarative_base()

async def get_db():
    session = SessionLocal()
    try:
        yield session
    except Exception as e:
        logger.error(f"error::{str(e)}")
    finally:
        await session.close()

...

view.py

...
async def add_user(user: UserCreate, db: AsyncSession = Depends(get_db)):
    if await record_exists(db, "users", {"userName": user.userName}):
        raise HTTPException(status_code=400, detail="User name already exists")
    else:
        db_user = User(**user.model_dump())
        db.add(db_user)
        await db.commit()
        await db.refresh(db_user)
        return db_user
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants