What is an ORM?

Welcome to this tutorial on Object Relational Mapping (ORM). Up to now, you’ve learned how to use SQL to manipulate data in a database. Now, we’re going to bridge the gap between traditional SQL and modern application development by exploring how an ORM lets you work with your data directly in your programming language. Think of an ORM as a translator between two languages—on one side, you have object-oriented JavaScript code, and on the other side, you have the relational language of SQL.

By the end of this lesson, you will be able to:

Object Relational Mapping Explained

Object Relational Mapping, or ORM, is a technique that allows you to work with data in a relational database (like MySQL, PostgreSQL, or SQLite) using an object-oriented approach. Instead of writing raw SQL queries, you use objects and methods that correspond directly to your database tables.

Imagine you have a table of books in your database. With an ORM, you would define a Book model in your code. This model acts as a blueprint for the table, letting you create, read, update, and delete book records using familiar JavaScript syntax.

How an ORM Works

Let’s consider a practical example. Suppose you are writing object-oriented JavaScript code. You can create a class called Book that represents a book with properties like title, series, and author. See the example below:

// Create a Book class constructor function
class Book {
  constructor(title, series, author) {
    this.title = title;
    this.series = series;
    this.author = author;
  }
}

// Create instances of Book
const fellowshipOfTheRing = new Book(
  'The Fellowship of the Ring',
  'The Lord of the Rings',
  'J.R.R. Tolkien'
);

const princeCaspian = new Book(
  'Prince Caspian',
  'The Chronicles of Narnia',
  'C.S. Lewis'
);
  

Now imagine that these two instances correspond to rows in a books table in your database:


id |           title              |             series              |      author
---------------------------------------------------------------------------
1  | The Fellowship of the Ring   | The Lord of the Rings           | J.R.R. Tolkien
2  | Prince Caspian               | The Chronicles of Narnia        | C.S. Lewis
  

The ORM acts as the bridge between your JavaScript objects and the SQL database. When you call a method like Book.create() or Book.findAll(), the ORM translates your JavaScript code into the corresponding SQL commands, executes them, and returns results as objects.

Why Use an ORM?

You might be wondering, "If I already know SQL, why do I need an ORM?" There are several benefits:

In a real-world project, using an ORM like Sequelize means that you can build complex applications more quickly, without getting bogged down in repetitive SQL. For example, in a full-stack application, an ORM lets your team work on different parts of the project (frontend and backend) in parallel, with a clear, consistent way to manage data.

Practical Usage and Real-World Examples

Let’s imagine you’re building an online library. With an ORM:

In addition, ORMs help maintain consistency when your database evolves over time. As your application grows, you’ll update your models and migrations, and the ORM keeps track of these changes in an organized manner. This ensures that your code and database stay in sync even as you add new features.

When to Use an ORM

Although ORMs simplify many aspects of database interaction, they aren’t always the perfect fit for every scenario. Consider using an ORM when:

However, if you need highly optimized, fine-tuned queries for complex reporting or analytics, you might sometimes write raw SQL or use a query builder in conjunction with an ORM.

Exploring Further

Once you have mastered the basics of an ORM, you might want to explore advanced topics such as:

Conclusion

In summary, Object Relational Mapping (ORM) transforms the way you work with databases by allowing you to write code in your preferred programming language—like JavaScript—rather than raw SQL. ORMs provide powerful tools for ensuring data integrity, managing relationships, and keeping your code clean and maintainable.

With this understanding, you’re now ready to dive deeper into using Sequelize—one of the most popular ORM libraries for Node.js. In upcoming lessons, we’ll explore how Sequelize lets you perform all CRUD operations on your data in an Express application, along with best practices for migrations, validations, and more.