from typing import Optional
from fastapi import FastAPI
from sqlmodel import (
    SQLModel,
    Field,
    create_engine,
    select,
    Session
)

# Criar engine do banco
engine = create_engine('sqlite:///database.db')


class Pessoa(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    nome: str
    idade: int


# Cria o banco de dados
SQLModel.metadata.create_all(engine)


app = FastAPI()


@app.get('/')
def home():
    return {'message': 'Deu bom'}


@app.get('/pessoa')
def get_pessoa():
    query = select(Pessoa)
    with Session(engine) as session:
        result = session.execute(query).scalars().all()

    return result


@app.get('/pessoa-nome')
def get_pessoa():
    query = select(Pessoa.nome)
    with Session(engine) as session:
        result = session.execute(query).scalars().all()

    return result


@app.get('/pessoa-nome-idade')
def get_pessoa():
    query = select(Pessoa.nome, Pessoa.idade)
    with Session(engine) as session:
        result = session.execute(query).scalars().all()

    return result


sqlmodel.SQLModel.metadata.create_all sqlmodel.Session sqlmodel.Field fastapi.FastAPI sqlmodel.select sqlmodel.create_engine
[((183, 221), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite:///database.db"""'], {}), "('sqlite:///database.db')\n", (196, 221), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((377, 413), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (405, 413), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((422, 431), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (429, 431), False, 'from fastapi import FastAPI\n'), ((284, 321), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (289, 321), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((546, 560), 'sqlmodel.select', 'select', (['Pessoa'], {}), '(Pessoa)\n', (552, 560), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((730, 749), 'sqlmodel.select', 'select', (['Pessoa.nome'], {}), '(Pessoa.nome)\n', (736, 749), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((925, 958), 'sqlmodel.select', 'select', (['Pessoa.nome', 'Pessoa.idade'], {}), '(Pessoa.nome, Pessoa.idade)\n', (931, 958), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((570, 585), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (577, 585), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((759, 774), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (766, 774), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((968, 983), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (975, 983), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n')]