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

@truepic/queryql

v0.17.0

Published

Easily add filtering, sorting, and pagination to your REST API through your old friend: the query string!

Downloads

5,613

Readme

QueryQL

npm GitHub Actions piratepx

QueryQL makes it easy to add filtering, sorting, and pagination to your Node.js REST API through your old friend: the query string! Read our introductory article to learn more about why we wrote it and the problems it solves at Truepic.

QueryQL works with any Node.js web framework (be it Express, Koa, etc.), supports any query builder / ORM through adapters, and allows for custom validators so you can define validation in a familiar way.

Out of the box, QueryQL supports the following:

Installation

npm install @truepic/queryql

Getting Started

QueryQL takes a parsed query string (like Express' req.query) and translates it into the appropriate function calls that your query builder / ORM understands to filter, sort, and paginate the records.

(Make sure your framework uses a query string parser that supports nested objects. Node.js's native querystring module does not, but a package like qs does. It's usually a simple config change to switch.)

Let's consider an example to illustrate:

/images?filter[id][in][]=2&filter[id][in][]=3&filter[status]=open&sort=name&page[size]=10
{
  filter: {
    id: {
      in: [2, 3],
    },
    status: 'open',
  },
  sort: 'name',
  page: {
    size: 10,
  },
}

To support this query, QueryQL only requires you to define (whitelist) what's allowed through what we call a querier. Here's how one might look for the /images endpoint:

const QueryQL = require('@truepic/queryql')

class ImageQuerier extends QueryQL {
  defineSchema(schema) {
    schema.filter('id', 'in')
    schema.filter('status', '=')
    schema.sort('name')
    schema.page()
  }
}

With your querier defined, you can now call it in your router / controller. Here's how it might look in an Express route:

app.get('/images', async (req, res, next) => {
  const querier = new ImageQuerier(req.query, knex('images'))
  let images

  try {
    images = await querier.run()
  } catch (error) {
    // Handle validation error, such as by passing to an Express error handler:
    next(error)
  }

  res.send({ images })
})

Behind-the-scenes, QueryQL takes your initial query builder (knex('images')), and applies the following Knex chain when querier.run() is called:

builder
  .where('id', 'in', [2, 3])
  .where('status', '=', 'open')
  .orderBy('name', 'asc')
  .limit(10)
  .offset(0)

(Remember: While Knex is our default adapter and the query builder used in this example, adapters can be written for any query builder / ORM.)

This is a simple example, but hopefully it illustrates how easy it is to add filtering, sorting, and pagination to your REST API without manually touching your query builder / ORM.

Read the full documentation to learn how to add validation, customize the queries, and more.

Development

Prerequisites

The only prerequisite is a compatible version of Node.js (see engines.node in package.json).

Dependencies

Install dependencies with npm:

npm install

Tests

Jest is our testing framework of choice. We strive for 100% code coverage.

To run the tests:

npm test

During development, it's recommended to run the tests automatically on file change:

npm test -- --watch [--notify]

Code Style & Linting

Prettier is setup to enforce a consistent code style. It's highly recommended to add an integration to your editor that automatically formats on save.

ESLint is setup with the "recommended" rules to enforce a level of code quality. It's also highly recommended to add an integration to your editor that automatically formats on save.

To run via the command line:

npm run lint

Releasing

When the development branch is ready for release, Release It! is used to orchestrate the release process:

npm run release

Once the release process is complete, merge the development branch into the main branch, which should always reflect the latest release.