Importance of relationship

Contributor - 20 December 2017 - 12 Mins
Contributor - 20 December 2017 - 12 Mins
Importance of relationship
A database cannot exists without a relationship. It connects tables which are logical related to each other. Even for NoSQL database we require to create some sort of relationship between collections.
For e.g. I have an application which manages society. A society will have flats and each flat will have registered members. I want to list all the members present in a flat. To achieve this, I will add an association between House entity and Person entity. This association can be of 2 types, reference and embedded.
In NoSQL, managing both type of association and enforcing this constraint is a pain in itself. We need a framework which should manage relationship-level integrity.
OneToMany relational attribute
Nodedata provides simple annotation based solution for this. To understand this, let go through below models first.
Next let’s add relationship between class and student in class entity
This is it. Now it is fully managed by framework.
Implementation and CRUD over relational attribute
As explained in the the blog (//url for nodedata first blog), Nodedata will automatically create the Rest API over these models. We will now insert data and add association between them.
Framework also provide a short-hand to create the relationship as follows.
Both of the above set of request will insert new flat which have one member named ‘Steve’. Now, to get house and its members, we can execute following API
Following will add another member ‘Clark’ into the flat number 001
Following will remove ‘Clark’ from ‘First’ class
Following will update the ‘Steve’ name to ‘Steve Smith’.
Above query will also update all the places wherever this instance is used.
Relationship strategy
NoSQL is a schema-less storage which provides flexibility in designing the database. Which means as per the actions present in the application we should design the relationship saving strategy. We can achieve this by configuring the annotation. We will go through case wise and see how it can be achieved.
Case 1: Action to fetch in only number of students present in the class.
Following annotation will save references of ‘Steve’ student in ‘First’ class
// code
Case 2: Action to get complete class information along with student information
Following annotation will embedded ‘Steve’ student in ‘First’ class
// code
Case 3: Action to get partial information of students present in the class
With growing number of students, document size will also grow, so this will require us to minimize the properties which we want to keep along with class object. Following annotation will embedded ‘Steve’ student’s name only in ‘First’ class object.
// code
Case 4: Action to get complete class information along with student information where each student is having large set of properties.
Since the student is having large set of properties, it will be difficult to embedded them in class object. Following annotation will save references of ‘Steve’ student in ‘First’ class and on fetching ‘First’ class it will also return the complete student object.
// code
Case 5: Action to delete all the students on deleting the class so that no orphan is present
Following annotation will embedded ‘Steve’ student in ‘First’ class. On deleting the ‘First’ class, all the students will also get deleted.
// code
There is 1 more very important aspect is database design change. Suppose initially you went with the action mentioned in Case 1 and after some time the requirement is changed to Case 3. Well this can be achieved by changing the annotation.
But what about existing data migration ? Well, let the framework do it for you. Just execute the put on all the class objects with the same data and it will update the class entity according to the new relation mentioned.
// code for executing the migration
Prerequisite–
Installation
Code changes
Testing
Conclusion
Now you can see how creating rest apis is super easy with node-data. If you want to know more check out the our github
https://github.com/ratneshsinghparihar/Node-Data
Or visit our main page
https://nodedataio.azurewebsites.net/
Leave a Reply