Working with one to one and one to many relationships in Hypi’s low-code platform

How do you work with One-to-One or One-to-Many relationships in Hypi?

The example in this tutorial will answer this question.

Hypi’s low code platform supports the following two types of relationships between two entities (tables).

  • One-to-One relationship
  • One-to-Many relationship

We commonly see One-to-Many relationships examples in data-driven applications.

In one-to-many relationships, a single record in a table(entity) gets referenced by many records in another table (entities).

Then, what is a One-to-One relationship?

A single record in one table (entity) gets referenced by exactly one record in another table (entity).

Let’s deep dive into using one-to-one and one-to-many relationships in Hypi’s low code platform!

In this example, we will use the below schema.

type Profile {
     userAccnt: Account @computed
                (query: "hypi.id = '${self.hypi.createdBy}'")
     address: Address
     friends: [Account!]
 }

This is a profile of a user of a social media platform. Now, we will check the usage of one-to-one and one-to-many relationships using the above schema.

*Please note Address and Account are the in-built core tables provided by Hypi.

How do you implement one-to-one relationships in Hypi?

Implementing one-to-one relationships is easy with Hypi!

Let’s say you want to connect Table A to Table B through one common field C. So, table A has this field C of type B. This is the one-to-one relationship.

In the schema defined above, the field address will be used to generate a one-to-one relationship between table Profile and table Address.

For creating a relationship, Hypi has the built-in function link that creates the reference between two objects. The reference can be removed using the unlink function. Please check the parameters of link/unlink functions here.

Let’s create objects and add data in the table Profile and table Address as follows. Check out this tutorial on CRUD Operations to know how to add data in the table.

# Address Object
mutation {
   upsert(
     values: {
       Address: { 
         hypi: { 
           id: "UserAddr" 
         }, 
         city: "Nagpur", 
         country: {
           name: "India",
           stateName: "Maharashtra"
         } 
       }
     }
   ) {
     id
   }
 }

# Profile Object

mutation {
   upsert(values: { 
     Profile: { 
       hypi: { 
         id: "UserAccount" 
       } 
     } 
   }
   ) {
     id
   }
 }

The Address object and Profile object gets created successfully.

# Address Object
{
   "data": {
     "upsert": [
       {
         "id": "UserAddr"
       }
     ]
   }
 }

# Profile Object

{
   "data": {
     "upsert": [
       {
         "id": "UserAccount" 
       }
     ]
   }
 }

If you want to enter the Address of UserAccount, you may link UserAccount with UserAddr via field address.

mutation {
     link(from:Profile,to:Address,
     via:"address",whereFromID:"UserAccount",andToID:"UserAddr")
 }
 # Result
 {
   "data": {
     "link": true
   }
 }

Linking the tables results in the insertion of data from UserAddr into the field address of UserAccount. Now, retrieve data of UserAccount using find function and verify. We will get below result.

{
   "data": {
     "find": {
       "edges": [
         {
           "node": {
             "hypi": {
               "id": "UserAccount"
             },
             # UserAddr Added
             "address": {
               "county": "India",
               "city": "Nagpur"
             },
             "friends": null
           },
           "cursor": "UserAccount"
         }
       ]
     }
   }
 }

If the user wants to remove his address from his profile, he may do so. Simply unlink UserAddr from UserAccount.

mutation {
     unlink(from:Profile,to:Address,
     via:"address",whereFromID:"UserAccount",andToID:"UserAddr")
 }

#Result
{
   "data": {
     "unlink": true
   }
}

This results in the setting of the value of the address to null. UserAddr data remains as it is. But the object simply gets removed from the field address of object UserAccount.

This way you can link/unlink two objects by creating a one-to-one relationship.

Please note that you cannot delete the object simply if there are existing references/links between two objects. You need to set the clearArrayReferences parameter in the delete function to true to delete an object.

How do you implement one-to-many relationships in Hypi?

Now you want to connect Table A to Table B through one common field C. If field C is an array or list of type B, it is a one-to-many kind of relationship.

In the schema defined above, the field friends will be used to generate one-to-many relationships between table Profile and table Account. A user profile may have many friends’ accounts.

The link function works on an array as well. You may link to the friends field from the UserAccount object to other User Accounts objects. We have created Account objects with hypi.id as One-to-Many and Many-to-One for demonstration purpose using createAccount function. Let’s link these objects with UserAccount.

mutation {
     link(from:Profile,to:Account,
     via:"friends",whereFromID:"UserAccount",
     andToID:"One-to-Many")
 }

mutation {
     link(from:Profile,to:Account,
     via:"friends",whereFromID:"UserAccount",
     andToID:"Many-to-One")
 }

#Result
{
   "data": {
     "link": true
   }
}

So, entries of One-to-Many and Many-to-One get added into the friends array of Profile object UserAccount. This creates one-to-many references between Profile and Account objects. Let’s retrieve Profile Object UserAccount.

{
   "data": {
     "find": {
       "edges": [
         {
           "node": {
             "hypi": {
               "id": "UserAccount"
             },
             "userAccnt": null,
             "address": {
               "county": "India",
               "city": "Nagpur"
             },
             "friends": [
               {
                 "username": "Many-to-One",
                 "emails": [
                   {
                     "value": "manytoone@hypi.io"
                   }
                 ]
               },
               {
                 "username": "One-to-Many",
                 "emails": [
                   {
                     "value": "onetomany@hypi.io"
                   }
                 ]
               }
             ]
           },
           "cursor": "UserAccount"
         }
       ]
     }
   }
 }

If the user wants to unfriend a person, he may do so by unlinking the Account of that person.

mutation {
     unlink(from:Profile,to:Account,
     via:"friends",whereFromID:"UserAccount",
     andToID:"One-to-Many")
 }

Unlinking the reference would simply remove the entry One-to-Many from the friends list. The One-to-Many object would not get deleted from the platform. But its entry would be removed from the array.

The link and unlink functions do not work with Scalar arrays. Please check out this guide to using Scalars on Hypi’s low code platform.

Concluding Note

This way you can build one-to-one or one-to-many relationships in Hypi’s low code platform. You may have guessed that creating and removing one-to-one or one-to-many relationships is easy with Hypi.

Draw E-R diagrams, work with one-to-one or one-to-many relationships between different entities and build robust data models!

In the next tutorial, we will see how to monitor the database in real-time using the Subscription function.

1 Like