DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on β€’ Originally published at sreekarreddy.com

πŸ”— ORM Explained Like You're 5

Objects to database rows translator

Day 120 of 149

πŸ‘‰ Full deep-dive with code examples


The Translator Analogy

In a foreign country, a translator converts between languages.

ORM translates between your code and the database.

You work with objects. ORM converts them to SQL.


Without vs With ORM

# Raw SQL
cursor.execute(
    "INSERT INTO users (name) VALUES (?)",
    ("Alice",)
)

# ORM
user = User(name="Alice")
session.add(user)
Enter fullscreen mode Exit fullscreen mode

Much cleaner!


Common ORMs

Language ORM
Python SQLAlchemy
JavaScript Prisma
Java Hibernate
Ruby ActiveRecord

Benefits

  • Write code in your language
  • SQL injection protection
  • Handle relationships naturally
  • Switch databases easily

Watch Out For

N+1 Problem: 100 users = 101 queries if not careful. Use eager loading!


In One Sentence

ORM maps database tables to programming objects, letting you interact with data using your language instead of SQL.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)