[IBM LoopBack 101] A Small ToDoList API with MongoDB

In this article, as a Distributed Systems 2020–21 Class project, we will make small ToDo List API that will save data into a MongoDB database. Keeping in mind that exist many different ways to make this work, where in this article we will show a first attempt to make this done.

“LoopBack is a highly extensible, open-source Node.js framework based on Express that enables you to quickly create dynamic end-to-end REST APIs and connect to backend systems such as databases and SOAP or REST services.With LoopBack, you can easily create models based on a schema (if you have one) or define models if you don’t and build relationships between models.”

developer.ibm.com/

Having the Visual Studio Code and MongoDB installed on a Windows 7 machine, we will start the VSCode and on the left of the window, on the explorer, we will create a new folder named SmallToDoListApi.

Right-click again on the folder explorer item and click Open in Integrated Terminal (and choose as New Command Prompt in the dropdown menu).

Navigate to the SmallToDoListApi folder early created, and execute the following command to install the LoopBack 4 CLI toolkit:

npm i -g @loopback/cli

Now, let’s install the MongoDB connector for the LoopBack framework:

npm install loopback-connector-mongodb --save

To generate our application using the LoopBack 4 CLI toolkit, we going to execute multiple commands on the terminal and fill the asked fields (when showed ‘(something)’, the something it’s usually the advised value).

Note: Pay attention on the following pictures to know witch answer was used/selected.

lb4 app

As the last command as created the project folder named small-to-do-list-api, navigate into it (usinf cd small-to-do-list-api) and continue.

lb4 model
lb4 datasource
 lb4 repository
lb4 controller
npm start

We can use MongoDB client and see if it was, in fact, added into the database. As the database was called todo and the collection Todo, we can use the following commands to show the POST result:

// To enter into the desired database
use todo
// To enter into the desired collection
use Todo
// To show content
db.Todo.find().pretty()

As we can see, our API is working as intended.

Hope you liked this small article, best regards, Ricardo Costa (Richards).

This article was created in a context of the Distributed Systems Class 2020–21, ESTG-IPG.