ReasonJun

ORM 본문

Backend

ORM

ReasonJun 2023. 6. 23. 01:43
728x90

ORM stands for Object-Relational Mapping. It is a programming technique and a software pattern that allows developers to work with relational databases using an object-oriented approach. ORM frameworks provide a level of abstraction between the database and the application, allowing developers to interact with the database using objects instead of writing raw SQL queries.

 

The main idea behind ORM is to map the relational data stored in database tables to objects in the programming language, such as classes and their attributes. This mapping is typically defined using metadata or annotations that describe how the objects correspond to the database tables, columns, and relationships.

 

ORM frameworks provide various features and functionalities that simplify database operations and make it easier to work with the data. Some common features include:

  1. Object-based query language: ORM frameworks often provide their own query language, which is usually more object-oriented than raw SQL. Developers can write queries using the programming language's syntax and constructs, making it easier to express complex queries and manipulate data.
  2. Automatic schema generation: ORM frameworks can generate the database schema based on the object definitions. This eliminates the need to write SQL scripts for creating tables, columns, and relationships manually.
  3. Data caching and lazy loading: ORM frameworks often include caching mechanisms to improve performance by reducing the number of database queries. Lazy loading allows loading related objects on-demand, which can optimize the use of database resources.
  4. Database independence: ORM frameworks abstract the underlying database system, allowing developers to switch between different database vendors without rewriting the entire application code. They provide a consistent API that works across multiple database systems.
  5. Transaction management: ORM frameworks usually provide transaction management capabilities, allowing developers to group database operations into transactions. This ensures data integrity and consistency by providing ACID (Atomicity, Consistency, Isolation, Durability) properties.

Popular ORM frameworks in different programming languages include Hibernate (Java), Entity Framework (.NET), Django ORM (Python), ActiveRecord (Ruby), and Sequelize (JavaScript/Node.js).

 

ORMs are widely used in software development to simplify database interactions, reduce the amount of boilerplate code, and increase productivity. However, it's important to note that ORM performance may not always match the performance of hand-optimized SQL queries, especially in complex scenarios. Developers should consider the specific requirements of their application and carefully evaluate the trade-offs between convenience and performance when choosing to use an ORM.

 

 

here are some examples of using ORM and cases not using JavaScript code:

 

ORM

  • Saving a new user:
const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await userRepository.save(user);
  • Finding all users:
const allUsers = await userRepository.find();
  • Finding a user by ID:
const firstUser = await userRepository.findOneBy({ id: 1 });

Not using ORM

  • Saving a new user:
const sql = `INSERT INTO users (firstName, lastName, age) VALUES (?, ?, ?)`;
const values = ['Timber', 'Saw', 25];
const result = await connection.query(sql, values);
  • Finding all users:
const sql = `SELECT * FROM users`;
const results = await connection.query(sql);
  • Finding a user by ID:
const sql = `SELECT * FROM users WHERE id = ?`;
const values = [1];
const result = await connection.query(sql, values);

As you can see, using ORM makes the code more concise and easier to read. It also helps to abstract away the details of the database, making it easier to change databases in the future.

Here are some additional benefits of using ORM:

  • Increased productivity: ORM can help to increase productivity by reducing the amount of code that needs to be written.
  • Improved code quality: ORM can help to improve the quality of code by reducing the risk of errors.
  • Enhanced maintainability: ORM can help to enhance the maintainability of code by making it easier to change and update.

If you are working on a project that requires you to interact with a database, I recommend using an ORM. It can save you time, improve the quality of your code, and make your project easier to maintain.

728x90
Comments