dinsdag 12 april 2016

Non-relational database using relations


Until now I knew about the existence of databases, that they hold information in a structured way and that they are used in nearly any mobile app or more sophisticated web application. Also I heard about the difference in databases. You have the old SQL version which holds tables and the NoSQL database and many varieties in between. But how you create, make, maintain one and use it in an app is a totally different story. The title holds a contradictio interminus. In this article I will describe how to use relations between data in a non-relational database works.

The app I’m working on should make use of the following information. Companies which will hold an address, telephone number and the people who work for the company. Furthermore there is an ability to add an event on a public event calendar. An event can be added by somebody who works for one of the companies which are also part of the database. In the event calendar the persons who want to attend an event can indicate in the app by tapping the attend button.

For simplicity sake I leave the amount of data as it is. What we can learn from above is that in the database there is need for three nodes. A company node, a person node and an event node. Each of these nodes have children. In every child specific information is stored. For instance: in a child of the company node the address is stored and in another child their telephone number. What about their coworkers? Aha, here we come across a new concept. Normally, you would think that a coworker is a member of the company and therefore one of the children in the company node. Wrong, that is where the Person node comes in handy. As there is a one to many relationship between a company and a coworker we can make a reference in the Person node with a child which refers to the company this person is working for. In this example we assume that a person can only work in one company for simplicity sake.

An the event functionality is another relationship as the one between companies and people. People can make multiple events and events are attended by multiple people (hopefully). This is what we call a many to many relationship. How do you make it possible in the database has this kind of relationship? The simplest way is to make a new node. In this case there should be a node that holds a relationship between the events and the people who attend it or a person who has made multiple events. A new node should be called something like PersonEvent to indicate the relationship. In the node a reference should be made to the event on the one hand and the person on the other hand.

The database we are using for our project sends a JSON message. The database does not present filtered info after the app has queried it. This means that the app should filter out what has to be displayed. In the app code should help to do this filtering.

Below tree represents the data. The colored boxes indicate the relationship of the data.