odata-filter-to-ast
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

odata-filter-to-ast

Simple OData query parser with zero dependencies.

npm version MIT license GitHub code size in bytes dependencies npm

Usage

$ npm install odata-filter-to-ast

Usage in code:

import {parseFilter, parseOrderBy} from 'odata-filter-to-ast';

parseFilter(`Name gt "Milk" or Price lt -2.55 or Size ne 3`);
parseOrderBy(`age,sum(height,width) desc`);

Result:

{
	type: 'OrExpr',
	left: {
		type: 'GtExpr',
		left: {
			type: 'MemberExpr',
			value: 'Name',
		},
		right: {
			type: 'Primitive',
			value: 'Milk'
		},
	},
	right: {
		type: 'OrExpr',
		left: {
			type: 'LtExpr',
			left: {
				type: 'MemberExpr',
				value: 'Price',
			},
			right: {
				type: 'Primitive',
				value: -2.55
			}
		},
		right: {
			type: 'NeExpr',
			left: {
				type: 'MemberExpr',
				value: 'Size',
			},
			right: {
				type: 'Primitive',
				value: 3
			}
		}
	}
}
[
	{
		type: 'OrderByItem',
		expr: {
			type: 'MemberExpr',
			value: 'age',
		},
		dir: 'asc',
	},
	{
		type: 'OrderByItem',
		expr: {
			type: 'FunctionExpr',
			name: 'sum',
			arguments: [
				{
					type: 'MemberExpr',
					value: 'height',
				},
				{
					type: 'MemberExpr',
					value: 'width',
				},
			],
		},
		dir: 'desc',
	},
]

Supported constructs

The following construct from OData specification are supported:

  • JS primitives -42, 3.14, 6.022e23, 'string', true, false, null
  • field identifiers iDenT_iFi3r
  • heterogeneous arrays ['a','r','r','a','y',7,false,null]
  • primitive relations eq, ne, gt, gt, lt, le
  • array relation in
  • boolean conjunctions and, or
  • operator priority grouping ( ... )
  • function calls includes(Name,'Joe')
  • sort directions asc, desc

Filters should be compatible with odata-filter-builder.

Did you find a string which could not be parsed? File an issue, please.

Related

Readme

Keywords

Package Sidebar

Install

npm i odata-filter-to-ast

Weekly Downloads

117

Version

2.0.0

License

MIT

Unpacked Size

168 kB

Total Files

8

Last publish

Collaborators

  • petrzjunior