What is an ORM?

Concepts

What is an ORM?

👍

Tip

JSData is considered an ORM/ODM, like SQLAlchemy for Python, Eloquent for PHP, Hibernate for Java, or ActiveRecord for Ruby on Rails. But JSData doesn't just run on the server, it works in the browser too.

What is an ORM? From Wikipedia:

Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

and

The equivalent of ORMs for Document-oriented databases are called Object-Document Mappers (ODMs).

JSData converts data from JavaScript Objects to formats used by MySQL, MongoDB, RethinkDB, Firebase, etc. and vice versa.

In a relational database such as MySQL, data must be stored as scalar values such as integers and strings organized within tables. Data management tasks in object-oriented programming languages almost always act on non-scalar values, or more specifically, objects that contain references to other related objects. The object-oriented representation of data is not directly compatible with how data is stored in many databases, and so it becomes necessary to convert the data between the two type systems.

For example, user.json shows how data might be represented in a JavaScript object, and how the data is scattered across disparate tables in MySQL:

{
  "id": 12,
  "name": "John Anderson",
  "organization_id": 32,
  "organization": {
    "id": 32,
    "name": "Company Inc."
  },
  "phone_numbers": [
    {
      "id": 123,
      "user_id": 12,
      "value": "+11234567890"
    },
    {
      "user_id": 12,
      "value": "0987654321"
    }
  ],
  "profile": {
    "id": 63,
    "user_id": 12,
    "email": "[email protected]",
    "join_date": "2016-04-13T16:43:55.823Z"
  }
}
+---------------------------------------+
| id  | organization_id | name          |
+---------------------------------------+
| 12  | 32              | John Anderson |
| ..  | ...             | ...           |
+---------------------------------------+
+------------------------------+
| id  | user_id | value        |
+------------------------------+
| 123 | 12      | +11234567890 |
| 445 | 12      | 0987654321   |
| ... | ...     | ...          |
+------------------------------+
+-----------------------------------------+
| id | user_id | email                    |
+-----------------------------------------+
| 63 | 12      | [email protected] |
| .. | ...     | ...                      |
+-----------------------------------------+
+--------------------+
| id  | name         |
+--------------------+
| 32  | Company Inc. |
| ..  | ...          |
+--------------------+

In JavaScript, it is convenient for the user object to contain references to its relations. But, the nested object structure is not compatible with MySQL's table organization. A MySQL client driver won't natively know what to do with such an object. You would need to write code to translate from one to the other. If you have a lot of tables and relations, you would have to write a lot of translation code. You might abstract the boilerplate into reu​sable code, but what about your next project? Will you do it all again? This is where an ORM library is useful. This is why you use JSData.

👍

Tip

JSData abstracts away a lot of boilerplate CRUD code, saving you a lot of time and energy.

Switching databases? It'd be a shame to have to rewrite your data layer. Switching app frameworks? It'd be a shame to have to rewrite your data layer. If you use JSData as your data layer, you no longer have to worry. You can switch from Angular to Ember, from Ember to React, from Hapi to Express, from MySQL to RethinkDB, from MongoDB to Firebase, and so on. Use JSData and with each switch, you don't have to learn another data framework or rewrite your data layer—JSData works great with all of them.

👍

Tip

JSData works with many databases and app frameworks, giving you a unified data API you can use without learning new data libraries for each new project.