responder
responder copied to clipboard
What is the best database with "responder"?
Thanks.
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
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)
,...
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())
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
Here's a list of async databases that might fit here https://github.com/timofurrer/awesome-asyncio#database-drivers
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.