Loopback: easy-peasy it's ready to serve APIs

Loopback: easy-peasy it's ready to serve APIs

Not only keeping data, Data Engineers can control data output to customers. I had many chances to allow other developer teams to integrate out data via API and here is the time to talk about creating the APIs.

How can we take less effort to build an API. There is a tool I have experienced with to do so with not much energy as cost.

Credit: https://loopback.io

Loopback is a NodeJS framework to build API endpoints at ease. Now it’s version 4 with various features affording us to create an API in an easy way, How easy? Let’s go.

1. Start from Docker

docker pull node:14-alpine3.10
docker run -it -d -p 3000:3000 – name node_lb node:14-alpine3.10 node
docker exec -it node_lb /bin/sh

We are pulling an image node:14-alpine3.10 since Loopback version 4 can support up to NodeJS version 14. Newer versions are not yet now.

Then run -it to create a container named “node_lb” with the port 3000 for Loopback services. And node at the last is to run the entry point.

Finally exec -it to go inside and install Loopback package.

2. Install Loopback

Refer to https://loopback.io/doc/en/lb4/Getting-started.html the official doc, install Loopback package as the following commands.

npm i -g @loopback/cli
lb4 app

After lb4 app, we have to write the information of the app. In this time we put “test” and select all default values. Now the folder “/test” is created.

cd test
npm start

run npm start inside the app folder.

Yes, we got it. Open a browser and enter http://localhost:3000 and we can see this.

Click the link of the API Explorer in order to view the swagger or the API contents. It’s done!

3. Connect to a database

Let’s say we have this table “test_students” in MySQL server. How can we handle this data from the Loopback?

3.1 create Loopback datasource

run lb4 datasource and it’s prompted to put information of other databases. Of course, we need to connect to MySQL.

3.2 create Loopback model

A model is an object of the API data. Create a model by the command lb4 model.

Noted that we are creating an object with ID, therefore the model base class has to be “Entity“.

Now we have “TestStudents” model with a number “id” and a string “name“. The fields are matched to the MySQL table.

3.3 create Loopback Repository

Repository is a pair of datasource and model to handle the data from/to a database. Run lb4 repository and select the values we desire.

3.4 create Loopback Controller

The last one, run lb4 controller to create a controller which is for handle actions of the APIs such as showing values or updating values into the databases.

Select “REST Controller with CRUD functions” in order to build CRUD methods of this table.

3.5 Execute it

Try npm start and play around. An error “ER_NO_SUCH_TABLE” appeared. What’s going on?

From the error, we noticed the datasource is “TestStudents” from Loopback’s automatic renaming that is not matched to the actual table “test_students“. The solution is adding an attribute "name" like this below in the model file, "test/src/model/model_name.model.ts".

@model ({ name: 'real_tablename' })

Got a solution from this forum https://stackoverflow.com/questions/52823165/how-can-i-set-the-mysql-table-name-for-a-model-on-loopback-4

Again, npm start to find it works. We can fetch data in MySQL via our API. Yeah!


Loopback is so easy to build an API. Moreover, we can add other functions or libraries of NodeJS to control security, stylings, optimizations, or etc. as we needed.

Show Comments