ORM (Object Relational Mapping): automate data recording

ORM (database and source code logo)

Have you ever thought or needed to pass data from your source code objects into a table of contents or database? It would be very practical to have a tool to be able to do that automatically and not do it manually, right? But for that to be possible, those values ​​should be in the proper format. Well if you need to do this, you must know ORM (Object Relational Mapping).

With ORM the data of your objects will be passed to a correct format to be able to save all that information in a database by mapping them. That creates a virtual database where the values ​​found in the application that you have created in your code, and thus are linked to this database to give them persistence and register them in this simple way. This gives the recorded information persistence so that it can be stored, analyzed, recorded or used later.

By exampleImagine that you have a Python program in charge of reading values ​​from a DHT11 sensor that records temperature and humidity. But you don't want to record these values ​​from an environment. In a simple way you could create a source code to program what you need and read the values ​​taken by the sensor and display them on the screen. But what if you want to store the values ​​to analyze when peaks of these values ​​have occurred or for any other utility?

In that case, you should have the ability for that data to be saved at the same time in a database and this is where ORM comes in handy. In addition to your DIY project you can store what you need and treat the values manually or through other software thanks to the fact that you have captured them in a database ...

What is ORM?

El object-relational mapping or ORM for its acronym in English, it is a programming technique that serves to transform data used in the object-oriented programming language or program and the relational database (SQL type) as a persistence engine. That will make the program values ​​create a virtual object-oriented database to hold the data you need.

If you have ever programmed an application connected to a database, you will have realized that it is quite difficult to transform the information to adapt it to the database or vice versa. Namely, mapping is a tedious thing that you can automate using ORM, in addition to making it independent of the database you want to use and you can even change the database engine without problem.

Un practical example is an F1 car, where it has a series of sensors that measure values ​​of pressure, temperature, consumption, RPM, speed, acceleration, gear changes, steering movements, oil, etc. All these values ​​are seen in real time by engineers on their computers thanks to telemetry. But when the session ends, the engineers need to study and analyze that data to understand how to improve the setup, evolve the car or what caused the failure. For this to be possible, they need to be exported to a database.

ORM advantages and disadvantages

As I have already commented, with ORM allows you to abstract from the database and greatly simplify the necessary source code. The mapping will be automatic and that means taking a lot of trouble off your shoulders when programming. In addition to the ease and speed of use, it provides security of the data access layer against attacks.

But not all are good things ORM also has its downsides. In heavily loaded environments it can reduce performance as you are adding an extra layer to the system. It also involves learning ORM so that you can use it, which can take time to properly understand and take advantage of.

ORM for programming languages

Depending on the programming language you use you can use an ORM. You cannot use just any ORM, you must use the correct one. For example:

  • Java: Hibernate, MyBatis, iBatis, Ebean, etc.
  • . NET: Entity Framework, nHibernate, MyBatis.Net, etc.
  • PHP: Doctrine, Propel, Rocks, Torpor, etc.
  • Python: Peewee, SQLAlchemy, PonyORM, Elixir, etc.

Example with Python and ORM

Peewee is a simple and shortened ORM for use with Python. You can get more information from your official website. Also, you should know that Peewee supports different DBMS, that is, several database management systems, such as SQLite, MySQL and Postgresql. You just have to change the initial bbdd declaration and that's it.

Eg your quick guide or quickstart From the site you can see simple code examples with Peewee like this:

from peewee import *

db = SqliteDatabase('people.db')

class Person(Model):
    name = CharField()
    birthday = DateField()

    class Meta:
        database = db # This model uses the "people.db" database.

In case that seems little to you, you have at your disposal the pwiz tool, a program that obtains Peewee models from databases. For example:

<br data-mce-bogus="1">

python -m pwiz -e postgresql basedatos &gt; modelo.py<br data-mce-bogus="1">


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.