Contact Us

Contact Us



Pleas confirm by checkbox


Technical

ReferencesMany relation with Custom ID in Loopback Framework

Author_img
By dhruv004 January 12, 2018

In this blog, I share my views on using ReferencesMany relation with Custom ID in Loopback framework. This blog is relevant to developers with a basic knowledge of loopback and want to know how to use RefrencesMany relation in loopback.

Before we begin, I would like to share that rewriting Express API using Loopback can result in minimalistic code. It can be used to create dynamic end-to-end Rest APIs. However, the learning curve is steep. You can learn more about loopback from its extensive documents, but this is out of our current scope.

RefrencesMany is one of the relations that lets you connect models using embedded relations. It embeds an array of foreign keys to reference other objects.

Sample referencesMany model:

Pretext –

 

Models are at the heart of LoopBack and represent back-end data sources such as databases. LoopBack models are JavaScript objects with both Node and REST APIs.

 

In our example, let us consider two models Account and Device.

Account:

Key Type Description
_id String Mongo ObjectId
name String Name of Accounts
deviceIds Array Array of device Ids

 

Device:

Key Type Description
myId String Self-created identifier
deviceUID String UID of the device

We will store a reference of deviceIds in the deviceIds key of account model.

To connect these models we will define a relation between the models. With connected models, LoopBack exposes a set of APIs to interact with each of the model instances and query and filter the information based on the client’s needs.

Generally the relations are created using loopback-cli, but it does not support creating a referencesMany relation.

So we need to edit our account.json file and update the relation key:

The forceId  forces generation of ID for embedded items and default to false.

This creates the following API in explorer:

With these APIs, we can query both accounts and their related devices.

Now, post the account relation using /accounts API.

The saved object looks something like the following:

And like this for device:

This creates a problem when querying devices for an account.  You can make the request for

And we get the following error response

To solve this, we must change the device structure to:

Key Type Description
_id String ObjectId
myId String Self-created identifier
deviceUID String UID of the device

Now when I add the devices, my sample account model will look like

We can search using deviceIds. Notice  that the deviceIds have the reference stored as Mongo ObjectIds. We will now be able to query using loopback find API.

Deleting Related Ids from RefrencesMany Relation

Once a device is deleted, we want its reference to be removed from all the accounts that it belonged to. To do this we will use after delete operation hook:

With the after delete operation,  I can find all the accounts with the device id in deviceIds array. Use each series to run series operation on all the accounts.

 

 

Related posts
3 Crucial Ways of Inserting Code into a Running Application without Creating Bottlenecks
Technical

3 Crucial Ways of Inserting Code into a Running Application without Creating Bottlenecks

By shekhar.wagh January 28, 2022
Apache Spark Standalone Setup On Linux/macOS
Technical

Apache Spark Standalone Setup On Linux/macOS

By shekhar.wagh October 20, 2021
Apache Flink Standalone Setup on Linux/macOS
Technical

Apache Flink Standalone Setup on Linux/macOS

By shekhar.wagh October 13, 2021
Identity, Authentication, And Access Management Using Azure Active Directory  
Technical

Identity, Authentication, And Access Management Using Azure Active Directory  

By shekhar.wagh September 22, 2021
Things to Know Before You Select A Crypto Wallet
Blockchain

Things to Know Before You Select A Crypto Wallet

By shekhar.wagh September 20, 2021
Solve 3 Most Irritating Outlook Email Rendering Issues.
Technical

Solve 3 Most Irritating Outlook Email Rendering Issues.

By shekhar.wagh September 15, 2021
Intuit Wasabi – A Scalable A/B Testing Solution
Technical

Intuit Wasabi – A Scalable A/B Testing Solution

By shekhar.wagh September 01, 2021
How To Pick The Right Data Analytics Strategy For Serverless Systems?
Big Data

How To Pick The Right Data Analytics Strategy For Serverless Systems?

By shekhar.wagh August 25, 2021
Change Notifications and Named Options using Options pattern in .NET Core
Technical

Change Notifications and Named Options using Options pattern in .NET Core

By shekhar.wagh August 18, 2021
Create Strongly Typed Configurations in .NET Core
Technical

Create Strongly Typed Configurations in .NET Core

By shekhar.wagh August 13, 2021

Stay updated

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