Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
odata-builder
Advanced tools
Readme
Generating odata queries typesafe. No more worries about wrong queries. Know that the query is correct while writing it.
To install the package use your favourite package mananger, e.g.:
npm install --save odata-builder
or
yarn add odata-builder
const item = {
someProperty: 'someValue',
}
const queryBuilder = new OdataQueryBuilder<typeof item>()
.count()
.filter({field: 'someProperty', operator: 'eq', value: 'test'})
.skip(10)
.top(100)
.select('someOtherProperty1', 'someOtherProperty2')
.toQuery();
// ^ ?$count=true&$filter=someProperty eq 'test'&$skip=10&$top=100&$select=someOtherProperty1, someOtherProperty2
If you want to count and only receive the amount of data:
const queryBuilder = new OdataQueryBuilder<MyAwesomeDto>()
.count(true)
.filter(...) // only for demonstrating the count
.toQuery();
// ^ /$count?$filter=....
You can also decide if you want to have single quotes when querying with guid
import { Guid, OdataQueryBuilder } from 'odata-builder';
// You could type your id directly as guid
type MyAwesomeDto = {
id: Guid;
...
}
const filter = {
field: 'id',
operator: 'eq'
value: 'f92477a9-5761-485a-b7cd-30561e2f888b', // must be guid
removeQuotes: true, // if not defined the guid will be added to the query with single quotes
}
const queryBuilder = new OdataQueryBuilder<MyAwesomeDto>()
.filter(filter)
.toQuery();
// ^ ?$filter=id eq some-guid
If there is an array in your item, you can use lambda expressions in oData to filter for them:
type MyAwesomeDto = {
...
someProperty: string[]
...
}
const queryBuilder = new OdataQueryBuilder<MyAwesomeDto>()
.filter({
field: 'someProperty',
operator: 'contains',
value: 'test',
lambdaOperator: 'any',
ignoreCase: true,
})
.toQuery();
// ^ ?$filter=someProperty/any(s: contains(tolower(s), 'test'));
There is also autocomplete for every property of the filter.
If the inner array ist an array of objects you need to provide the inner field for the filter:
type MyAwesomeDto = {
...
someProperty: { someInnerProperty: string }[]
...
}
const queryBuilder = new OdataQueryBuilder<MyAwesomeDto>()
.filter({
field: 'someProperty',
operator: 'contains',
value: 'test',
lambdaOperator: 'any',
innerProperty: 'someInnerProperty', // <-- you will also get autocomplete for this property
ignoreCase: true,
})
.toQuery();
// ^ ?$filter=someProperty/any(s: contains(tolower(s/someInnerProperty), 'test'));
If you want to choose the filter logic or you need to filter for two depended fields there is the CombinedFilter:
const queryBuilder = new ODataQueryBuilder<MyAwesomeDto>
.filter({
logic: 'or',
filters: [
{ field: 'x', operator: 'eq', value: 'test' },
{ field: 'y', operator: 'eq', value: 5 },
],
})
.toQuery();
// ^ ?$filter=(x eq test or y eq 5)
You could combine it with multiple Combined filters or even with the single QueryFilter.
If you want to add a function to your codebase that encapsulates the query creation you can do it like this:
const item = {
x: 4,
y: 'test',
z: new Date(Date.now()),
};
const testFn = (
field: FilterFields<typeof item, string>, // you can use that type to get only the fields with type string
operator: FilterOperators<string>, // only allows filter operators for the given type
value: string, // you should use the type that you have defined in the FilterFields type
): string => {
const queryBuilder = new OdataQueryBuilder<typeof item>();
queryBuilder.filter({ field, operator, value });
return queryBuilder.toQuery();
};
const result = testFn('y', 'eq', 'test');
// ^ ?$filter=y eq 'test'
If you need to expand a property you can use the expand function:
const item = {
x: { someProperty: '' },
}
const queryBuilder = new OdataQueryBuilder<typeof item>();
.expand('x')
.toQuery();
// ^ ?expand=x
You can do this with inner properties as well:
const item = {
x: { someProperty: { nestedProperty: '' } },
}
const queryBuilder = new OdataQueryBuilder<typeof item>();
.expand('x/someProperty') // you will get autocomplete for these properties
.toQuery();
// ^ ?expand=x/someProperty
Any feature missing here? Please open an issue and add your feature request.
FAQs
odata builder for easier and typesafe usage
The npm package odata-builder receives a total of 144 weekly downloads. As such, odata-builder popularity was classified as not popular.
We found that odata-builder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.