Meet Loopback!
How to Create an Application Using the Loopback Framework
Most of us know several backend frameworks. Well, today we are going to expand your circle of acquaintances.
Meet IBM’s Loopback!
Loopback is just one of the Strongloop products, but that’s what this article will talk about since it provides basic functionality for writing the backend.
So, why do we need to stuff your head with knowledge of another framework? There is a list of reasons for that:
- It’s free (but if anyone wants to use the product with lots of additional functionality then he can pay for it);
- A big Loopback developer community;
- It’s easy to deploy backend from the start;
- The ability for fast model migration from the finished database (DB);
- The ability for model auto-migration to the empty DB;
- Graphical user interface Explorer;
- The ability to set a complicated nested structure of the received object easily;
- The ability to set a push notification technology easily;
- Easy to work with relational and non-relational DB;
- A big open database of working examples (that’s important!) with step-by-step instructions and full documentation.
Impressive? And that’s not all…
We programmers mostly do not want to write a code from scratch, we want to use turnkey solutions! We want to save our time! Аnd Loopback gives us this opportunity. The combination of a large number of built-in features with clear and intuitive interface and the most detailed examples allows you to orient yourself and create working servers easily.
Do you really think that it’s difficult to create a server application? Let me bring you over!
I was interested in learning new methods of creating the access control lists (ACL) in the Loopback application. Wherein, I set myself a task to create such an application with MongoDB database.
Loopback documentation is interesting and clear but quite extensive. So armed with a keyboard, I asked Google «loopback example acl». Google responded faithfully, Google responded genuinely: https://github.com/strongloop/loopback-example-access-control.
Yes, just like that we get a very detailed example. Just one thing is missing — MongoDB. However, we’ll survive. We can just install loopback-connector-mongodb module and set up the application. Although it is perhaps too difficult. What if we write down in the terminal window the «slc loopback:datasource» command?
So slowly and gently, we came to the Loopback application creation. And all we need is just five commands:
slc loopback
slc loopback:datasource
slc loopback:model
slc loopback:relation
slc loopback:acl
The first command creates the project directory with needed files and folders’ structure and installs the necessary modules.
The second command installs the connector module for a chosen database. Besides, there is quite an extensive list of alternate databases except for MongoDB and MySQL.
The slc loopback:model
command helps us to create and set up the needed models that subsequently will be tables and database collections with customized fields of relevant types.
The slc loopback:relation
command helps to install the needed connections properly between the established models so that we could r request complex objects with needed information from the backend via a standard query.
Finally, the salt why the whole game was started — ACL (access control lists). The slc loopback:acl
command tells us what rule types can be established.
Each of the above commands provides a set of selectors and tips that greatly simplifies and accelerates the process of creating entities.
The demonstrated example uses a different database than the one kindly provided by the Loopback framework. For some ORM this detail could be a serious or even insurmountable problem, but not for Loopback! It’s as easy to use a different database as to select a suitable connector for server applications from the list. The only correction that has to be made is to change an id field type from the number to string value.
Thus, we install ACL for:
- all methods of all models;
- all methods of a chosen model;
- one particular method of a chosen model.
We can provide an access for for both users as well as for certain categories of users:
- defined by the framework (for example, authenticated);
- defined by roles (for example, admin);
- selected according to some principle (for example, teamMembers).
For illustration, the methods of one of the models in the Explorer Loopback are shown in the screenshot.
In the result of this method execution, we receive:
Wherein, we can create methods and principles of sampling in an arbitrarily complex and sophisticated form. Instead of teamMembers, it can be a user named “Jack” or people whose relatives suffer from allergies to peanuts (the latter option, however, implies a slightly different structure of the database to obtain the required information about the user).
Strongloop composer is a visual editor that configures and manages application performance. It is an alternative to the project management through the terminal.
So, you need to do the following steps:
- perform all the commands above in the terminal window;
- create user, team, project models and connections between them, implying the presence of the owner and participants in each team and each project;
- create access rules for all categories of users: both for created administrator role and a group of all authorized users;
- shamelessly copy three small non-standard methods (listProjects, donate, withdraw) for the project model from the example ( writing these methods is by no means the goal of this article);
- create an example of a simple algorithm for determining teamMembers.
As a result, we obtain a turnkey project.
You can launch the project with the node .
command in the terminal. After that, we can contemplate the power of our project created in the GUI Explorer link http://0.0.0.0:3000/explorer. It has all the models we created and for each of them, there is a long list of standard methods with a brief description. In addition, there is a list of user’s methods with a description defined by a user. Moreover, each of the methods can be tested in action, and all changes will be properly entered in the database.
Little remains to be done — we need to choose the right tool for writing the frontend and actually implement it, referring to the finished REST API, that was created in a few minutes using Loopback.
Conclusion
In this article, my goal was to demonstrate how easy it is to create an application using the Loopback framework. The source code is available at the link https://github.com/ShepelE/loopback-mongo-acl. Using this project, you can create your own app just in a few minutes.
And if you need professional assistance, don’t hesitate to contact us!
If you liked this, show your support by clapping us to share with other people on Medium.
Follow us on Facebook, Instagram, LinkedIn, Behance, Medium and visit our corporate blog for more news and articles on smart solutions.
Any questions? Feel free to contact us!