ORM (Object Relational Mapping): automates data recording

ORM (database and source code logo)

Have you ever thought about or needed to pass the data of your source code objects in 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 right 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 bbdd mapping them. That creates a virtual database where the values that are in the application you have created in your code, and this way ‘string’ are linked to this database to give them persistence and register them in this simple way. This gives the registered information persistence to be able to store, analyze, register or use it later.

For example, imagine you have a Python program that reads 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 that will read the values taken by the sensor and will show them on the screen. But what if you want to store the values to analyze when there have been peaks of these values or for any other utility?

In that case, you should have the ability to have that data stored in a database at the same time 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 because you have captured them in a database .

What is ORM?

The object relational mapping or ORM is a programming technique that serves to transform data used in the program or object-oriented programming language and the relational database (SQL type) as a persistence engine. That will make the program values create a virtual object oriented database to contain the data you need.

If you have ever programmed an application connected to a database you will have realized that it is quite complicated to transform the information to adapt it to the database or vice versa. That is to say, the mapping is a rather tedious one that you can automate using ORM, besides making it independent of the database you want to use and you can even change the database engine without any problem.

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

Advantages and disadvantages of ORM

As I have already mentioned, with ORM you can abstract from the database and simplify the necessary source code a lot. The mapping will be automatic and that means taking a lot of problems off your hands when programming. Besides the ease and speed of use, it provides data access layer security against attacks.

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

ORM for programming languages

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

  • Java: Hibernar, 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 reduced ORM for use with Python. You can get more information from its official website. Also, you should know that Peewee supports different DBMS, that is, several database management systems, such as SQLite, MySQL and Postgresql. You only have to change the initial declaration of bbdd and that’s it.

For example, in its quickstart guide you can see simple examples of Peewee code 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.

As if that’s not enough, 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 > model.py<br data-mce-bogus="1">



Leave a Comment