@luolapeikko/odata-query-select
v0.0.1
Published
OData $select string parser and evaluator
Readme
@luolapeikko/odata-query-select
Builds a data selector based on $select query values.
Parses OData $select values into an AST and applies field projection to plain JavaScript/TypeScript objects.
Supports top-level and nested property selection (for example name,address/city), wildcard selection (*), and duplicate item normalization.
Usage examples
import {createODataSelectPicker} from '@luolapeikko/odata-query-select';
interface Person {
name: string;
age: number;
address?: {city?: string; zip?: string};
}
const personList: Person[] = [
{name: 'John', age: 30, address: {city: 'Seattle', zip: '98101'}},
{name: 'Mary', age: 25, address: {city: 'Portland', zip: '97201'}},
];
const selectPicker = createODataSelectPicker<Person>('name,address/city');
console.log(personList.map(selectPicker));
// [{name: 'John', address: {city: 'Seattle'}}, {name: 'Mary', address: {city: 'Portland'}}]import {applyODataSelect} from '@luolapeikko/odata-query-select';
const handleRequest = (req, res, next) => {
const dataObject = await getSomeData();
const querySelect = req.query.$select;
if (typeof querySelect === 'string') {
const selectedData = applyODataSelect(querySelect, dataObject);
res.json(selectedData);
}
};