@odata-filter/marshalers
v1.8.0
Published
Marshaler utilities for OData filter expressions, including a MongoDB query generator for MikroORM usage.
Maintainers
Readme
OData Filter Marshalers
Installation
See INSTALL.md for full instructions.
Usage
Package Installation
npm i @odata-filter/marshalers --saveBasic Example
import { tokenize, parse } from '@odata-filter/core';
import { toMongoJson } from '@odata-filter/marshalers';
const tokens = tokenize("country/name eq 'US' and age gte 21");
const ast = parse(tokens);
console.log(ast);
/*{
type: 'logical_operator',
value: 'and',
left: {
type: 'comparison_operator',
value: 'eq',
left: { type: 'field', value: 'country/name' },
right: { type: 'string_value', value: 'US' }
},
right: {
type: 'comparison_operator',
value: 'gte',
left: { type: 'field', value: 'age' },
right: { type: 'number_value', value: 21 }
}
}*/
toMongoJson(ast);
/*{
$and: [
{ 'country/name': { $eq: 'US' } },
{ age: { $gte: 21 } }
]
}*/SQL Example
import { tokenize, parse } from '@odata-filter/core';
import { toSqlWhere } from '@odata-filter/marshalers';
const tokens = tokenize("country/name eq 'US' and age gte 21");
const ast = parse(tokens);
toSqlWhere(ast);
// "(country/name = 'US' AND age >= 21)"trim() Example
import { tokenize, parse } from '@odata-filter/core';
import { toMongoJson, toSqlWhere } from '@odata-filter/marshalers';
const tokens = tokenize("trim(CompanyName) eq 'Alfreds Futterkiste'");
const ast = parse(tokens);
toMongoJson(ast);
/*{
$expr: {
$eq: [{ $trim: { input: '$CompanyName' } }, 'Alfreds Futterkiste']
}
}*/
toSqlWhere(ast);
// "(TRIM(CompanyName) = 'Alfreds Futterkiste')"endswith() Example
import { tokenize, parse } from '@odata-filter/core';
import { toMongoJson, toSqlWhere } from '@odata-filter/marshalers';
const tokens = tokenize("endswith(email, '@example.com')");
const ast = parse(tokens);
toMongoJson(ast);
// { email: { $regex: '@example\\.com$' } }
toSqlWhere(ast);
// "email LIKE '%@example.com'"Contributing
See CONTRIBUTING.md for full instructions.
License
See LICENSE for licensing information.
