Contact Us

Contact Us



Pleas confirm by checkbox


Uncategorized

Importance of relationship

Author_img
By Mayank Agrawal December 20, 2017

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.

[gist]7765a8e3449d1dd6d82d4c53267fba7c[/gist]

[gist]a1d431389fa81965f6d36441170da7f1[/gist]

Next let’s add relationship between class and student in class entity

[gist]e8c789af2047578c4e0edf2807ea0bbb[/gist]

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.

[gist]6a0b9542b96f7fb3eeac8e44460b4cc7[/gist]

Framework also provide a short-hand to create the relationship as follows.

[gist]0ea59f37a5c431afef387cd5dc88d92f[/gist]

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

[gist]881e36325eacb5db69833b3f713a6305[/gist]

Following will add another member ‘Clark’ into the  flat number 001

[gist]5305e36950913f57b33e2ab00b8b9f50[/gist]

Following will remove ‘Clark’ from ‘First’ class

[gist]e7b9df025c506305153ccb8802b392d1[/gist]

Following will update the ‘Steve’ name to ‘Steve Smith’.

[gist]2ce2b5830eddba84c96657db8fd98b1e[/gist]

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

  1. mongodb is installed and server is running on default port.
  2. Node 6.9.0 is installed.
  3. IDE like vscode is available to you.
  4. Rest client like postman,curl for testing.

Installation

  1. git clone https://github.com/ratneshsinghparihar/nodedata-demo-sample.git
  2. cd nodedata-demo-sample/Demo-Sample
  3. npm install

Code changes

  • Add a model blogmodel.ts inside models folder.
  • Add a repository blogmodelrepository.ts inside repositories folder.

[gist]6beea0b1a959769ff529d53203a9bc6d[/gist]

    • Model with @document tell the system to create a document inside blogs collection.
    • repository with blog name will create all necessary rest end points.

Testing

  1. npm start
  2. Post a JSON {“name”: “testBlog”} to http://localhost:9999/data/blogs
  3. Hit the api to get data(http://localhost:9999/data/blogs)
  4. Hit the api with put method http://localhost:9999/data/blogs/{{blogId}} with body {“name”: “testBlog1”}
  5. Hit the api with delete method http://localhost:9999/data/blogs/{{blogId}}

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/

Related posts
VPC Sharing Using AWS RAM (Resource Access Manager)
Uncategorized

VPC Sharing Using AWS RAM (Resource Access Manager)

By mynkgrwl22 June 18, 2019
Reduce App size with On Demand Resources
Uncategorized

Reduce App size with On Demand Resources

By mynkgrwl22 May 29, 2019
iMessage Stickers and Apps
Uncategorized

iMessage Stickers and Apps

By mynkgrwl22 May 27, 2019
What is UX Writing?
Uncategorized

What is UX Writing?

By mynkgrwl22 April 08, 2019
AWS ECS (Amazon Elastic Container Service )
Uncategorized

AWS ECS (Amazon Elastic Container Service )

By mynkgrwl22 March 26, 2019
Scala code analysis and coverage report on Sonarqube using SBT
Uncategorized

Scala code analysis and coverage report on Sonarqube using SBT

By mynkgrwl22 March 19, 2019
Introduction to Akka Streams
Uncategorized

Introduction to Akka Streams

By mynkgrwl22 March 08, 2019
Decentralized Applications - Utilizing the Power of Blockchain Technology
Uncategorized

Decentralized Applications - Utilizing the Power of Blockchain Technology

By mynkgrwl22 March 05, 2019
App Store Connect API To Automate TestFlight Workflow
Uncategorized

App Store Connect API To Automate TestFlight Workflow

By mynkgrwl22 February 28, 2019
Using Custom Metrics for CloudWatch Monitoring
Uncategorized

Using Custom Metrics for CloudWatch Monitoring

By mynkgrwl22 February 06, 2019

Stay updated

Get the latest creative news from Fubiz about art, design and pop-culture.