aqs

parse query strings with advanced features

Usage no npm install needed!

<script type="module">
  import aqs from 'https://cdn.skypack.dev/aqs';
</script>

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)
defaultValueDefault value of specific param
defaultOptionsSet default operator for specific param
parserUse a parser to parse value
allowUnParseAbleIf true, throws error if value is not parsable with the parser, Otherwise unparsed value will be usedfalse
aliasUse another key instead of passed key
allowedOpsList of allowed operators to be usedall
mapOpIf false, short name of operator will be returned, Otherwise fullname of operator will be returnedtrue
globalConfigs
(Configs that will be applied on all parameters)
allowedParamsList of parameters that could be passed in query stringall
excludedParamsList of parameters that could not be passed in query string
parseArraysParse array values or nottrue
parseJsonsParse json values or nottrue
throwOnIllegalOpIf true, throws error if an unknown operator passedfalse
allowLogicAllow logic in query string or nottrue
throwWrongLogicThrow error if incorrect logic passedfalse
fixedParams
(List of parameters that always will be returned)
nameName of parameter
valueValue of parameter
opOperator of parameter
notNegate operator or not