Skip to main content

Custom application logic for Loopback models

This post is the continuation of the previous one
What if you want to customize your REST API or add different methods for it?When building an application, you’ll generally need to implement custom logic to process data and perform other operations before responding to client requests.
In Loopback there are 3 ways to do this:
  1. Adding logic to models
  2. Defining boot scripts
  3. Defining middleware
For this post I'm explaining about the first one, Adding logic to models. There are 3 types to add custom application logic to model.
  • Remote Method
  • Remote Hook
  • Operation Hook

Remote Method :

A remote-method is nothing but nodejs function mapped to REST endpoints. You can create remote-method by cli or typing by yourself (click here to know how). 
In the previous post, I used memory db. For this one, I'm going to use MongoDB as datasource. You need to install loopback-connector-mongodb to connect to MongoDB.
npm install loopback-connector-mongodb --save
Then you need to add datasource :
lb datasource
Enter the datasource name and select MongoDB connector from the list. Then, configure the MongoDB database configuration.
After adding datasource, create a model as described in the previous post (select the freshly created datasource).
Now, for creating remote-method, run this command :
lb remote-method
Select the model, in which you want the remote-method, enter the name of remote-method, enter endpoints (you can set multiple endpoints for the same remote-method) to be mapped to this remote-method, select method type (get, post, put, delete) and set arguments to be passed with the appropriate type.

? Select the model: library
? Enter the remote method name: test
? Is Static? Yes
? Description for method: 

Let's configure where to expose your new method in the public REST API.
You can provide multiple HTTP endpoints, enter an empty path when you are done.
? Enter the path of this endpoint: /test
? HTTP verb: post

Let's add another endpoint, enter an empty name when done.
? Enter the path of this endpoint: 

Describe the input ("accepts") arguments of your remote method.
You can define multiple input arguments.
Enter an empty name when you've defined all input arguments.
? What is the name of this argument? name
? Select argument's type: string
? Is this argument required? Yes
? Please describe the argument: 
? Where to get the value from? (auto)

/**
*
* @param {string} name
* @param {Function(Error)} callback
*/

library.test = function (name, callback) {
// TODO
callback(null);
};


At last, there is a sample code given in cli, copy that and paste it into your model.js. You can write your logic instead of // TODO. If you are returning anything in response, you have to set return type in the model.json. If you are returning an Object set type "object" and if there is no root in an object or other type set root to "true". 

"returns": {
"type": "object",
"root": true
},

To return response use callback(). callback accepts two arguments, first as error and second as success. If there is some logical error you just use this callback(error), otherwise callback(null,success).
This remote-method is now listed in loopback explorer.

Remote Hook :

Basically, hooks in loopback used to do something before the method executes or after the method executes. Sometimes you got some parameters with a blank value, then you have to set the default value for them, so you can use before hook. Just like that, sometimes you have to process something after the method is done executing, so you use after hook.
This runs before the remote method :

modelName.beforeRemote( methodName, function( ctx, next) {
//...
next();
});

This runs after the remote method :

modelName.afterRemote( methodName, async function( ctx) {
//...
return;
});

Remote hooks are provided with a Context ctx object that contains transport-specific data (for HTTP: req and res). The ctx object also has a set of consistent APIs across transports.
The context object passed to afterRemoteError() hooks has an additional property ctx.error set to the Error reported by the remote method.

Operation Hook :

Operation hooks are not tied to a particular method, but rather are triggered from all methods that execute a particular high-level create, read, update, or delete operation. These are all methods of PersistedModel that application models inherit.

So, basically, if you want to check the data passed in the built-in create operation, use before create-hook. Similarly, after the create operation, on the basis of the response you need to add some logs or want to perform some action using after create-hook.
These are the operation hooks supported in loopback :
access, before save, after save, before delete, after delete, loaded, persist
Here is how you use these hooks. I am using only after save and before save here, for example, you can use any of above you need. This is after save method,


Library.observe('after save', function (ctx, next) {
ctx.instance //the request data
ctx.Model.modelName //name of the model
next();
})

Here's before save,

Transaction.observe('before save', async function (ctx, next) {
ctx.instance //the request data
ctx.Model.modelName //name of the model
next();
})


Here's the link for the official documentation:
https://loopback.io/doc/en/lb3/Adding-logic-to-models.html



Happy Coding

Comments

Popular posts from this blog

Live Game Streaming on YouTube

Hello Guys, Today I gonna talk about how to stream your mobile gameplay to YouTube Gaming. Nowadays, PUBG (PlayerUnknown's BattleGround) is the most trending mobile game. Many Pro players upload/stream their gameplay to YouTube and earn money through monetization. Ok. Let's start. First of all Download YouTube Gaming App. After download, open the app and create/choose your google account for your YouTube channel. If you're a new user then you have to wait for 24 hours until you stream your gameplay (YouTube need to activate your account). Now Just click on the first icon(streaming icon) on the top left corner and this screen will appear: Click Next and select Stream if you want to live stream your gameplay or Record if you want to save the video to your phone and upload it later. Select the video format you want to upload/stream. Click next, you'll see a tips screen, read it and click next. Now, Select the game you want to stream or record. Add the st

Give Her Wings - Women's day special

Woman equality . A burning issue in the current world. How we can manage to get through this? an obvious answer is " Equal parenting to boy and girl." This is the base to start achieving the goal. Why differentiate the parenting to boy and girl? Giving boys all the freedom and for a girl its ban to go out alone! Isn’t there something wrong? Why should boys have all the fun? If you have a baby girl, it’s a blessing, cheer her, encourage her and protect her. Yes, it’s hard to be a parent of a girl child in this cruel minded culture where we read the news of acid attacks and rap every second day. Yes, it is the duty of every parent to make their children safe, But don’t be so overprotective that she feels the burden over her head. As Newton's third law, as hard as you push the spring, it’ll bounce back to you with the same force.  Give her freedom to be herself. If you gave her good sacrament than belief in her, she’ll not do anything wrong that ruin

Electron app with AngularJs and MySQL Database

Hello folks,  Do you know about Electron API (a GitHub repository, helping a web developer to make standalone cross-platform app)? The electron can make any web app to Desktop Application in all three major OS (i.e. Windows, Mac, Linux). Okay, enough intro about Electron, let's move on Problem... So, if you are an Angular Developer and you want to make a Desktop app having a database in MySQL, you came to right place!  The Directory structure should look like this : AngularElectronMySQL ->      -Electron      -WebApp (Angular app)      -Setup So, First Initialize your Angular project using angular-cli  (you can use also build it simply). Here, I am assuming that you completed your Angular Project and Built it. Just one more thing add a data-service for getting data from MySQL. Subscribe to the MySQL server (for our case: http://localhost:1024). After that, you need to copy the files from the built project to electron folder. I used npm like this :  Now, for Electr