Url query parser for typeorm
npm install typeorm-query-parsercreated by: Mladen Skrbic
Typeorm Query Parser is simple url string parser for typeorm.
``sh`
$ npm install typeorm-query-parser
`javascript
import { QueryBuilder } from 'typeorm-query-parser';
const query = req.query;
const options = {};
const parser = new QueryBuilder(options);
const parsedQuery = parser.build(query);
EntityRepository.find(parsedQuery);
`
#### Select
select what fields you want to get from database
`example.com?select=field,field2`
#### Sort
sort results from database by field
Each sorting condition must be seperated by: `;``example.com?sort=field,ASC;field2,DESC``
##### note: example.com?sort=field;field2,DESC`
If you dont provide order of sorting it will default to ASC
#### Filter
here you specify conditions (where) of data you request
`example.com?filter=id||$eq||4;name||$isnull||`
Conditions separated by `;` will create `AND` statement.`
You can also use ||$or||` and create `OR` statement and you can also combine them.
`example.com?filter=id||$eq||4;name||$isnull||||$or||id||$in||1,2,3`
This is an example of combining both OR and AND in request.
#### Limit
limits the number of rows you get from database
`example.com?limit=10``
##### note: Default is 25, you can change default in options.
#### Page
adds pagination functionalityexample.com?limit=25&page=2`
page number start from 1.
#### Cache
Enables or disables query result caching. See caching for more information and options
`example.com?cache=true`
default is false.
#### Join
`example.com?join=relation,relation2,relation.nested`
javascript
const options={
LOOKUP_DELIMITER:'||',
CONDITION_DELIMITER:';',
VALUE_DELIMITER:',',
EXACT: '$eq',
NOT: '!',
CONTAINS: '$cont',
IS_NULL: '$isnull',
GT: '$gt',
GTE: '$gte',
LT: '$lt',
LTE: '$lte',
STARTS_WITH: '$starts',
ENDS_WITH: '$ends',
IN: '$in',
BETWEEN: '$between',
OR: '$or',
DEFAULT_LIMIT:'25'
}
`
you can change anything you want by passing options object to QueryBuilder constructor
example:
`javascript
import { QueryBuilder } from 'typeorm-query-parser';const query = req.query;
const options = {
DEFAULT_LIMIT: '15'
};
const parser = new QueryBuilder(options);
const parsedQuery = parser.build(query);
`
##### note: `VALUE_DELIMITER, CONDITION_DELIMITER, LOOKUP_DELIMITER` can NOT be the same.Operators
`javascript
{
EXACT: '$eq',
CONTAINS: '$cont',
IS_NULL: '$isnull',
GT: '$gt',
GTE: '$gte',
LT: '$lt',
LTE: '$lte',
STARTS_WITH: '$starts',
ENDS_WITH: '$ends',
IN: '$in',
BETWEEN: '$between',
}
`
##### you can change operators in options.
###### you can negate every operator by puting `!` before it.
##### Negating example
`example.com?filter=id||!$eq||4;name||!$isnull||||$or||id||!$in||1,2,3``Please make sure to update tests as appropriate.