DEV Community

il3ven
il3ven

Posted on • Edited on

2 2

Brief Introduction to ORM

ORM (Object-Relational Model) in 50 words

Relational databases like MySQL are not object oriented. An ORM solves this problem. It provides an abstraction layer over relational databases.

In other words, ORM is an library using which you can create objects for your entities. For example, the entities for an ecommerce app can be Users, Orders and Products. These entities will get converted into tables by an ORM. An ORM will generate SQL queries for you and communicate with your database.

ORM

Example

Creating tables with one-to-many relationships in ORM is as easy as creating a class. Here one teacher teaches many courses.

class Teachers {
    id: Primary_Key,
    name: String,
    age: Number,
    courses: Courses[], // one-to-many relationship is as simple as creating an array
}
Enter fullscreen mode Exit fullscreen mode

Inserting entries into the table is as easy as using the new operator.

teacher = new Teacher()

teacher.name = 'Alice'
teacher.age = 32

teacher.save()
Enter fullscreen mode Exit fullscreen mode

Pros of an ORM

  • Modelling data is easier because instead of tables you are working with objects.
  • Time is saved as fewer queries have to written.
  • Cleaner code is produced when using an ORM.
  • Switching databases is easier as it is abstracted.

Cons of an ORM

  • Different ORMs have slightly different syntax and need to be learned.
  • Some queries cannot be performed with an ORM and one needs to use SQL for them.

Popular ORMs

ORM is a library available in many different programming languages.

JavaScript
Python
Java

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay