Filter model data in LoopBack

Bram Borggreve
InstructorBram Borggreve
Share this video with your friends

Social Share Links

Send Tweet

In this lesson we will look at the filters that can be used to retrieve and manipulate the data stored in LoopBack.

We will use Postman to interact with the /Categories and /Products endpoints.

Using the URL parameters we will apply at the various filters LoopBack has to offer: include, order, where, fields, limit and offset.

[00:00] We can use filters to manipulate which data gets returned by loopback. Let's see this in action. We'll interact with the GetCategories and GetProducts endpoints. I used the API Explorer to create some sample data to play with.

[00:14] We can apply the filters using URL parameters. For example, with the include filter, we can include the products that belong in a category.

[00:24] Now, let's get to our products endpoints and we include the category in the result. We use the order filter to sort in price ascending or descending. We add a where filter to only return all products with a price higher than 200. Using the where filter, we can also do a wildcard search using like.

[00:43] The filter field is used to only show certain fields. When filtering on fields, we see that our category is no longer included in the results. To fix our include filter, we need to make sure that we add our foreign key in the fields filter. Once we add category ID to our fields filter, we see that the category is included again.

[01:03] Next, we add a limit filter to limit the number of items in our result. We can use the offset filter to skip items from the results. The last two filters can be useful if you want to page through the data.