Skip to content

Refactor objects to use Pydantic models #123

@jbriones1

Description

@jbriones1

Currently, a lot of the tables.py files hold classes that have their own patching and serialization functions (see OfficerTerm in src/officers/tables.py. We should just let Pydantic's built in features handle that since it can handle serialization, patching, and validation. The data flow would look something like:

  1. GET request -> server
  2. Fetch the entry/entries from the database as SQL table entries <Table>DB
  3. SQL Table Model -> (process data) -> Pydantic Model <Model>.model_validate(<TableDB>)
  4. Use Pydantic's serialize <Model>.model_dump_json() -> Send reponse

For requests that take in data, like POST/PATCH/PUT:

  1. POST/PATCH/PUT -> server
  2. [if PATCH/PUT] fetch entry(ies) from database -> put in Pydantic model, otherwise create a new Pydantic model
  3. update the edited/new entry with the data, using Pydantic's function to do it
  4. Use Pydantic's validation method to ensure the data is good
  5. Convert to a SQL Table Model and send it to the database

To do this we'd need to:

  • Create a valid Pydantic model that covers all of the CRUD operations with proper validation fields that match the SQL table models (sort of done)
  • Remove the validation, serialization, and patching functions from each of the SQL table models
  • Update all the URL endpoints with the Pydantic models (sort of done)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions