0.0.13 • Published 4 years ago

steplix-query-filters v0.0.13

Weekly downloads
33
License
ISC
Repository
github
Last release
4 years ago

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 } = require('steplix-query-filters');
const parser = new Parser();

parser.parse('active eq 1,description li %casa');
// { "active": { "eq": "1" }, "description": { "li": "%casa" } }

parser.parse('active eq 1,description li %casa,description eq depto');
// { "active": { "eq": "1" }, "description": { "li": "%casa", "eq": "depto" } }

Operators

NameExampleDescription
eqid eq 1Check equality. id = 1
nename ne nicoCheck inequality. name != 'nico'
gtid gt 1Check greater than. id > 1
geid ge 10Check greater than or equal. id >= 10
ltid lt 1Check less than. id < 1
leid le 10Check less than or equal. id <= 10
liname li nico%Check matches with nico*. name like nico%
nlname nl nico%Check not matches with nico*. name not like nico%
inid in 1;2;3Check if included on 1,2,3. id in (1,2,3)
niid ni 1;2;3Check if not included on 1,2,3. id not in (1,2,3)
beid be 1;10Check if it is between a and b. id between (1 and 10)
nbid nb 1;10Check if it is not between a and b. id not between (1 and 10)

Configurations

NameTypeDefaultDescription
separatorstring","Filter separator.
keystring"A-Za-z0-9_+"String with RegExp format for match key on filters.
valuestring".+"String with RegExp format for match value on filters.
operatorsarray'eq','ne','gt','ge','lt','le','li','nl','in','ni','be','nb'Operators known to the parser.
operatorPrefixstring" "Operator prefix in the string filter.
operatorSuffixstring" "Operator suffix in the string filter.
operatorFlagsstring"i"Operator regexp flag.
mapOperatorobject or functionnullMapper used to replace operators.
mapValuefunctionnullMapper used to replace values.
mapValueFormatfunctionnullMapper used to replace values only on format method.
mapValueParsefunctionnullMapper used to replace values only on parse method.
mapKeyobject or functionnullMapper used to replace keys.
mapKeyFormatobject or functionnullMapper used to replace keys only on format method.
mapKeyParseobject or functionnullMapper used to replace keys only on parse method.
separatorGroupsstring";"Filter group separator. Example "id in 1;2;3"

Configuration examples

const parser = new Parser({
  separator: '---'
});

parser.parse('active eq 1---description li %casa');
// { "active": { "eq": "1" }, "description": { "li": "%casa" } }
const parser = new Parser({
  operators: Parser.defaults.operators.concat(['my-operator'])
});

parser.parse('active eq 1,description my-operator casa');
// { "active": { "eq": "1" }, "description": { "my-operator": "casa" } }

Configuration mapper

Inspired to use in combination with steplix-database

const { Parser, Mappers } = require('steplix-query-filters');
const parser = new Parser({
  mapper: Mappers.SQL
});

parser.parse('active eq 1,description li %casa');
// { "active": { "=": "1" }, "description": { "LIKE": "%casa" } }

Complete example with steplix-database

const { Database, Query } = require('steplix-database');
const { Parser, Mappers } = require('steplix-query-filters');
const db = new Database({
  host: 'localhost',
  user: 'myuser',
  password: 'mypass',
  database: 'mydbname'
});
const parser = new Parser({
  mapper: Mappers.SQL
});

const where = parser.parse('active eq 1,description li %nicolas');
const query = Query.select('users', { where });
// "SELECT * FROM users WHERE active = '1' AND description LIKE '%nicolas'"

db.query(query);
// array<user models>

Format

const parser = new Parser();

parser.format({
  active: {
    eq: "1"
  },
  description: {
    li: "%casa"
  }
});
// "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 install
npm test
0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago