Here we go again
Hi vatos ! today i’am gonna talk to you a little bit about loopback4, a not so popular framework created with NodeJs (only 3k stars for the version 4) by IBM, who allow you to create your API for prototype within a few minutes.
As i said previously, loopback4 will allow you to create a nodejs API in a few minutes, but how can he do this ? dont we have to use an agnostic framework like expressjs in order to build a real personnalised architecture instead ? An architecture who suits perfectly to my company, my process, my wife and my dog ?
No. Really, you dont have to do that.
If you’re trying to prototype an app, you’re trying to convince people to believe in your project.
No matter the complexity of it, the goal here is to make thing simple and clear, before building a solid and robust architecture.
Loopback4 is pretty cool because it combines the both advantages about API generator and solid architecture building. it provide a standard nodejs API pattern with all that you’ll can find in a classic developed way API.
Did you say generator ?
Exactly. Loopback4 is built in the way that you can think directly about the data that you want to treat. It’s built around Domain Driven Developement (Aka DDD) a pattern that focus on the business logic of your application.
Theres loopback 4 generator for :
- Models (Your data model)
- Controllers (Your routes containing generated DB manipulation for CRUD operation)
- Datasources (the DB that you want to plug to your API)
Show me what you got !
Okay, now that your convinced, lets create our first API using loopback4. The first step about that is to generate your API. For this, you will need :
- NodeJs (you can find him here : https://nodejs.org/en/ )
- Loopback4 (
npm i -g @loopback/cli)
Then, you can go to your terminal and generate your api by typing in your console :
Loopback4 will ask you multiple informations about your project like the title, the description, and the default options that you will use for your project.
Once everything is okay in terms of generation, you can access your api by :
- Accessing your folder
npm startin your console
Then, you can go to your http://localhost:3000/
1. Generating our model
Lets be simple and efficiant, for this tutorial, we’re gonna treat cars, so we will generate a car model with multiple informations inside by typing
lb4 model car in our console
Once you enter this command, loopback will ask you about the property that you want inside this model. When everything is okay, the file will be generated and available inside the model folder in your generated API.
2. Generating our datasource
Datasource is the data connector to your DB system, loopback provide also a generator who allow you to generate the link to this datasource to persist your data in your API.
You can generate a datasource using the command :
lb4 datasource car
for this example, we will use the in-memory DB. in-memory DB will continue to persist data while the API is running in our server. For production usage, i would recommend to use a classic database connector like MongoDB or SQL database.
Once you enter this command, loopback will ask you about the property that you want inside this datasource. When everything is okay, the file will be generated and available inside the datasources folder in your generated API.
Generating our repository
To generate a service, you can use the followed command :
lb4 repository car
Once everthing is generated, a new repository file will be generated into our reposiroty folder. This file will contain all the mapping relation between our car model generated previsously.
Generating our controllers
Last thing, in order to make all this previously generated component work with each other, you’ll need to create a controller. The controller is the orchestrator of your API call actions.
It will allow you to do operation related to the linked model / repository that you’ve generated before.
Loopback 4 generated controller allow you to have a fully CRUD REST file with all the classic operations that you’ll do on an application or a website such as GET, POST, PUT, PATCH, DELETE
Once its generated, you’ll see a new file created in the controller folder of your Loopback 4 API with all the actions listed before.
Launch it !
You can now start your new fresh API by running a yarn / npm start and see whats happen.
If you go to your http://localhost:3000 you’ll see the default API page with two links (to openapi.json and generated documentation about your API)
Test it !
If you want to test a request to the API, you should use http client in order to see whats really happen during the call. We are gonna use Postman for this (but other client will suits well too)
/POST a new car :
/GET cars list :
As you can see, everything seems to be runing. If we want to go further, we can also add a generated ID propertys instead of specifying it at each /post request.
Conclusion & usefull links
As you saw, Loopback 4 is a very nice framework with an incredibely complete generator in order to go straight to the point. In the next article, i’ll talk about my personal experience using it and how you can implement an authentication system on your API.
If you got questions, i got answers, feel free to contact me.
And if you want to implement it in your company, feel free also to contact us at this website : https://grow-your-business.fr/ (this is our agency)
You can also find the github source of this article here :
Have a nice day ! :-)