Typeorm Query Parser is simple url string parser for typeorm.
$ npm install typeorm-query-parser
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);
I recommend that you create file from parsed and specify all options and then export it
Basicly you just create instance of QueryBuilder and pass query object to .build() it will return you object ready for .find() in typeorm.
select what fields you want to get from database
example.com?select=field,field2
sort results from database by field
Each sorting condition must be seperated by: ;
example.com?sort=field,ASC;field2,DESC
example.com?sort=field;field2,DESC
If you dont provide order of sorting it will default to ASC
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.
limits the number of rows you get from database
example.com?limit=10
adds pagination functionality
example.com?limit=25&page=2
page number start from 1.
Enables or disables query result caching. See caching for more information and options
example.com?cache=true
default is false.
example.com?join=relation,relation2,relation.nested
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:
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);
{
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',
}
example.com?filter=id||!$eq||4;name||!$isnull||||$or||id||!$in||1,2,3
this will result: id!=4 && name!=null || id NOT in 1,2,3
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.