Brief Introduction to ORM

il3ven

il3ven

Posted on May 29, 2021

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
đź’– đź’Ş đź™… đźš©
il3ven
il3ven

Posted on May 29, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related