odata-fluent-query
Client side queries with extensive filtering and typesafe joins
This lib only generates the query string, so you need to use it with your own implementation of http request. There is no need to scaffold any pre build model.
Filtering with filter
Every query exposes a method called filter
. This method accepts a function as parameter that builds an expression.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>()
.filter(u => u.id.equals(1))
.toString()
Note that the parameter u
is not a User
type but FilterBuider<User>
. The FilterBuider
will exposes all properties from T
as FilterBuilderType
to provide all filter functions based on its property type which can be:
FilterCollection
FilterString
FilterNumber
FilterBoolean
FilterDate
FilterBuilder
Check out all the available methods here.
export type FilterBuider<T> = {
[P in keyof T]: FilterBuilderType<T[P]>
}
You can modify/combine expressions using not()
, and()
and or()
.
.filter(u => u.username.contains('dave').not())
.filter(u => u.emailActivaed.equals(true).and(u.username.contains('dave')))
Calling filter
multiple times will merge the expression in a bigger expression using the and
operator. In this example you will get the users where "the id is not equal to 1 AND the username start with 'harry'".
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>()
.filter(u => u.id.notEquals(1))
.filter(u => u.username.startsWith('Harry'))
.toString()
More filter examples:
odataQuery<User>().filter(u => not(u.id.equals(1)))
odataQuery<User>().filter(u => u.id.equals(1).and(
u.username.startsWith('Harry')
)))
odataQuery<User>().filter(u => u.id.equals(1).or(
u.username.startsWith('Harry')
)))
odataQuery<User>().filter(u => u.email.startswith(u.name))
You can also use "key selector" passing the property key at the first parameter.
odataQuery<User>().filter('id', id => id.equals(1))
Selecting properties with select
select
is used to select a set of properties of your model:
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>().select('id', 'username')
Ordering with orderBy
orderby
is used to order the result of your query. This method accepts a function that returns the property you want to order by.
odataQuery<User>().orderBy(u => u.id)
It is possible to order on relations:
odataQuery<User>()
.select('username')
.orderBy(u => u.address.city)
You can set the order mode by calling Desc
or Asc
.
odataQuery<User>().orderBy(u => u.id.desc())
You can also orderBy
with key string.
odataQuery<User>().orderBy('id', 'desc')
Expanding with expand
expand
is used to load the relationships of the model within the current query. This query can be used to filter, expand and select on the relation you are including.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>()
.expand('blogs')
.toString()
All the query methods are available inside an "expand" call.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>()
.expand('blogs', q =>
q
.select('id', 'title')
.filter(b => b.public.equals(true))
.orderBy('id')
.paginate(0, 10)
)
.toString()
It's possible to nest "expand" calls inside each other.
import { odataQuery } from "odata-fluent-query";
odataQuery<User>()
.expand('blogs', q => q
.select('id', 'title')
.expand('reactions' q => q.select('id', 'title')
))
.toString();
Key getters can easily get to deeper levels.
odataQuery<User>()
.expand(
u => u.blogs.reactions,
q => q.select('id', 'title')
)
.toString()
Grouping with groupBy
groupBy
uses odata $apply
method to group data by property with optional aggregations.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>().groupBy(['email']).toString()
It's posible to apply custom aggregations.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>()
.groupBy(['email', 'surname'], a =>
a.countdistinct('id', 'all').max('phoneNumbers', 'test')
)
.toString()
Paginating with paginate
paginate
applies $top
, $skip
and $count
automatically.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>().paginate(10).toString()
Skip and top.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>().paginate(25, 5).toString()
Using object and setting count
to false.
import { odataQuery } from 'odata-fluent-query'
odataQuery<User>().paginate({ page: 5, pagesize: 25, count: false }).toString()
Development
Dependencies are managed by using npm
. To install all the dependencies run:
npm
To build the project run:
npm build
The output files will be placed in the build
directory. This project contains unittest using jest
and ts-jest
. They are placed in the test
directory. To run all the test run:
npm run test
After this you can open coverage/lcov-report/index.html
in your browser to see all the details about you tests. To publish the package you can run:
npm publish