Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.
/ query-filters Public archive

Parse, extract and convert to ORM's options your query filters. Made for easy communication between client and server

Notifications You must be signed in to change notification settings

webshopr/query-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to query-filters 👋

Version Twitter: Spectaa_

Parse, extract and convert to ORM's options your query filters. Made for easy communication between client and server

Install

npm install

Run tests

npm run test

Usage

1) Encode (E.g: client-side)

Using stringifyParsedFilters with a valid filter object

let queryString = stringifyParsedFilters({
    "name": {
        "$eq": "Jon",
    },
    "lastname": {
        "$like": "doe"
    },
    "age": {
        "$gte": 18,
        "$lt": 65.5
    },
    "role": {
        "$in": [ "user", null, "%|@?&&__?./::|@#¼½^{[}] " ],
    },
    "banned": {
        "$not": true
    }
})

This object will be stringified as this:

name[$eq]=%40Jon&lastname[$like]=%40doe&age[$gte]=18&age[$lt]=65.5&role[$in][0]=%40user&role[$in][1]&role[$in][2]=%40%25%7C%40%3F%26%26__%20Test%20%3F.%2F%3A%3A%7C%40%23%C2%BC%C2%BD%5E%7B%5B%7B%7D%20&banned[$not]=true

If it's needed, it's fairly human readable:

name [$eq] = %40Jon
& lastname [$like] = %40doe
& age [$gte] = 18
& age [$lt] = 65.5
& role [$in] [0] = %40user
& role [$in] [1]
& role [$in] [2] = %40%25%7C%40%3F%26%26__%20Test%20%3F.%2F%3A%3A%7C%40%23%C2%BC%C2%BD%5E%7B%5B%7B%7D%20
& banned[$not]=true

> Only values are encoded ! Keys and operatos stay as is

Type Raw Encoded Rule
Booleans true, false true, false As is
Nulls null null As is
Numbers 10, 42.42, Infinity, ... 10, 42.42, Infinity, ... As is
Strings Hello World! %40Hello%20World%21 Encoded with and prefixed with an encoded @ (%40)

Why do strings are prefixed with an @?

Prefix strings prevents keyword like or number like string to be badly parsed:

Type check with prefix
Number
String
10
"%4010"
10
"10"
Boolean
String
true
"%40true"
true
"true"

2) Decode (E.g: server-side)

Decode the transmitted query string using parseQueryString(req.query.filters), query will be parsed back to the original object

Operators

Allowed operators
$eq equals ==
$lt lower than >
$lte lower than or equals >=
$gt greater than <
$gte greater than or equals <=
$in is contained in array of values IN as in SQL
$not different from !=
$like is like ILIKE as in SQL

Author

👤 Mathias Billot

🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page. You can also take a look at the pull requests.

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

About

Parse, extract and convert to ORM's options your query filters. Made for easy communication between client and server

Resources

Stars

Watchers

Forks