Message Image  

Configuring Loopback Request Node with RestAPI and MongoDB provisioned by Atlas

 View Only
Wed July 29, 2020 11:16 AM

MongoDB Atlas provides an easy way to host and manage your data in the cloud. The IBM App Connect Enterprise Loopback Request Node can be used to connect to MongoDB and perform insert, update, retrieve and delete documents in MongoDB.

Configuring the MongoDB cluster

    1. Create an account in https://cloud.mongodb.com and login with that particular user
    2. Create new cluster -> Click on “Build new cluster” and select the cloud provider and region you want.
    3. Click on “Create cluster” after selecting the cloud provider and region.
    4. Give the appropriate cluster name and create the cluster as shown below.

      Here the example cluster name is “mongohamsa”

    5. Click on “Connect” to connect to mongodb cluster
      You can use any of these 3 connections to get the URL
      If you are using Loopback request node in the message flow to access the MongoDB cluster, configure datasources.json in C:\ProgramData\IBM\MQSI\connectors\loopback on Windows as shown below.

      (Location of datasources.json on Linux : /var/mqsi/connectors/loopback)

      datasources.json file should contain the below field
      Example url : “mongohamsa-jhvam.mongodb.net”
    6. (Optional) You can also create the model.json file inside the newly created directory with the datasource name in the same location “C:\ProgramData\IBM\MQSI\connectors\loopback”
      Example datasource name in the above case : mongodb
      NOTE: Name of the model.json should be the collection name which you are trying to access.
      Sample movies.json file contents :

      {
      "name": "movies",
      "type": "object",
      "properties": {
      "title": {
      "type": "string"
      },
      "releasedate": {
      "type": "string"
      },
      "rating": {
      "type": "number"
      }
      }
      }
    7. Click on “COLLECTIONS”. Create the collections with the required name and inside the required database.

      Example in this case : collection name is Customerdb, Database name is Customer

      If the database does not exist, create the database with the required name.

    8. In the Database Access tab, Select “Add new database user”.

      Add the username, password, select the database user privileges and click on “Add User”.

      It creates an entry in the Database Access tab as shown below.

    9. Network Access tab
      Set the IP in the unblocked list by using “Add IP address” as shown below
      Enter the IP you want to add. Here we are using 0.0.0.0/0 since we are allowing all the IP to access. Then click on 'Confirm'.

      This creates entry in the Network Access tab as shown below.

Designing the loopback message flow using REST API

Loopback Request node

  • Allows flow developers to easily create, retrieve, update and delete data records in external systems such as MongoDB, Cloudant and PostgreSQL.
  • Uses the LoopBack framework to facilitate interaction with external systems through LoopBack connectors.
  • LoopBack is an open source Node.js framework that can be extended by downloading and installing connectors from a large open source catalog using the ‘npm’ tool.

Install the required node modules using the commands below:

Command to list the node modules installed : npm ls

cd C:\ProgramData\IBM\MQSI on Windows , /var/mqsi on Linux

Command to install loopback-connector-mongodb: npm install loopback-connector-mongodb

Create a REST API and define the required operation and message flow as provided below

File -> New -> RestApi

Give the name “TestMongoDB” as shown below

Go to the “Resources” section as shown below. Click on (+) Create resource

Add the resource path and operation

addCustomer is the operation added to add customer document in the collection as shown below

retrieveCustomerById is the operation added to retrieve the customer details by their id as shown below

updateCustomer is the operation added to update the customer details using their id as shown below

Add the subflow for each of the operations created

  1. addCustomer subflow

    Datasource name is “mongodb” given in datasources.json
    Loopback object is “Collection name”
    Operation is “Create”
    Security identity is mongodb defined using mqsisetdbparms using below command

    mqsisetdbparms <Integration node> -n loopback::mongodb -u lbadmin -p lbpwd1
    mqsireportdbparms <Integration node> -n loopback::mongodb -u lbadmin -p lbpwd

    Restart the integration node

    Click Next and then Finish

  2. retrieveCustomerById subflow – Operation is Retrieve

    ESQL
  3. updateCustomer subflow – Operation is update

    ESQL:

    Main Flow will be generated automatically

Accessing the url with the REST API

Make sure that the url is taken from RESTAPI BASE URL

We can also change the base url of RESTAPI. Make sure same is replicated in HTTP Input node request url

Example url is mentioned below.


NOTE : Make sure that VPN is not connected while posting the data using REST client.

Add the customer details to Mongodb database “Customer” and collection “Customerdb”


Retrieve the required data using GET operation and id

Example id : 5e732adfaa0e0938f04baeee

Example url : http://localhost:7800/testmongodb/Customer/1.0.0/customers/5e732adfaa0e0938f04baeee


Similarly for the update operation we need to PUT and change the required data

Updated in mongodb for that particular document.


NOTE:
To debug more if any errors occur :
Set DEBUG=* in mqsiprofile.cmd and start broker

C:\ProgramData\IBM\MQSI\components\

→ This file shows all the information about the loopback connection

Hamsa Nanjundaswamy is a software developer in the IBM India ISL Lab specialising in IIB/ACE


#AppConnectEnterprise(ACE)
#MongoDB
#Atlas
#Loopbackrequestnode
#rest_apis