learn-sql-model/templates/model/learn_sql_model/cli/{{modelname.lower()}}.py.jinja
2023-06-09 16:04:58 -05:00

92 lines
2.4 KiB
Django/Jinja

import sys
from typing import List, Optional, Union
from engorgio import engorgio
from rich.console import Console
import typer
from learn_sql_model.config import get_config
from learn_sql_model.factories.{{modelname.lower()}} import {{modelname}}Factory
from learn_sql_model.models.{{modelname.lower()}} import (
{{modelname}},
{{modelname}}Create,
{{modelname}}Delete,
{{modelname}}Read,
{{modelname}}Update,
)
{{modelname.lower()}}_app = typer.Typer()
config = get_config()
@{{modelname.lower()}}_app.callback()
def {{modelname.lower()}}():
"model cli"
@{{modelname.lower()}}_app.command()
@engorgio(typer=True)
def get(
{{modelname.lower()}}_id: Optional[int] = typer.Argument(default=None),
) -> Union[{{modelname}}, List[{{modelname}}]]:
"get one {{modelname.lower()}}"
{{modelname.lower()}} = {{modelname}}Read.get(id={{modelname.lower()}}_id)
Console().print({{modelname.lower()}})
return {{modelname.lower()}}
@{{modelname.lower()}}_app.command()
@engorgio(typer=True)
def list(
where: Optional[str] = None,
offset: int = 0,
limit: Optional[int] = None,
) -> Union[{{modelname}}, List[{{modelname}}]]:
"list many {{modelname.lower()}}s"
{{modelname.lower()}} = {{modelname}}Read.list(where=where, offset=offset, limit=limit)
Console().print({{modelname.lower()}})
return {{modelname.lower()}}
@{{modelname.lower()}}_app.command()
@engorgio(typer=True)
def create(
{{modelname.lower()}}: {{modelname}}Create,
) -> {{modelname}}:
"create one {{modelname.lower()}}"
{{modelname.lower()}}.post()
@{{modelname.lower()}}_app.command()
@engorgio(typer=True)
def update(
{{modelname.lower()}}: {{modelname}}Update,
) -> {{modelname}}:
"update one {{modelname.lower()}}"
{{modelname.lower()}}.update()
@{{modelname.lower()}}_app.command()
@engorgio(typer=True)
def delete(
{{modelname.lower()}}: {{modelname}}Delete,
) -> {{modelname}}:
"delete a {{modelname.lower()}} by id"
{{modelname.lower()}}.delete()
@{{modelname.lower()}}_app.command()
@engorgio(typer=True)
def populate(
n: int = 10,
) -> {{modelname}}:
"Create n number of {{modelname.lower()}}s"
if config.env == "prod":
Console().print("populate is not supported in production")
sys.exit(1)
for {{modelname.lower()}} in {{modelname}}Factory().batch(n):
{{modelname.lower()}} = {{modelname}}Create(**{{modelname.lower()}}.dict())
{{modelname.lower()}}.post()