Skip to content

Instantly share code, notes, and snippets.

@AdamOswald
Forked from morkesiden/fast_api_page.py
Created September 23, 2022 18:16
Show Gist options
  • Select an option

  • Save AdamOswald/88b36f79fa803f0072b6f265c5b77016 to your computer and use it in GitHub Desktop.

Select an option

Save AdamOswald/88b36f79fa803f0072b6f265c5b77016 to your computer and use it in GitHub Desktop.

Revisions

  1. @dineshkumarkb dineshkumarkb created this gist Sep 20, 2022.
    37 changes: 37 additions & 0 deletions fast_api_page.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    from fastapi_pagination import Page, paginate, add_pagination
    from pydantic import BaseModel
    from fastapi import FastAPI
    from sqlalchemy.exc import SQLAlchemyError
    from sqlalchemy.orm import sessionmaker, declarative_base
    from datetime import date

    app = FastAPI(title=" A simple pagination learning exercise",
    debug=True)
    add_pagination(app)

    class Employee(Base):

    __tablename__ = "employee"

    empid = Column(Integer, primary_key=True)
    empname = Column(String)
    dept = Column(Integer)
    location = Column(String)
    dob = Column(Date)


    class EmployeeOut(BaseModel):
    empid: int
    empname: str
    dept: int
    location: str
    dob: date

    class Config:
    orm_mode = True

    @app.get(path="/api/employees/all", name="Gets all employees", response_model=Page[EmployeeOut])
    async def get_all_employees():
    conn = connect_to_db()
    results = conn.query(Employee).all()
    return paginate(results)