Skip to content

Latest commit

 

History

History
125 lines (109 loc) · 2.75 KB

README.md

File metadata and controls

125 lines (109 loc) · 2.75 KB

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