Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's Build an ORM - PyOhio 2019

Let's Build an ORM - PyOhio 2019

Applications rely on data, and relational databases are a convenient way to organize structured information. Object-relational mappers like SQLAlchemy and Django’s ORM are complex libraries, but they aren’t black magic. De-mystify some of the magic as we build the basics of an ORM in under an hour.

Greg Back

July 28, 2019
Tweet

More Decks by Greg Back

Other Decks in Programming

Transcript

  1. (Potential) Responsibilities of an ORM • create database • define

    tables • define fields ◦ size/length ◦ nullable ◦ default values • define foreign keys • define indices • define relationships ◦ one-to-many ◦ many-to-many ◦ one-to-one ◦ inverse relationships • define stored procedures • class inheritence • caching • transactions ◦ commit ◦ rollback ◦ nested transactions • support sqlite • support MySQL • support PostgreSQL • support Oracle • support MS SQL Server • prevent SQL injection • remain performant • support multiple clients • use multiple databases • error handling • logging • create tables • insert data ◦ convert data types ◦ validate data • retrieve data ◦ support joins ◦ order results ◦ subqueries ◦ nested queries ◦ lazy loading ◦ eager loading • update data ◦ by ID ◦ using filters • delete data ◦ by ID ◦ using filters • cascading operations • bulk operations
  2. Responsibilities of our ORM • create database • define tables

    • define fields ◦ size/length ◦ nullable ◦ default values • define foreign keys • define indices • define relationships ◦ one-to-many ◦ many-to-many ◦ one-to-one ◦ inverse relationships • define stored procedures • class inheritence • caching • transactions ◦ commit ◦ rollback ◦ nested transactions • support sqlite • support MySQL • support PostgreSQL • support Oracle • support MS SQL Server • prevent SQL injection • remain performant • support multiple clients • use multiple databases • error handling • logging • create tables • insert data ◦ convert data types ◦ validate data • retrieve data ◦ support joins ◦ order results ◦ subqueries ◦ nested queries ◦ lazy loading ◦ eager loading • update data ◦ by ID ◦ using filters • delete data ◦ by ID ◦ using filters • cascading operations • bulk operations
  3. Responsibilities of our ORM • create database • define tables

    • define fields ◦ size/length ◦ nullable ◦ default values • define foreign keys • define indices • define relationships ◦ one-to-many ◦ many-to-many ◦ one-to-one ◦ inverse relationships • define stored procedures • class inheritence • caching • transactions ◦ commit ◦ rollback ◦ nested transactions • support sqlite • support MySQL • support PostgreSQL • support Oracle • support MS SQL Server • prevent SQL injection • remain performant • support multiple clients • use multiple databases • error handling • logging • create tables • insert data ◦ convert data types ◦ validate data • retrieve data ◦ support joins ◦ order results ◦ subqueries ◦ nested queries ◦ lazy loading ◦ eager loading • update data ◦ by ID ◦ using filters • delete data ◦ by ID ◦ using filters • cascading operations • bulk operations • create database • define tables • define fields
  4. Responsibilities of our ORM • create database • define tables

    • define fields ◦ size/length ◦ nullable ◦ default values • define foreign keys • define indices • define relationships ◦ one-to-many ◦ many-to-many ◦ one-to-one ◦ inverse relationships • define stored procedures • class inheritence • caching • transactions ◦ commit ◦ rollback ◦ nested transactions • support sqlite • support MySQL • support PostgreSQL • support Oracle • support MS SQL Server • prevent SQL injection • remain performant • support multiple clients • use multiple databases • error handling • logging • create tables • insert data ◦ convert data types ◦ validate data • retrieve data ◦ support joins ◦ order results ◦ subqueries ◦ nested queries ◦ lazy loading ◦ eager loading • update data ◦ by ID ◦ using filters • delete data ◦ by ID ◦ using filters • cascading operations • bulk operations • create database • define tables • define fields • define foreign keys
  5. Responsibilities of our ORM • create database • define tables

    • define fields ◦ size/length ◦ nullable ◦ default values • define foreign keys • define indices • define relationships ◦ one-to-many ◦ many-to-many ◦ one-to-one ◦ inverse relationships • define stored procedures • class inheritence • caching • transactions ◦ commit ◦ rollback ◦ nested transactions • support sqlite • support MySQL • support PostgreSQL • support Oracle • support MS SQL Server • prevent SQL injection • remain performant • support multiple clients • use multiple databases • error handling • logging • create tables • insert data ◦ convert data types ◦ validate data • retrieve data ◦ support joins ◦ order results ◦ subqueries ◦ nested queries ◦ lazy loading ◦ eager loading • update data ◦ by ID ◦ using filters • delete data ◦ by ID ◦ using filters • cascading operations • bulk operations • create database • define tables • define fields • define foreign keys • create tables • insert data • retrieve data
  6. Responsibilities of our ORM • create database • define tables

    • define fields ◦ size/length ◦ nullable ◦ default values • define foreign keys • define indices • define relationships ◦ one-to-many ◦ many-to-many ◦ one-to-one ◦ inverse relationships • define stored procedures • class inheritence • caching • transactions ◦ commit ◦ rollback ◦ nested transactions • support sqlite • support MySQL • support PostgreSQL • support Oracle • support MS SQL Server • prevent SQL injection • remain performant • support multiple clients • use multiple databases • error handling • logging • create tables • insert data ◦ convert data types ◦ validate data • retrieve data ◦ support joins ◦ order results ◦ subqueries ◦ nested queries ◦ lazy loading ◦ eager loading • update data ◦ by ID ◦ using filters • delete data ◦ by ID ◦ using filters • cascading operations • bulk operations • create database • define tables • define fields • define foreign keys • create tables • insert data • retrieve data • support sqlite