npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

odata-query-builder

v0.0.7

Published

An eloquently fluent OData query builder.

Downloads

14,659

Readme

odata-query-builder

An eloquently fluent OData query builder.

Build Status js-standard-style

Install

yarn add odata-query-builder

or

npm install --save odata-query-builder

Then in your code...

const query = new QueryBuilder()
  .count()
  .top(5)
  .skip(5)
  .expand('NavigationProp')
  .orderBy('MyPriorityProp')
  .filter(f => f.filterExpression('Property', 'eq', 'MyValue'))
  .select('My Properties')
  .toQuery()

Outputs: ?$orderby=MyPriorityProp&$top=5&$skip=5&$count=true&$expand=NavigationProp&$filter=Property eq 'MyValue'

Filtering

Filter Expressions

Filter expresssions utilize logical operators to filter data on a specific property.

Operator Options:

  • Equal: eq
  • Not Eqaul: ne
  • Greater Than: gt
  • Greater Than or Equal: ge
  • Less Than: lt
  • Less Than or Equal: le
const query = new QueryBuilder()
  .filter(f =>
    f.filterExpression('Property1', 'eq', 'Value1')
  ).ToQuery();

Outputs: ?$filter=Property1 eq 'Value1'

Filter Phrases

Filter phrases are meant to be used with canonical functions. Filter Phrasing exposes the filter as a string which allows you to inject any of the various filtering mechanisms available in OData v4.

Below are a few examples:

const query = new QueryBuilder()
  .filter(f =>
     .filter(f =>
        f
        .filterPhrase(`contains(Property1,'Value1')`)
        .filterPhrase(`startswith(Property1,'Value1')`)
        .filterPhrase(`endswith(Property1,'Value1')`)
        .filterPhrase(`indexOf(Property1,'Value1') eq 1`)
        .filterPhrase(`length(Property1) eq 19`)
        .filterPhrase(`substring(Property1, 1, 2) eq 'ab'`)
  ).ToQuery();

Outputs: ?$filter=contains(Property1,'Value1') and startswith(Property1,'Value1') and endswith(Property1,'Value1') and indexOf(Property1,'Value1') eq 1 and length(Property1) eq 19 and substring(Property1, 1, 2) eq 'ab

Conditional Filtering Operators

By default when you utilize .filter you are using the and operator. You can be explict by passing your operator into the filter as a secondary parameter.

const query = new QueryBuilder().filter(f => f
      .filterExpression('Property1', 'eq', 'Value1')
      .filterExpression('Property2', 'eq', 'Value1'),
      'and'
    ).toQuery();

Outputs: ?$filter=Property1 eq 'Value1' and Property2 eq 'Value1'

const query = new QueryBuilder().filter(f => f
      .filterExpression('Property1', 'eq', 'Value1')
      .filterExpression('Property2', 'eq', 'Value1'),
      'or'
    ).toQuery();

Outputs: ?$filter=Property1 eq 'Value1' or Property2 eq 'Value1'

Nested Filter Chaining

Nested or grouped filtering is used when we need to write a more complex filter for a data set. This can be done by utilizing .and() or .or() with the filter.

 const query = new QueryBuilder().filter(f => f
      .filterExpression('Property1', 'eq', 'Value1')
      .filterExpression('Property2', 'eq', 'Value2')
      .and(f1 => f1
        .filterExpression('Property3', 'eq', 'Value3')
        .filterExpression('Property4', 'eq', 'Value4')
      )
    ).toQuery();

Outputs: ?$filter=Property1 eq 'Value1' and Property2 eq 'Value2' and (Property3 eq 'Value3' and Property4 eq 'Value4')

 const query = new QueryBuilder().filter(f => f
      .filterExpression('Property1', 'eq', 'Value1')
      .filterExpression('Property2', 'eq', 'Value2')
      .or(f1 => f1
        .filterExpression('Property3', 'eq', 'Value3')
        .filterExpression('Property4', 'eq', 'Value4')
      )
    ).toQuery();

Outputs: ?$filter=Property1 eq 'Value1' and Property2 eq 'Value2' and (Property3 eq 'Value3' or Property4 eq 'Value4')

Reminder: We can still explicitly control the conditional operators within each of the filters by utilizing the filter's condition operator parameter which gives us even more control over the filter.

 const query = new QueryBuilder().filter(f => f
      .filterExpression('Property1', 'eq', 'Value1')
      .filterExpression('Property2', 'eq', 'Value2')
      .or(f1 => f1
        .filterExpression('Property3', 'eq', 'Value3')
        .filterExpression('Property4', 'eq', 'Value4')
      ),
      'and'
    ).toQuery();

Outputs: ?$filter=Property1 eq 'Value1' and Property2 eq 'Value2' and (Property3 eq 'Value3' or Property4 eq 'Value4')