README
AQS is a query parser library that provides advanced operations comparing to typical query parser libraries.
For example, you can use different operators additional to =
, or parse values from string to whatever you want and even define complex logic instead of just using and
operator for parameters!
Installation
npm install aqs
Usage
Basic
const { parse } = require('aqs');
const parsed = parse('id=3&age=22');
Use other operators
const { parse } = require('aqs');
const parsed = parse('id=3&age{gt}22'); // Greater than
Use negative operators
You can add n
prefix to operator names or not_
to full name of operators to make them negative.
const { parse } = require('aqs');
const parsed = parse('id=3&age{ne}22'); // Not equal
Use with config
const { parse } = require('aqs');
const parsed = parse('id=3&age=22', { paramsConfigs: { age: { defaultValue: 20 } } });
Use with custom logic
You can define logic of parameters in logic
query string, using parentheses with and
and or
operators.
Benefit of this feature is that you can allow client side that makes more complex queries instead of simple equal
queries.
const { parse } = require('aqs');
const parsed = parse('skill=coding&age=30&experience=8&logic=(and,skill,(or,age,experience))'); // skill==coding and (age==30 or experience==8)
Other features
There are some other features such as defining custom operators, parse values, ... that you can find them in examples folder.
List of operators
Operator | Full-name |
---|---|
e | equal |
i | in |
sw | startsWith |
ew | endsWith |
gt | greaterThan |
lt | lessThan |
ge | greaterOrEqual |
le | lessOrEqual |
b | between |
c | contains |
ic | includes |
l | like |
il | ilike |
r | regex |
Configurations
Option | Description | Default value | |
---|---|---|---|
paramsConfigs (Configs that can be set for each parameter) |
defaultValue | Default value of specific param | |
defaultOptions | Set default operator for specific param | ||
parser | Use a parser to parse value | ||
allowUnParseAble | If true, throws error if value is not parsable with the parser, Otherwise unparsed value will be used | false | |
alias | Use another key instead of passed key | ||
allowedOps | List of allowed operators to be used | all | |
mapOp | If false, short name of operator will be returned, Otherwise fullname of operator will be returned | true | |
globalConfigs (Configs that will be applied on all parameters) |
allowedParams | List of parameters that could be passed in query string | all |
excludedParams | List of parameters that could not be passed in query string | ||
parseArrays | Parse array values or not | true | |
parseJsons | Parse json values or not | true | |
throwOnIllegalOp | If true, throws error if an unknown operator passed | false | |
allowLogic | Allow logic in query string or not | true | |
throwWrongLogic | Throw error if incorrect logic passed | false | |
fixedParams (List of parameters that always will be returned) |
name | Name of parameter | |
value | Value of parameter | ||
op | Operator of parameter | ||
not | Negate operator or not |