diff --git a/pyproject.toml b/pyproject.toml index cecd388..5a88aa7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ classifiers = [ "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", ] -dependencies = [ 'rich', 'sqlmodel', 'typer', 'iterfzf'] +dependencies = [ 'rich', 'sqlmodel', 'typer', 'iterfzf', 'fastapi', 'uvicorn'] [project.urls] Documentation = "https://github.com/waylonwalker/sqlmodel-base#readme" diff --git a/sqlmodel_base/base.py b/sqlmodel_base/base.py index 80f91fb..75d9ecb 100644 --- a/sqlmodel_base/base.py +++ b/sqlmodel_base/base.py @@ -1,13 +1,14 @@ -import json from typing import Optional import typer +import uvicorn +from fastapi import APIRouter, FastAPI from iterfzf import iterfzf -from pydantic import BaseModel, validator +from pydantic import BaseModel from pydantic_core._pydantic_core import PydanticUndefinedType from rich.console import Console from sqlalchemy import func -from sqlmodel import Field, Session, SQLModel, create_engine, select +from sqlmodel import Session, SQLModel, select from sqlmodel_base.database import get_engine @@ -177,6 +178,31 @@ class Base(SQLModel): item.update() console.print(item) + @classmethod + def api(cls): + api = FastAPI( + title="FastAPI", + version="0.1.0", + docs_url=None, + redoc_url=None, + openapi_url=None, + # openapi_tags=tags_metadata, + # dependencies=[Depends(set_user), Depends(set_prefers)], + ) + + api.include_router(cls.router()) + + return api + + @classmethod + def router(cls): + router = APIRouter() + router.add_api_route("/get/", cls.get, methods=["GET"]) + router.add_api_route("/list/", cls.all, methods=["GET"]) + router.add_api_route("/create/", cls.interactive_create, methods=["POST"]) + router.add_api_route("/update/", cls.interactive_update, methods=["PUT"]) + return router + @classmethod @property def cli(cls): @@ -206,37 +232,16 @@ class Base(SQLModel): ) ) + @app.command() + def api(): + cls.run_api() + @app.command() def update(): console.print(cls.interactive_update()) return app - -# replace with alembic commands -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson").create() - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador").create() - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48).create() - - -def page_heroes(): - next_page = 1 - while next_page: - page = Hero.get_page(page=next_page, page_size=2) - console.print(page) - next_page = page.next_page - - -def main(): - create_db_and_tables() - create_heroes() - page_heroes() - - -if __name__ == "__main__": - main() + @classmethod + def run_api(cls): + uvicorn.run(cls.api(), host="127.0.0.1", port=8000) diff --git a/sqlmodel_base/database.py b/sqlmodel_base/database.py index 90ceedb..96e1617 100644 --- a/sqlmodel_base/database.py +++ b/sqlmodel_base/database.py @@ -1,6 +1,6 @@ from functools import lru_cache -from sqlmodel import Field, Session, SQLModel, create_engine, select +from sqlmodel import Session, SQLModel, create_engine sqlite_file_name = "database.db" sqlite_url = f"sqlite:///{sqlite_file_name}" @@ -8,8 +8,6 @@ sqlite_url = f"sqlite:///{sqlite_file_name}" @lru_cache def get_engine(): - from sqlmodel_base.hero.models import Hero - from sqlmodel_base.team.models import Team engine = create_engine(sqlite_url) SQLModel.metadata.create_all(engine) diff --git a/sqlmodel_base/hero/cli.py b/sqlmodel_base/hero/cli.py index 458d319..123b337 100644 --- a/sqlmodel_base/hero/cli.py +++ b/sqlmodel_base/hero/cli.py @@ -1,12 +1,8 @@ -import json -import typer -from iterfzf import iterfzf from rich.console import Console from sqlmodel_base.database import get_engine from sqlmodel_base.hero.models import Hero -from sqlmodel_base.team.models import Team engine = get_engine() diff --git a/sqlmodel_base/hero/models.py b/sqlmodel_base/hero/models.py index fa90c87..2f399eb 100644 --- a/sqlmodel_base/hero/models.py +++ b/sqlmodel_base/hero/models.py @@ -1,9 +1,8 @@ from typing import Optional -from pydantic import BaseModel, validator +from pydantic import validator from rich.console import Console -from sqlalchemy import func -from sqlmodel import Field, Session, SQLModel, create_engine, select +from sqlmodel import Field from sqlmodel_base.base import Base diff --git a/sqlmodel_base/team/models.py b/sqlmodel_base/team/models.py index e57384e..791abe3 100644 --- a/sqlmodel_base/team/models.py +++ b/sqlmodel_base/team/models.py @@ -1,6 +1,5 @@ from typing import Optional -from rich.console import Console from sqlmodel import Field from sqlmodel_base.base import Base