ReferencesMany relation with Custom ID in Loopback Framework

Contributor - 12 January 2018 - 12 Mins
Contributor - 12 January 2018 - 12 Mins
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.
"How will you add accounts from explorer like above you have given example : { "name": "Home", "deviceIds": [ ObjectId("deviceId1"), ObjectId("deviceId2") ] I tried to insert from explorer. it is throwing error.".