From CS 340 to ORM


My capstone group has chosen to develop a full stack web application for our class project. Most students likely understand that full stack development involves development of both the front end (client side) and back end (server side) of a web application. The front end is typically built using HTML, CSS and a JavaScript framework or library such as Angular or React. Back end involves development of business logic and database interactivity which are typically exposed to the front end through an application programming interface (API).

As students nearing the end of the Postbacc CS program, we’ve all been exposed to full stack development in some of our classes. CS 290 introduced us to various full stack web technologies. CS 340 specifically taught us the design and implementation of relational databases and how to use SQL to create database tables as well as query data in those tables. This portion of a full stack web application is typically called the persistence layer. In many real-world full stack web apps, rather than manually creating database tables and reading and writing data with raw SQL queries as we did in CS 340, often times software tools are used to automate features of the persistence layer. One such tool is called an object relational mapper, or ORM, and we will discuss this tool and some of its capabilities in this post.

Many definitions for object relational mapping can be found online. One in particular that I like is:

Object-relational mapping (ORM) is a mechanism that makes it possible to address, access and manipulate objects without having to consider how those objects relate to their data sources.

https://www.theserverside.com/definition/object-relational-mapping-ORM

A code example can help illustrate what this means as a developer.

If we wanted to select records from a book table in our database, instead of doing this:

books = db.engine.execute('select title, isbn from books')
for book in books:
  print(f'title: {book[0]}, isbn: {book[1]}'

using an ORM, we would do something like this:

books = session.query(Books).all()
for book in books:
    print(f'title: {book.title}, isbn: {book.isbn}'

By creating classes such as these:

class Author {
    int id;
    String firstName;
    String lastName;
    List<Book> books;
}
class Book {
    int id;
    String title;
    String isbn;
    Author author;
}

an ORM would map these classes to corresponding author and book tables that are created for us in our database. The book table would include an authorId foreign key column, representing the one-to-many relationship between Author and Book objects.

As you can see, an ORM allows us to write code that interacts with objects rather than database tables, and any SQL queries are hidden in our logic. An ORM also provides a programming interface that is agnostic to any specific database platform, and its unique SQL dialect, that our application is using. Our code would be the same whether we are using MySQL, PostgreSQL, MongoDB or other database system.

These examples only scratch the surface of the features and capabilities available when using an ORM. An ORM can provide an interface for handling transactions, setting a data fetching strategy, enforcing relationships and handling cascading, defining keys and indexes, enable caching, perform batch processing, and more.

For the purpose of learning relational database and SQL fundamentals, we were not allowed to using object relational mapping in CS 340. Having learned those fundamentals, my class teammates and I are able to better understand and appreciate the benefits of using object relational mapping in our capstone project, and we have decided to take this approach while developing the persistence layer of our full stack application.

Print Friendly, PDF & Email

One response to “From CS 340 to ORM”

Leave a Reply

Your email address will not be published.