awesome-fastapi-projects/app/create_index.py
Vladyslav Fedoriuk f608ff0500 Create index.json
2023-08-28 21:29:03 +02:00

54 lines
1.3 KiB
Python

"""Create index.json file from database."""
import json
from pathlib import Path
import aiofiles
import sqlalchemy.orm
import typer
from app.database import Repo
from app.models import RepoDetail
from app.uow import async_session_uow
INDEX_PATH = Path(__file__).parent.parent / "index.json"
async def create_index() -> None:
"""
Create index.json file from database.
Creates an index which is going to be used by the frontend.
:return: None
"""
async with async_session_uow() as session, aiofiles.open(
INDEX_PATH, "w"
) as index_file:
await index_file.write(
json.dumps(
{
"repos": [
RepoDetail.model_validate(repo).model_dump()
async for repo in (
await session.stream_scalars(
sqlalchemy.select(Repo)
.order_by(Repo.id)
.options(sqlalchemy.orm.selectinload(Repo.dependencies))
)
)
],
},
indent=4,
)
)
def main() -> None:
"""Create index.json file from database."""
import asyncio
asyncio.run(create_index())
if __name__ == "__main__":
typer.run(main)