Skip to content

strongloop/loopback-filters

Repository files navigation

loopback-filters

⚠️ LoopBack 3 has reached end of life. We are no longer accepting pull requests or providing support for community users. The only exception is fixes for critical bugs and security vulnerabilities provided as part of support for IBM API Connect customers. (See Module Long Term Support Policy below.)

We urge all LoopBack 3 users to migrate their applications to LoopBack 4 as soon as possible. Refer to our Migration Guide for more information on how to upgrade.

Overview

This module implements LoopBack style filtering without any dependencies on LoopBack.

Install

$ npm install loopback-filters

Usage

Below is a basic example using the module

var applyFilter = require('loopback-filters');
var data = [{foo: 'bar'}, {bat: 'baz'}, {foo: 'bar'}];
var filter = {where: {foo: 'bar'}};

var filtered = applyFilter(data, filter);

console.log(filtered);

The output would be:

[{foo: 'bar'}, {foo: 'bar'}]

Features

Where

// return items where
applyFilter({
  where: {
    // the price > 10 && price < 100
    and: [
      {
        price: {
          gt: 10
        }
      },
      {
        price: {
          lt: 100
        }
      },
    ],

    // match Mens Shoes and Womens Shoes and any other type of Shoe
    category: {like: '.* Shoes'},

    // the status is either in-stock or available
    status: {inq: ['in-stock', 'available']}
  }
})

Only include objects that match the specified where clause. See full list of supported operators.

Geo Filter / Near

applyFilter(data, {
  where: {
    location: {near: '153.536,-28'}
  },
  limit: 10
})

Sort objects by distance to a specified GeoPoint.

Order

Sort objects by one or more properties.

Limit / Skip

Limit the results to a specified number. Skip a specified number of results.

Fields

Include or exclude a set of fields in the result.

Note: Inclusion from loopback is not supported!

Docs

See the LoopBack docs for the filter syntax.

Module Long Term Support Policy

This module adopts the Module Long Term Support (LTS) policy, with the following End Of Life (EOL) dates:

Version Status Published EOL
1.x End-of-Life Dec 2017 Dec 2020

Learn more about our LTS plan in docs.