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

query-builder-odata

v1.5.0

Published

query-builder-odata builds OData v4 queries.

Downloads

46

Readme

query-builder-odata

query-builder-odata builds OData v4 queries.

NPM Version Action Status Coverage Status License

Installation

yarn add query-builder-odata

or

npm install --save query-builder-odata

Create a query

import { QueryBuilder } from 'query-builder-odata';

const query = new QueryBuilder()
  .top(10)
  .skip(10)
  .count()
  .select('name')
  .orderBy('name')
  .expand('Books', e => e.select('title'))
  .filter(f => f.eq('name', 'John Doe'))
  .toQuery();

query => "?$top=10&$skip=10&$count=true&$select=name&$orderby=name&$expand=Books($select=title)&$filter=name eq 'John Doe'"

Usage

Pagination

Top

const query = new QueryBuilder()
  .top(10)
  .toQuery();

query => "?$top=10"

Skip

const query = new QueryBuilder()
  .skip(10)
  .toQuery();

query => "?$skip=10"

Counting

const query = new QueryBuilder()
  .count()
  .toQuery();

query => "?$count=true"

Selecting

const query = new QueryBuilder()
  .select('id', 'name')
  .toQuery();

query => "?$select=id,name"

Ordering

const query = new QueryBuilder()
  .orderBy('id desc' ,'name')
  .toQuery();

query => "?$orderBy=id desc,name"

Expanding

const query = new QueryBuilder()
  .expand('Books')
  .toQuery();

query => "?$expand=Books"

Nested expand

const query = new QueryBuilder()
  .expand('Books', e =>
    e.expand('Chapters')
  )
  .toQuery();

query => "?$expand=Books($expand=Chapters)"

Nested expand with other queries

Expand can be used together with top, skip, count, select, orderBy, filter

const query = new QueryBuilder()
  .expand('Books', e => e
    .select('title')
    .orderBy('isbn')
  )
  .toQuery();

query => "?$expand=Books($select=title;$orderBy=isbn)"

Filtering

const query = new QueryBuilder()
  .filter(f => f.eq('name', 'John Doe'))
  .toQuery();

query => "?$filter=name eq 'John Doe'"

Comparison operator

Supported operators:

  • eq, ne, gt, ge, lt, le, in
  • startswith, endswith, contains

When using multiple operators, the logical and operator is used by default.

const query = new QueryBuilder()
  .filter(f => f
    .startsWith('name', 'John')
    .gt('rating', 5)
  )
  .toQuery();

query => "?$filter=startswith(name, 'John') and rating gt 5"

Logical operator

Supported operators: and, or

const query = new QueryBuilder()
  .filter(f =>
    f.or(o => o
      .startsWith('name', 'John')
      .gt('rating', 5)
    )
  )
  .toQuery();

query => "?$filter=startswith(name, 'John') or rating gt 5"

Duplicate query operators

When adding multiple query operators of the same type, one of two scenarios will happen: overriding or addition.

Overriding

The following operators will override the existing value: top, skip, count

const query = new QueryBuilder()
  .top(10)
  .top(15)
  .toQuery();

query => "?$top=15"

Addition

The following operators will add to the existing value: select, orderBy, expand, filter

const query = new QueryBuilder()
  .select('name')
  .select('age')
  .toQuery();

query => "?$select=name,age"

More examples can be found in the tests.