Loopback Connector
Loopback provides connectors for many popular databases.
Here is the list of some of the databases:
DB2
Oracle
PostgreSQL
Cloudant
MongoDB
Loopback database connectors implement Create, Retrieve, Update and Delete operations. (CRUD operations)
Loopback request node
This node provides the synchronous call feature to connect to Database using Loopback connector.
Loopback connector helps perform CRUD data operations from database.
We can install required Loopback connectors using npm tool.
Loopback request node operations are non-transactional. Database updates are not rolled back if the message flow fails.
Loopback request node properties
Data source name: It is defined in the datasources.json file.
Loopback object: It can be Database name or the Database table. In cloudant, we mention db name while in postgres, we name db table name.
Operation: It can be any of the CRUD operations.
Security identity: Provides authentication to connect to database.
Database connector configuration and how to use it in a message flow
- Install the required database connector (using npm command)
- Configure the datasource for Database connector.
- Create a Model for the database table (if required).
- Configure security credentials to connect to the database (if required).
- Create a message flow using loopback request node.
Implementation using PostgreSQL Database
Please follow these steps:
- Install the PostgreSQL
- Create a database and table to insert data.
- Install the PostgreSQL Connector.
- Configure the datasources.json file using the details from PostgreSQL database.
- Create a Model for the database table.
- Create a message flow using Loopback Request node.
We shall skip step 1 and 2 and move to install the PostgreSQL Database connector.
Go to node_modules path in the work path directory. In windows, the default path is:
C:\ProgramData\IBM\MQSI\node_modules
We need to install the connector using IBM console. Run the following command:
npm install loopback-connector-postgresql — save
After installing, got this path
C:\ProgramData\IBM\MQSI
And open the package.json file.
And voilà. There’s our postgresql connector.
Configure Datasource
Configure the datasources.json file using the details from PostgreSQL database.
Below is json template for postgreSQL database.
Datasource name →We can give any name.
Name → Same name, we will give here.
Hostname, Port, Username→ We can get this info in our postgresql database.
Protocol →Remain constant.
Database →You can select the database you want
Then give a password.
Connector → This also remains constant.
We are going to use this details in the datasources .json.
Let’s open the file and copy it. We already have a cloudant datasource.
Configure Models for Tables
Here is our table:
Here is the corresponding model:
Go to this path:
C:\ProgramData\IBM\MQSI\connectors\loopback
Create a folder and name it as per what you gave as your datasource name and paste it here.
Message Flow to create record in PostgreSQL Database Table — No Security
Here is a simple message flow:
The loopback object is the table name.
The operation is create as we are creating a record.
Now deploy the application and test it using postman.
Our record has been created.
Message Flow to create record in PostgreSQL Database Table — With Security ID
For scenarios where you need a security id… Open up console.
We need to create the security resource. Give your integration node name, name of resource, username and password.
After successful completion, confirm if the resource has been created.
Restart the integration node.
For our datasource…
Retrieve, Update and Delete operations
Let’s try the retrieve operation.
Deploy and head over to postman.
Add the column name ‘ID’ and enter the id you want to retrieve.