Socket
Socket
Sign inDemoInstall

@henry781/querysearch

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @henry781/querysearch

This library translates URL query params to a standard object including filter, sort, limit and offset.


Version published
Weekly downloads
61
increased by64.86%
Maintainers
1
Install size
15.0 kB
Created
Weekly downloads
 

Readme

Source

QuerySearch

This library translates URL query params to a standard object including filter, sort, limit and offset.

let's consider this request url:

http://api/planets?
    filter=star=sun&
    filter=type=terrestrial&
    filter=name[regex]=/^e/&
    sort=name=ASC&
    offset=0&
    limit=10

Express.js or some other frameworks will produce a queryParams object:

const queryparams = {
   filter: [ 'star=sun', 'type=terrestrial', name[regex]=/^e/' ],
   sort: ['name=ASC'],
   offset: 0,
   limit: 10
}

QuerySearch can parse each queryParam and returns:

const querySearch = QuerySearch.fromQueryParams(queryParams);
console.log(querySearch);
{
    filter: {
        star: { $eq: 'sun' },
        type: { $eq: 'terrestrial' },
        name: { $regex: /^e/ }
    },
    sort: {
        name: 1
    },
    offset: 0,
    limit: 10
}

It returns a clean search object you can use in your app!

Filter object is based on mongodb operators but there is no dependency with mongodb.

Filter

Simple filters

/planets?filter=star=sun&filter=type=terrestrial

will produce:

{
    star: { $eq: 'sun' },
    type: { $eq: 'terrestrial' }
}

Operators

/planets?filter=name[regex]=/^ea/

will produce:

{
    name: { $regex: /^ea/ }
}

Sort

/planets?sort=name=ASC

will produce:

{
    name: 1
}

Limit & Offset

/planets?limit=10&offset=20

will produce:

{
    limit: 10,
    offset: 20
}

FAQs

Last updated on 24 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc