Steplix Query Filter
Steplix Query Filters is a module for parse filters from string to object.
Index
Download & Install
NPM
$ npm install steplix-query-filters
Source code
$ git clone https://github.com/steplix/SteplixQueryFilters.git$ cd SteplixQueryFilters$ npm install
How is it used?
Simple usage
const Parser = ;const parser = ; parser;// { "active": { "eq": "1" }, "description": { "li": "%casa" } } parser;// { "active": { "eq": "1" }, "description": { "li": "%casa", "eq": "depto" } }
Operators
Name | Example | Description |
---|---|---|
eq | id eq 1 | Check equality. id = 1 |
ne | name ne nico | Check inequality. name != 'nico' |
gt | id gt 1 | Check greater than. id > 1 |
ge | id ge 10 | Check greater than or equal. id >= 10 |
lt | id lt 1 | Check less than. id < 1 |
le | id le 10 | Check less than or equal. id <= 10 |
li | name li nico% | Check matches with nico*. name like nico% |
nl | name nl nico% | Check not matches with nico*. name not like nico% |
in | id in [1;2;3] | Check if included on [1,2,3]. id in (1,2,3) |
ni | id ni [1;2;3] | Check if not included on [1,2,3]. id not in (1,2,3) |
be | id be [1;10] | Check if it is between a and b. id between (1 and 10) |
nb | id nb [1;10] | Check if it is not between a and b. id not between (1 and 10) |
Configurations
Name | Type | Default | Description |
---|---|---|---|
separator | string | "," | Filter separator. |
key | string | "[A-Za-z0-9_]+" | String with RegExp format for match key on filters. |
value | string | ".+" | String with RegExp format for match value on filters. |
operators | array | ['eq','ne','gt','ge','lt','le','li','nl','in','ni','be','nb'] | Operators known to the parser. |
operatorPrefix | string | " " | Operator prefix in the string filter. |
operatorSuffix | string | " " | Operator suffix in the string filter. |
operatorFlags | string | "i" | Operator regexp flag. |
mapOperator | object or function | null | Mapper used to replace operators. |
mapValue | function | null | Mapper used to replace values. |
mapValueFormat | function | null | Mapper used to replace values only on format method. |
mapValueParse | function | null | Mapper used to replace values only on parse method. |
mapKey | object or function | null | Mapper used to replace keys. |
mapKeyFormat | object or function | null | Mapper used to replace keys only on format method. |
mapKeyParse | object or function | null | Mapper used to replace keys only on parse method. |
separatorGroups | string | ";" | Filter group separator. Example "id in [1;2;3]" |
Configuration examples
const parser = separator: '---'; parser;// { "active": { "eq": "1" }, "description": { "li": "%casa" } }
const parser = operators: Parserdefaultsoperators; parser;// { "active": { "eq": "1" }, "description": { "my-operator": "casa" } }
Configuration mapper
Inspired to use in combination with steplix-database
const Parser Mappers = ;const parser = mapper: MappersSQL; parser;// { "active": { "=": "1" }, "description": { "LIKE": "%casa" } }
Complete example with steplix-database
const Database Query = ;const Parser Mappers = ;const db = host: 'localhost' user: 'myuser' password: 'mypass' database: 'mydbname';const parser = mapper: MappersSQL; const where = parser;const query = Query;// "SELECT * FROM users WHERE active = '1' AND description LIKE '%nicolas'" db;// array<user models>
Format
const parser = ; parser;// "active eq 1,description li %casa"
Tests
In order to see more concrete examples, I INVITE YOU TO LOOK AT THE TESTS :)
Run the unit tests
npm installnpm test