Getting Started with Loopback.io on IBMi

Loopback.io is an open-source framework for creating dynamic REST APIs on a backend servers, and now we can run it on IBMi using Node version 4.

What can Loopback.io do for IBMiers?
A first approach to Loopback.io can be a little bit overwhelming because of the vast amount of features and complexity, but the most important things that this framework can do for use are:

  • Map our database with a model and ORM Language (no more sql sentences in our code).
  • It is based on Express.js and it can generate automatic routes to access our data (It doesnt access directly to our database, all the work is done by "middleware").
  • Built-in role-based access controls. We can define what users can use our "read" data routes and what users can use "inserts" or "update".
  • Data-Juggler: This is the part of Loopback that allow us to connect to IBMi and many databases.
  • REST API autodocumentation with Swaggler
  • IBM maintain the framework!
There is also an enterprise version called "StrongLoop" with more features.

Getting Started with IBMi

All  my tests have been done in: V7R2, Latest PTFs and Node Version 4.

There is nothing differenty about setting Loopback in different platforms. 
 I will install globally, so we could use the strongloop commands to create all our projects.


 npm install -g strongloop

There are some packages that will not get installed in PASE:
heapdump
modern-syslog
sqlite3
strong-agent
utf-8-validate@
bufferutil@
strong-debugger

Before the community can solve how to compile those packages, you could try also an install ignoring the optional components:


npm install -g strongloop --no-optional


Next step is create a project using the loopback cli. You will be able to create an empty application, a hello world example or create the squeleton of your app.

Lets just start with a simple "Hello World"


slc loopback RESTAPI



Go to the directory of the project and start the app simple with:

> node .

Discovering your REST API.

Now you can navigate over your new REST API. Loopback support Swagger, which is basically an standard interface to REST APIs which allow humans and computer to understand the API.

You would be able to discover your API in this link:

http://servername:port/explorer



API Explorer will discover inmediatly all the new models and methods created. By example, lets create a new model and see how Loopback create all the methods.


 slc loopback:model



In this case i created a simple model, using memory as datasource. This is just for testing, because the model would not be persistent in the disk, just in memory. If we add data to this model, the data will be removed after restart the server.


Querying Data from IBMi

For 1-tier applications, we need to use an internal connector that will use the "db2.js" object from IBMi. There are another choices in case you want to run Looback 2-tier, but you will need DB2 Connect Driver.

Lets install the connector:

npm install loopback-connector-ibmi --save
The connector will be installed in node_modules folder. Now it is time for some configuration. The first step is to configure a datasource:

projectname/server/datasources.json

 {
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "ibmi-db": {
    "name": "ibmi-db",
    "connector": "ibmi",
    "username": "",
    "password": "",
     "database": "",
     "hostname": "",
     "schema": "my lib",
      "port":   50000
  }
}



Next step is create the model and specify what datasource the model will use.
We can select our favourite file, use dds. I will start simple and use an old DDS file instead of SQL:


 A                                
 A          R CUSTOMER            
 A            NAME          50A   
 A            EMAIL         50A   
 A            SALDO         10S 2 
 A            CUSTOMERID    10S 0 
 A          K CUSTOMERID          

And now, in Loopback, we configure the model, in server/model-config.json file. I will add the complete file, but check how we can add as many datasources and connections we want


 {
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "note": {
    "dataSource": "db"
  },
  "CUSTOMER": {
    "dataSource": "ibmi-db",
    "public": true
  },
  "People": {
    "dataSource": "db",
    "public": true
  }
}


In this file, you can see that the models "note" and "People" are using the datasource "db", which has been declared a in-memory datasource. "customer" model is using the datasource IBMi.

The last step is to create the model in common/models. We could do it also with the CLI command "slc loopback:model", but i will do it manually.

The model can describe the fields, data types, relations, REST methods,ACL,  etc. For simplicity, i will exclude them all and i will define only fields and data types.

In the folder server/models, we will need 2 files:

customer.js


 module.exports = function(CUSTOMER) {

};

and customer.json


 {
  "name": "CUSTOMER",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "CUSTOMERID": {
      "type": "Number",
      "id": true
    },
    "NAME": {
      "type": "string"
    },
    "EMAIL": {
      "type": "string",
      "required": true
    },
    "SALDO": {
      "type": "number",

      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

The connector "loopback-connector-ibmi" will take care to map this definitions to the database, create a connection pool and send SQL statements. i setup "debug" mode in the loopback-connector-ibmi , so we can see some info in the console.

So, we are done!. Now is just time to restart our loopback app and play with the API explorer.

Now it is time to play with the API Explorer.



TO-DO
The "loopback-connector-ibmi" is uncomplete and need to be tested for all CRUD operations and test connection pools. By Example the "delete" function still not working. It is not ready for production. Actually is under development and im trying to find time to complete it, but everyone is invited to help :)

LoopbackConnector could be used with DataJuggler without the need of LoopBack.


Here the source code.

REST-API for LoopBack and IBMi

LoopBack Connector for IBMi