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

@vlad160/odata-query-builder

v1.1.1

Published

OData qury builder with chain synatax

Downloads

24

Readme

OData Query Builder

pipeline status coverage report

OData query builder that uses chain sytax.

Table of Content

Install

    yarn add @vlad160/odata-query-builder

or

    npm i @vlad160/odata-query-builder --save

Usage

Use type helpers to build your query expression and call toString()

NOTE: Query is not encoded by default. Use toString({ encode: true }) for query encoding

Only OData4 provider is implemented for now. PR's with OData3 are accepted :)

Filter

You can use both ODataFilter or ODataQuery to build filter query.

Default logical operator is AND. You can change it by passing LogicalOperator directly to the ODataFilter constructor. See below for more examples

Simple filter

const query = new OData4Query();
query.filter(f => f.eq('Name', 'John')
    .gt('Age', 5)
    .toString()
)

Output

$filter=(Name eq 'John') and (Age gt 5)

Filter with fuctions

const query = new OData4Query();
query.filter(f => f.eq(x => x.toLower('Name'), 'John')
    .gt(x => x.round('Age'), 5)
    .toString()
)

Output

$filter=(tolower(Name) eq 'John') and (round(Age) gt 5)

Piping

const query = new OData4Query();
query.filter(f => f.pipe(
    eq(x => x.toLower('Name'), 'John'),
    gt(x => x.round('Age'), 5)
))

Complex filtering with logical operator changing

const filter = new OData4Filter();

filter
    .pipe(eq(x => x.toUpper('Surname'), 'SNOW'), gt('Age', 25))
    .eq('Name', 'John')
    .or(x => x.gt(x => x.length('Name'), 15))
    .not(x => x.eq('Name', 'Tition')
        .lt('Age', 50), LogicalOperator.OR);

Output

((toupper(Surname) eq 'SNOW') and (Age gt 25) and (Name eq 'John')) or (length(Name) gt 15) not ((Name eq 'Tition') or (Age lt 50))

Count

const query = new OData4Query();

query
    .count()

Output

$count=true

Skip/Top

const query = new OData4Query();

query
    .skip(5)
    .top(10)
    .toString()

Output

$top=10&$skip=5

Select

const query = new OData4Query();

query
    .select('Name', 'Surname')
    .toString()

Output

$select=Name,Surname

Search

const query = new OData4Query();

query
    .select('Some Query')
    .toString()

Output

$select=Some Query

OrderBy

Keep in mind that orderBy returns OrderedQuery and only skip, top and thenBy operators could be applied

const query = new OData4Query();

query
    .orderBy('Name', Order.ASC)
    .thenBy('Surname', Order.DESC)
    .skip(5)
    .top(228)
    .toString()

Output

$orderBy=Name asc,Surname desc&$top=228&$skip=5

Expand

Simple multiple expand

const query = new OData4Query();

query
    .expand(['Result', 'Items'])
    .toString()

Output

$expand=Result,Items

Expand nested property

const query = new OData4Query();

query
    .expand('Result/Items')
    .toString()

Output

$expand=Result($expand=Items)

Expand with query

const query = new OData4Query();

query
    .expand('Result', q =>
        q.select('Items')
            .filter(f => f.gt('Price', 5))
            .top(5))
    .toString()

Output

$expand=Result($select=Items;$top=5;$filter=Price gt 5)

Object based expand

const query = new OData4Query();

query
    .expand({
        'Result': {
            top: 5
        }, 'Items': {}
    })
    .toString()

Output

$expand=Result($top=5),Items

Piping

const query = new OData4Query();

query
    .pipe(select('Name'),
        expand('Result'),
        skip(5),
        top(10))
    .toString()

Output

$expand=Result&$select=Name&$top=10&$skip=5