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

query2query-js

v1.2.2

Published

A simple utility for converting a JSON representation of a query string into a SQL where clause.

Downloads

8

Readme

query2query-js

About

query2query-js is a simple utility for converting a JSON representation of a query string into a SQL where clause. The package is designed to be used with express and pg but may be useful in other contexts as well.

Installation

To install query2query-js simply run the following command: npm install --save query2query-js

NOTE: query2query-js is not the same package as query2query.

Usage

query2query-js expects two arguments when called. The first argument is the JSON representation of the query string you desire to parse. The second is a set of optional instructions on how to translate the values into SQL in the form of an object.

The Options Object

There are three fixed top level attributes available in the options object.

previusQuery

If provided query2query-js will start the generated query using AND instead of WHERE and will append it's results to the string supplied by the previusQuery attribute.

whiteList

If whiteList is a truthy value any query string key that does not have a coresponding key in the options object will be ignored when the query is created. Whitelist has a default value of true.

NOTE: Always use whiteList: true for anything besides debugging/development as it would leave you open to SQL injection attacks otherwise.

sort

This option is used to obtain the desired order of the generated query. If present sort should be an array of objects that have the following attributes:

| attribute | description | |-----------|-------------| | key | A column name on which sorting is allowed | | namespace | An optional namespace to scope the key under in the created sort statement. |

The column and direction of sort will be derived from reading the options sort and sortOrder from the query string object. The sort attribute of the query string object will be expected to be a string value that coresponds to one of the keys provided in the sort options array. sortOrder is expected to be one of two string options: ASC or DESC.

Search Params

All other attributes found in the options object will be assumed to be descriptions of allowed search criteria. The attribute keys should be the coresponding camel case value of a snake case column in your databasse schema. The value of the attribute should be an object with the following attributes:

| option | description | |--------|-------------| | namespace | A name space to scope the item under in the created query. | | type | The type of match desired. Options described below. |

Match Types

query2query-js provides several options to format what the segment of a query will look like. These options can be provided as the type attribute of the options object as described above.

| type | description | SQL | |------|-------------|-----| | string | A string ilike search. NOTE: default if no value is provided | some_item ILIKE $1 | | integer | An exact match for an integer. | id = $1 | | exact | An exact match for a string. | name = $1 | array | An array of string values. NOTE: does not currently work with integer values| name IN ($1,$2,$3,$4,...)

Example

const q2q = require('query2query-js')

const optionsObject = {
  whiteList: true,
  id: { type: 'integer', namespace: 'u' },
  userName: { type: 'string' },
  userEmail: { type: 'exact' },
  userHobbies: { type: 'array' },
  sort : [
    { key: 'id', namespace: 'u' },
    { key: 'userEmail' },
  ],
}

const queryString2 = {
  id: 1,
  userName: 'Bob',
  userEmail: '[email protected]',
  userHobbies: ['swimming', 'running', 'polka'],
  sort: 'id',
  sortOrder: 'ASC',
}

q2q(queryString, optionsObject)

//output
{
  text: 'WHERE u.id = $1 AND user_name ILIKE $2 AND user_email = $3 AND user_hobbies IN ($4,$5,$6) ORDER BY u.id ASC',
  values: [ 1, 'Bob%', '[email protected]', 'swimming', 'running', 'polka' ]
}