npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-query-builder

v2.1.0

Published

The hapi-query-builder hapi.js to get all query from url and create mongodb query.

Downloads

303

Readme

Universal query builder for Hapi.js

Hapi-query-builder is a Hapi.js plugin. It is an ambitious attempt to create a kind of "URL" to "Mongoose/mongodb query" translator which provides a mongoose/mongodb query for GET API. It provides facilities to the developer to pass query params in URL and get the mongoos/mongodb query.

The primary benefits of this plugin (currently) are:

  • Ability to convert directory query params to mongoose/mongodb get query.
  • Supports all get query command of mongoose/mongodb (like- pagination and populate)
  • Automatically identify where condition and option from query params.
  • You can access mongoose/mongodb query in request parameter with name "parsedQuery". It is object witch hold where and option keys.
  • For better experience use "mongoose-paginate-v2" npm package.

Installation

You can add the module to your Hapi using npm:

 npm i hapi-query-builder --save

Compatibility

node: >= 18.x.x,
hapi: >= 21.x.x

Quick start

In your Hapi apps server object simply add hapi-query-builder and also add the routes for your API as described on the hapijs.com site.

const Hapi = require('@hapi/hapi');

(async () => {
    try{
        const server = await new Hapi.Server({
            host: 'localhost',
            port: 3000,
        });

        await server.register({
            plugin: require('hapi-query-builder'),
            options: {
                defaultSelectField: '_id', // (optional)- Pass field name for default select if $select is empty
            },
        });

        server.route({
            method: 'GET',
            path: '/query',
            handler: function (request, h) {
                return {
                    query: request.parsedQuery,
                };
            },
        });

        await server.start();
        console.log('Server running at:', server.info.uri);
    } catch (err) {
        console.error(err);
        process.exit(1);
    }
})();

Example

You just pass query in query params form Front-end in request.

const axios = require('axios');

const response = axios.get('/[email protected]&$limit=10&$skip=1&$sort=updatedAt|-1');

You can get mongoose query in handler in hapi.js

 handler: function (request, h) {
   query: request.parsedQuery,
 };


// example response
{"where":{"email":"[email protected]"},"options":{"lean":true,"offset":1,"limit":10,"sort":{"updatedAt":"-1"}}}

Documentation

Where

Passing DB field name (Get all record where field match with value)

- GET       /api-path?field1=value&field2=value&.....fieldn=value
- Ex        /user?_id=5ff48a66070a2466418e6adc&[email protected]
- Result    {"where":{"_id":"5ff48a66070a2466418e6adc","email":"[email protected]"},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

Or operator

Passing DB field name ($or condition for mongodb)

- GET       /api-path?$or=field1|value,field2|value,.....,fieldn|value
- Ex        /user?$or=_id|5ff48a66070a2466418e6adc,email|[email protected]
- Result    {"where":{"$or":[{"_id":"5ff48a66070a2466418e6adc"},{"email":"[email protected]"}],"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

Select field value

Passing DB field name with space in $select

- GET       /api-path?$select=field1 field2 field3..... fieldn
- Ex        /user?$select=_id email title name
- Result    {"where":{},"options":{"select":"_id email title name","lean":true,"offset":0,"limit":20,"sort":{}}}

Search query($q) (in given fields)

Passing search text with DB fields name with pip operator in $q

- GET       /api-path?$q=text|field1,field2....fieldn
- Ex        /user?$q=amjesh|email,name
- Result    {{"where":{"$or":[{"email":{"$regex":"amjesh","$options":"i"}},{"name":{"$regex":"amjesh","$options":"i"}}]},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

Search with case sensitive

Passing DB field name and search value with pip operator in $search

- GET       /api-path?$search=field|value,field1|value,....fieldn|value
- Ex        /user?$search=name|Amjesh,email|[email protected]
- Result    {"where":{"name":{"$regex":/Amjesh/}},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

Search without case sensitive

Passing DB field name and search value with pip operator in $isearch

- GET       /api-path?$isearch=field|value,field1|value,...fieldn|value
- Ex-       /user?$isearch=name|Amjesh,email|[email protected]
- Result    {"where":{"name":{"$regex":/amjesh/,"$options":"i"}},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

Populate

Passing modal with comma separated in $populate

- GET       /api-path?$populate=modal1,modal2,....modaln
- Ex        /user?$populate=post,comment
- Result    {"where":{},"options":{"populate":["post","comment"],"lean":true,"offset":0,"limit":20,"sort":{}}}

Sort

Passing DB field name for shorting in $sort
For Ascending pass value 1 or asc and
For Descending pass value -1 or desc any one

- GET       /api-path?$sort=field1|1,field2|1
- Ex        /user?$sort=updatedAt|1,_id|1
- Result    {"where":{},"options":{"lean":true,"offset":0,"limit":20,"sort":{"updatedAt":"1","_id":"1"}}}
            OR
- GET       /api-path?$sort=field1|-1,field2|-1
- Ex        /user?$sort=updatedAt|-1,_id|-1
- Result    {"where":{},"options":{"lean":true,"offset":0,"limit":20,"sort":{"updatedAt":"-1","_id":"-1"}}}

Skip(optional)

Skip record for pagination in $skip(By default it's 0)

- GET       /api-path?$skip=value
- Ex        /user?$skip=1
- Result    {"where":{},"options":{"lean":true,"offset":1,"limit":20,"sort":{}}}

Limit(optional)

Passing limit for total number of record for pagination in $limit.
You can pass defaultLimit in plugin option

- GET       /api-path?$limit=value
- Ex        /user?$limit=100
- Result    {"where":{},"options":{"lean":true,"offset":0,"limit":100,"sort":{}}}

IN or NOT IN

Get all records where the value does $in(In) or does not $nin(Not-in) match from values

- GET       /api-path?field[$in]=value1,value2.....valuen
- Ex        /user?id[$in]=10,11,12,13,14
- Result    {"where":{"id":{"$in":["10","11","12","13","14"]}},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

GT Or GTE

Get all records where the value does $gt(Greater then) or does not $gte(Greater then equal to) match from value

- GET       /api-path?field[$gt]=value
- Ex        /user?comment[$gt]=100
- Result    {"where":{"comment":{"$gt":"100"}},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

LT or LTE

Get all records where the value does $lt(Less then) or does not $lte(Less then equal to) match from value

- GET       /api-path?field[$lt]=value
- Ex        /user?likes[$lt]=100
- Result    {"where":{"likes":{"$lt":"100"}},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}

NE

Get all records where the value does $ne(not equal to) match from value

- GET       /api-path?field[$ne]=value
- Ex        /user?archive[$ne]=true
- Result    {"where":{"archive":{"$ne":true}},"options":{"lean":true,"offset":0,"limit":20,"sort":{}}}