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

rsql-builder

v1.3.0

Published

RSQL query builder

Downloads

3,717

Readme

rsql-builder Coverage Status

Here is the simple rsql-query builder utility. It's as minimal as possible but quite powerful at the same time.

import { and, cmp, eq, ge, inList, or } from 'rsql-builder'; // or const { and, cmp, eq, ge, inList, or } = require('rsql-builder')

const query = and(
  cmp('genres', inList('sci-fi', 'action', 'non fiction')),
  or(cmp('director', eq('Christopher Nolan')), cmp('actor', eq('*Bale'))),
  cmp('year', ge(2000))
); // 'genres=in=(sci-fi,action,"non fiction");(director=="Christopher Nolan",actor==*Bale);year>=2000'

Installation

npm install --save rsql-builder

Available methods

and(...comparisons): string

Create "and"-group of comparison.

Arguments

  • comparisons – list of comparisons, instances of Comparison-class or strings (for other comparison groups)

Example

import { and, cmp, eq, ge } from 'rsql-builder';

const op = and(cmp('year', ge(1980)), cmp('director', eq('Quentin Tarantino'))); // 'year>=1980;director=="Quentin Tarantino"

or(...comparisons): string

Create "or"-group of comparison.

Arguments

  • comparisons – list of comparisons, instances of Comparison-class or strings (for other comparison groups)

Example

import { cmp, eq, ge, or } from 'rsql-builder';

const op = or(cmp('year', ge(1980)), cmp('director', eq('Quentin Tarantino'))); // 'year>=1980,director=="Quentin Tarantino"

cmp(field, operation): Comparison or comparison(field, operation): Comparison

Create a new comparison for the field.

Arguments

  • field {string} – field name
  • operation {Operation} - operation

Example

import { cmp, eq } from 'rsql-builder';

const comp = cmp('field1', eq(200)); // 'field1==200'

eq(argument): Operation

Create "equal"-operation.

Arguments

  • argument – Any kind of value

Example

import { eq } from 'rsql-builder';

const op1 = eq(300); // '==300'
const op2 = eq('Taran*'); // '==Tarant*'
const op3 = eq('John Travolta'); // '=="John Travolta"'

ge(argument): Operation

Create greater-or-equal operation

Arguments

  • argument – Any kind of value

Example

import { ge } from 'rsql-builder';

const op1 = ge(300); // '>=300'
const op2 = ge('Taran*'); // '>=Tarant*'
const op3 = ge('John Travolta'); // '>="John Travolta"'

gt(argument): Operation

Create greater-than operation

Arguments

  • argument – Any kind of value

Example

import { gt } from 'rsql-builder';

const op1 = gt(300); // '>=300'
const op2 = gt('Taran*'); // '>=Tarant*'
const op3 = gt('John Travolta'); // '>="John Travolta"'

inList(...args): Operation

Create in-list operation

Arguments

  • args – List of any values

Example

import { inList } from 'rsql-builder';

const op = inList(300, 'Taran*', 'John Travolta'); // '=in=(300,Taran*,"John Travolta")'

le(argument): Operation

Create less-or-equal operation

Arguments

  • argument – Any kind of value

Example

import { le } from 'rsql-builder';

const op1 = le(300); // '<=300'
const op2 = le('Taran*'); // '<=Tarant*'
const op3 = le('John Travolta'); // '<="John Travolta"'

lt(argument): Operation

Create less-than operation

Arguments

  • argument – Any kind of value

Example

import { lt } from 'rsql-builder';

const op1 = lt(300); // '<300'
const op2 = lt('Taran*'); // '<Tarant*'
const op3 = lt('John Travolta'); // '<"John Travolta"'

ne(argument): Operation

Create not-equal operation

Arguments

  • argument – Any kind of value

Example

import { ne } from 'rsql-builder';

const op1 = ne(300); // '!=300'
const op2 = ne('Taran*'); // '!=Tarant*'
const op3 = ne('John Travolta'); // '!="John Travolta"'

outList(...args): Operation

Create out-list operation

Arguments

  • args – List of any values

Example

import { outList } from 'rsql-builder';

const op = outList(300, 'Taran*', 'John Travolta'); // '=out=(300,Taran*,"John Travolta")'

Custom operators

New operators can be easily created as follows:

import { Operation } from 'rsql-builder';

export function like(value) {
  return new Operation(escapeValue(value), '=like=');
}
import { like } from '../my-rsql-operators/like';

const op = like('John Travolta'); // '=like="John Travolta"'

It is recommended to use escapeValue function that handles special characters, however, you can omit it if you don't need it.

new Operation(escapeValue(value), '=like=');

A custom list operator could look like this:

function customListOperator(value: Argument[]): Operation {
  return new Operation(`(${escapeValue(value)})`, '=customListOperator=');
}