Object-Relational Mapping (ORM)
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.
With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.
In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.
I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.
What is ORM?
ORM, short for Object-Relational Mapping, is an additional piece of software or a service that acts as a bridge between your application code and the database. Its main responsibility is to help you retrieve or save data to the database with ease, without writing complex SQL queries.


Famous ORMs
TypeORM: Ideal for SQL databases like PostgreSQL, MySQL, and others. It also supports MongoDB but isn’t as optimized for it as Mongoose.
Mongoose: A great ORM specifically for MongoDB.
Other ORMs:
Prisma
MikroORM
Sequelize
Popular Databases

Databases can broadly be categorized into SQL and NoSQL. Here are some examples:
SQL Databases:
PostgreSQL
MySQL
MariaDB
CockroachDB
Oracle
NoSQL Databases:
- MongoDB
ORM as an Abstraction Layer

ORMs provide an abstraction over query languages like SQL. Instead of writing raw SQL queries, you can use simple methods in your code.
For example:
SQL Query:
SELECT * FROM users WHERE id = 1;TypeORM Query:
userRepository.findOne(id);
This makes your code easier to write, read, and maintain.


Advantages of ORMs
Simplified Query Writing: Allows you to write complex queries in a simple, language-specific way (e.g., JavaScript/TypeScript).
Database Relationships in Code: ORM makes it easy to manage relationships (e.g., one-to-many, many-to-many) in your entities or schemas.
Switching Databases: ORMs abstract queries, so switching databases (e.g., from PostgreSQL to MySQL) has minimal impact on your code.
Indexing Support: ORMs like TypeORM make it easy to define indexes in your entities. For example:
@Index() @Column() email: string;Indexes improve query performance.



Disadvantages of ORMs
Less Low-Level Control: While ORMs simplify query writing, they abstract away low-level details, which might be necessary for complex use cases.
Performance Optimization: In cases of highly complex queries, ORM-generated queries might not be as efficient as hand-written SQL queries.
Direct Query Support: While ORMs abstract queries, tools like TypeORM allow you to write direct database queries when needed.

For example:
await entityManager.query('SELECT * FROM users WHERE id = $1', [id]);
TypeORM and Indexing
One of the useful features of TypeORM is its support for adding indexes directly in the code. Indexes help optimize query performance by allowing the database to search specific columns faster.
Example:
import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Index()
@Column()
email: string;
}
Summary
ORM simplifies the interaction between your codebase and databases.
It reduces the need for complex SQL queries, helps in managing relationships, and supports indexing.
While ORMs like TypeORM are powerful, they also allow direct query writing for performance optimization when needed.
Choosing the right ORM depends on your database and project requirements.