Understanding Object-Relational Mapping: Pros, Cons, and Types
This is a guest article by tech writer Mia Liang
One of the challenges of using object-oriented programming (OOP) languages and databases is the complexity of aligning the programming code with database structures. Object-relational mapping (ORM) is a technique that creates a layer between the language and the database, helping programmers work with data without the OOP paradigm.
The challenge OOP developers have is the need to understand and code in the structured query language (SQL) in order to connect their application to an SQL database. Developers who know SQL can write data-access code. This raw SQL coding can be extremely time-consuming because it requires the developer to extract the code strings’ data elements. SQL query builders add a layer of abstraction to the SQL code to provide more information about the data. However, developers still need to understand and write SQL.
ORMs are popular and controversial at the same time. Advocates of ORMs claim they increase productivity, improve application design, reuse code and maintain the application over time. According to detractors, a negative aspect of ORMs is performance. This article will provide an overview of ORMs, compare them against SQL tools, and review the pros and cons of these tools so that you can decide if ORMs will help or hurt your database application development efforts.
What is an ORM?
An object-relational mapper provides an object-oriented layer between relational databases and object-oriented programming languages without having to write SQL queries. It standardizes interfaces reducing boilerplate and speeding development time.
Object-oriented programming includes many states and codes in a format that is complex to understand and interpret. ORMs translate this data and create a structured map to help developers understand the underlying database structure. The mapping explains how objects are related to different tables. ORMs use this information to convert data between tables and generate the SQL code for a relational database to insert, update, create and delete data in response to changes the application makes to the data object. Once written, the ORM mapping will manage the application’s data needs and you will not need to write any more low-level code.
How does ORM work?
ORMs create a model of the object-oriented program with a high-level of abstraction. In other words, it makes a level of logic without the underlying details of the code. Mapping describes the relationship between an object and the data without knowing how the data is structured. The model can then be used to connect the application with the SQL code needed to manage data activities. This “plumbing” type of code does not have to be rewritten, saving the developer a tremendous amount of time.
Types of ORMs
ORMs employ two different strategies: active record pattern and data-mapper pattern.
Active record pattern
This strategy maps data within the structure of objects in the code. You manage data using classes and structures within your programming code. This method has problems since the database structure is tightly connected to the code, making it difficult to remove the database and migrate it to a different application.
The data-mapper pattern attempts to decouple the business logic in the objects from the database. This separation can make it easier to change databases and use the same programming logic.
ORM vs. SQL
Developers can use raw SQL code to write a direct interface between the application and the database. Most relational databases support SQL to build data interfaces and applications. It’s stable, and since SQL has been used since the 1970s, it’s well documented and supported. Programmers maintain a lot of control over their data interface with SQL. It requires a lot of work, but it is more flexible and detailed than an ORM abstraction.
Native Querying with SQL
Using raw SQL also has its drawbacks. For instance, the developer is responsible for the safety and security of the database code. SQL injection is a problem where user input can affect the data state causing issues with the application and data integrity. ORMs sanitizes the code, making it easier to avoid these problems.
SQL Query Builders
Query builders add a layer of abstraction over the raw SQL without masking all of the underlying details. The builders formalize querying patterns and add methods to or functions that add escape items for easier application integration. They add a templating layer to help developers understand the database structure within the same coding application. Template builders still require developers to understand the database structure, requiring them to know SQL.
Pros of Object-Relational Mapping
ORM tools are popular with OOP developers because they minimize the amount of SQL knowledge required to connect a database to an application. ORMs also automatically generate the SQL code, allowing you to focus on generating business logic. There are four significant benefits of using object-related mappers to manage the interface between applications and databases.
Writing data-access code is time-consuming and doesn’t add a lot of value to the application’s functionality. It’s essentially the plumbing of the code. Using a tool like an ORM that automatically generates the data-access code saves tremendous development time that does not add value to the application. In some cases, the ORM can write 100 percent of the data-access code for the application. The ORM can also help you keep track of database changes making it easier to debug and change the application in the future.
A well-written ORM will implement design patterns to force you to use best practices for application design. If you use an ORM to manage the data interface, you do not need to create the perfect database schema in advance. You will be able to change the existing interface easily. Separating the database table from the programming code also allows you to switch out data for different applications.
One way to reuse data is to create a class library to generate a separate dynamic-link library (DLL). You can create a new application without needing to duplicate the data-access code.
Since the code generated by the ORM is well-tested, you do not need to spend as much time testing the data-access code. Instead, you can focus on testing the business logic and code.
Cons of Object-Relational Mapping
ORMs are an excellent tool for many applications, but some developers found several drawbacks in using ORMs for data-access applications. The issues seem to correlate with the complexity of the application. With simple applications, having a high-level of abstraction helps the development process. But when the applications are complex, abstraction covers up many details needed to address data-related issues.
A common complaint among OOP developers is the extra code generated by the ORM. The added code slows application performance and makes it harder to maintain. A well-designed ORM should be able to create high-quality code without affecting application speed.
Need to Know SQL
High-level abstractions do not always generate the best SQL code, and developers cannot rely on the ORM 100 percent of the time. You still need to know SQL as well as the syntax generated by the ORM.
ORMs can sometimes create an incorrect mapping between data tables and objects. These problems can cause application problems and be difficult to recognize. ORMs also encourage one-to-one mapping even though it is rare that business applications have many one-to-one relationships.
Effects on Schema and Migrations
A poorly-written ORM layer often makes it challenging to improve data schemas. It can often limit your choices and depending on the ORM, your options may be limited. If your ORM doesn’t support migrations, writing the migrations in your OOP is more complicated than writing the code for migrations in SQL. A well-written ORM will include tools to facilitate future database migrations.
Overviews of Popular ORMs
Prisma 2 is a next-generation open-source ORM for Node.js and TypeScript. Prisma 2 works with PostgreSQL, MySQL, and SQLLite databases. Modules include Prisma Schema, Prisma Client, Prisma Migrate and Prisma Studio.
Sequelize is a promise-based ORM first launched in 2011. It has an extensive documentation set and is a well-supported ORM for Node.js. It supports PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL.
Writing SQL code to attach a relational database to an object-oriented application can be a time-consuming activity that generates little value to the business application. Developers can write raw SQL code or use SQL query builders to improve the process, but both methods still require in-depth database knowledge and the ability to code in SQL. ORMs enhance productivity by creating highly-abstract data models and automatically generating SQL code. These tools also make it easier to separate the database from the programming logic giving developers more flexibility. But ORMs have their detractors. Common complaints include reduced performance, extra coding, and poor mapping depending on the ORM quality.
Developers need to do their homework before committing to using an ORM to speed up the OOP development process. Several open-source and commercial tools are available. Since these tools have been available for many years, they are well documented and supported with extensive development communities.
Mia Liang has always been interested in software, databases, and expanding her education of all things technical. She has taken numerous coding boot camps and development workshops to further her education in the tech space. Mia is a writer for Girls Write Tech, which specializes in technical writing content and aims to encourage more female developers to share their knowledge.
Want to write an article for our blog? Read our requirements and guidelines to become a contributor.