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

Dynamic Form and Grid with C# in WPF

Ever wanted to generate the form & grid dynamically and add data from some data source? Here's to help you. Create a WPF project in Visual Studio. Add a UserControl and name it mainUserControl.xaml Now add a stackpanel just like this : <UserControl x:Class="TestApp.mainUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SaiData.TestApp" mc:Ignorable="d" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="5*"/> <ColumnDefinition Width="43*"/> </Grid.ColumnDefinitions>

Automate Build & Deploy React production build with gitlab-ci to Linux base server (AWS)

Right now as everyone wanting to automate literaly everything, this post will show you how you can automate the react build and deployment. I am using create-react-app getting started with react and assuming you have a similar setup. Setup .gitlab-yml file for gitlab auto deploy Here's the whole .gitlab-yml file that you can copy directly to root of your project. Breaking each line: image: node : 10 cache:   paths: - node_modules / This line define the node version that should be used by the docker instance of shared gitlab-runner and the path defines to cache the node_modules dir. before_script: - apt - get update - qq & & apt install - y openssh - client openssh - server - mkdir - p ~ / . ssh - echo - e " $SSH_PRIVATE_KEY " > ~ / deploy . pem - chmod 400 ~ / deploy . pem - '[[ -f /.dockerenv