ReferencesMany relation with Custom ID in Loopback Framework

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:

blog1

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:

blog2

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

This creates the following API in explorer:

blog3

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

blog4

Now, post the account relation using /accounts API.

blog5

The saved object looks something like the following:

blog6

And like this for device:

blog7

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

blog8

And we get the following error response

blog9

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

blog10

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:

blog11

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.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s