Async web framework on top of fastapi and orm
Let's create demo project
$ mkdir demo-project
$ cd demo-project
$ pipenv install joey
$ pipenv shell
$ joey init
Joey will create the following project structure
.
├── alembic.ini
├── asgi.py
├── migrations
│ ├── env.py
│ ├── script.py.mako
│ └── versions
└── settings
├── common.py
├── common.yml
├── development.py
└── __init__.py
Let's add the application to the given project structure
$ joey add hello
# or with autoregister parameter
$ joey add -a hello
Joey will add the following files in a separate folder
.
└── hello
├── __init__.py
├── models.py
└── routes.py
If you use -a
flag then joey automatic register your app and route in project settings file
APPLICATIONS:
- hello
ROUTES:
hello:
prefix: /hello
tags:
- hello
otherwise manually edit settings/common.yml
file.
Now implement model in file hello/models.py
class Item(Model):
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.Text(),
}
Implement a simple route in hello/routes.py
, than can access to database
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from hello.models import Item
class ItemResponse(BaseModel):
text: str
router = APIRouter()
@router.get('/{id}', response_model=ItemResponse)
async def item(id: int) -> ItemResponse:
try:
return await Item.objects.get(id=id)
except Item.DoesNotExist:
raise HTTPException(status_code=404, detail='Item not found')
Next step - create a database, then migrate it and add a couple of elements
$ joey revise 'init database'
$ joey migrate
$ sqlite3 db.sqlite "insert into items (text) values ('hello'), ('joe here');"
Everything is ready, now you can start with uvicorn
$ joey run
# or
$ uvicorn asgi:app --reload
And request data with Swagger UI by http://localhost:8000/docs
.