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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-odata-filter

v0.0.10

Published

A typescript library for buiding OData filter string

Readme

ts-odata-filter

A typescript library for buiding OData filter string

npm version

Install

npm install ts-odata-filter --save

Usage

Basic usage

import { ODataFilterBuilder, BuilderFunc, BoolArg } from 'ts-odata-filter'

interface IEntity {
  id: number
}

interface IProduct extends IEntity {
  name: string
  description: string
  price: number
}

interface IOrderItem extends IEntity {
  product: IProduct
  quantity: number
  price: number
  parentOrder: IOrder
}

interface IOrder extends IEntity {
  customer: ICustomer
  status: 'CREATED' | 'PAID' | 'CANCELLED'
  createdDate: Date
  orderItems: IOrderItem[]
}

interface ICustomer extends IEntity {
  name: string
}

// build() method accepts a callback which first argument is an ODataFilterBuilder instance
// builder has members which correspond to OData operators and functions (eq, ne, contains, and, or, not, etc.)
// builder also has "prop" member which allows to use strongly typed entity property names
ODataFilterBuilder.build<IOrder>(builder => builder.eq(builder.prop.customer.id, 123)).getString(); // returns 'customer/id eq 123'

ODataFilterBuilder.build<IOrder>(
  builder => builder.eq(builder.prop.someProperty, 123)
).getString(); // compilation error (unknown property 'someProperty')

ODataFilterBuilder.build<IOrder>(
  builder => builder.eq(builder.prop.customer.id, '123')
).getString(); // compilation error (string '123' is not assignable to customer.id)


// builder.prop is also passed as the second arg of a callback 
const filter: BuilderFunc<IOrder> = (b, p) => 
  b.and(
    b.eq(p.customer.id, 123),
    b.eq(p.status, 'PAID')
  );

ODataFilterBuilder.build<IOrder>(filter).getString(); // returns '(customer/id eq 123 and status eq 'PAID')'

Destructuring builder argument


// you can use destructuring to make the code shorter or prettier 
const filter: BuilderFunc<IOrder> = ({eq, and}, p) => 
  and(
    eq(p.customer.id, 123),
    eq(p.status, 'PAID')
  );

ODataFilterBuilder.build(filter).getString(); // returns '(customer/id eq 123 and status eq 'PAID')'

Working with collections


// find all orders with iphones 
const filter: BuilderFunc<IOrder> = (b, p) =>
  b.collection(p.orderItems, 'i').any(
    i => i.contains(i.prop.product.name, 'IPhone')
  );
  
ODataFilterBuilder.build(filter).getString(); // returns 'orderItems/any(i: contains(i/product/name, 'IPhone'))'

Reusing filter expressions


// find all iphones
const filterIphones: BoolFunc<IProduct> = (b, p) => 
  b.or(
    b.contains(p.name, 'IPhone'),
    b.contains(p.description, 'IPhone')
  );
ODataFilterBuilder.build(filterIphones).getString(); // returns '(contains(name, 'IPhone') or contains(description, 'IPhone'))'

// find all iphones selled for more than $1000
const filterOrderItems: BoolFunc<IOrderItem> = (b, p) =>
  b.and(
    b.nested(p.product, filterIphones),
    b.gt(p.price, 1000)
  );

// returns '((contains(product/name, 'IPhone') or contains(product/description, 'IPhone')) and price gt 1000)'
ODataFilterBuilder.build(filterIphones).getString(); 

// find all orders with iphones for more than 1000$ where customer name is Jonh 
const filterOrders: BoolFunc<IOrder> = (b, p) =>
  b.and(
    b.collection(p.orderItems, 'i').any(filterOrderItems),
    b.contains(p.customer.name, 'John')
  );

// returns '(orderItems/any(i: ((contains(i/product/name, 'IPhone') or contains(product/description, 'IPhone')) and price gt 1000)) and contains(customer/name, 'John'))'
ODataFilterBuilder.build(filterOrders).getString();