responder icon indicating copy to clipboard operation
responder copied to clipboard

What is the best database with "responder"?

Open theblackturtle opened this issue 5 years ago • 6 comments

Thanks.

theblackturtle avatar Nov 19 '18 02:11 theblackturtle

I think its database agnostic, we can hook onto to SqlAlchemy or pymongo. But I don't know how async /ASGI based web frameworks work with databases

sanchitrk avatar Nov 19 '18 04:11 sanchitrk

There are multiple async drivers asyncpg, aiosqlite, aiomysql, if you're looking for an ORM there's tortoise a pure asyncio ORM and some layers that add asyncio to existing sync ORMs like peewee-async(peewee), gino(SqlAlchemy),...

taoufik07 avatar Nov 19 '18 08:11 taoufik07

Here is a really simple example using an sqlite database with Tortoise ORM:

# yourapp.models.py

from tortoise.models import Model
from tortoise import fields


class User(Model):
    id = fields.IntField(pk=True)
    name = fields.TextField()

    def __str__(self):
        return self.name
# app.py

import responder
from tortoise import Tortoise
from yourapp.models import User


api = responder.API()


@api.on_event("shutdown")
async def close_db_connection(self):
    await Tortoise.close_connections()


@api.route("/")
async def home(req, resp):

    await Tortoise.init(
        db_url="sqlite://yourapp.db", modules={"models": ["yourapp.models"]}
    )

    # Create a test user
    await User.create(name="Test User")
    # Fetch the new user
    user = await User.first()

    resp.text = f"Hello, {user.name}"
# init_db.py

from tortoise import Tortoise,  run_async


async def init():
    await Tortoise.init(
        db_url="sqlite://yourapp.db", modules={"models": ["yourapp.models"]}
    )
    # Generate the schema
    await Tortoise.generate_schemas()


run_async(init())

jordaneremieff avatar Nov 19 '18 11:11 jordaneremieff

The creator of this project also has https://github.com/kennethreitz/records to interface databases with it, but I'm not shure if it can ran in async

Serkan-devel avatar Nov 25 '18 12:11 Serkan-devel

Here's a list of async databases that might fit here https://github.com/timofurrer/awesome-asyncio#database-drivers

Serkan-devel avatar Dec 28 '18 17:12 Serkan-devel

Working towards getting a good baseline for async database support here: https://github.com/encode/databases

For a higher level API Gino is probably the most mature option at the moment. You need migrations support for anything serious, and because it builds on SQLAlchemy you can use alembic with it just fine.

tomchristie avatar Feb 12 '19 21:02 tomchristie