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

dynamic-query-builder-client

v0.2.4

Published

Typescript client for Dynamic Query Builder

Downloads

112

Readme

Dynamic Query Builder Client

npm version CircleCI istanbul coverange

Dynamic query builder is able to build http query string for filtering, sorting, pagination operations. It works with DynamicQueryBuilder library.

NOTE: QueryBuilder is not able to perform http requests. It is only responsible to build query string.

NOTE: To perform http requests with query string, the builded query must be url encoded

Getting Started

All query building operations are done by QueryBuilder class. To create a new instance. Here's the constructor parameters;

export interface QueryBuilderParams {
  filters: Array<Filter>;
  sortBy?: Array<SortField>;
  pagination?: Pagination;
}

A full example would be the following;

import { QueryBuilder } from "@dynamic-query-builder";

export interface User {
  name: string;
  age: number;
}

const builder = new QueryBuilder({
  filters: [
    new NumericFilter({
      property: "age",
      value: 25,
      op: NumericFilterOperation.GreaterThan,
      logicalOperator: LogicalOperator.OrElse,
    }),
    new StringFilter({
      property: "name",
      value: "Y",
      op: StringFilterOperation.StartsWith,
    }),
  ],
  pagination: new Pagination({
    offset: 0,
    count: 10,
  }),
  sortBy: [
    new SortField({
      property: "name",
      by: SortDirection.DESC,
    }),
  ],
});

const query = builder.build();
// Query will hold the following string
// o=GreaterThan|OrElse&p=age&v=25&o=StartsWith&p=name&v=Y&offset=0&count=10&s=name,desc

// Give me 10 users sorted by name descending from offset 0
// whose age is greater than 25 AND
// whose names starts with Y

Filters

The possible filters in dynamic query builder is the following;

StringFilter

String Filter is able to make string filtering;

Possible Operations

export enum StringFilterOperation {
  In = "In",
  Equals = "Equals",
  Contains = "Contains",
  NotEqual = "NotEqual",
  EndsWith = "EndsWith",
  StartsWith = "StartsWith",
}

// example
const filter = new StringFilter({
  property: "name",
  op: StringFilterOperation.StartsWith,
  value: "s",
});

Numeric Filter

Numeric Filter is able to make numeric filtering;

Possible Operations

export enum NumericFilterOperation {
  In = "In",
  Equals = "Equals",
  NotEqual = "NotEqual",
  LessThan = "LessThan",
  LessThanOrEqual = "LessThanOrEqual",
  GreaterThan = "GreaterThan",
  GreaterThanOrEqual = "GreaterThanOrEqual",
}

// example
const filter = new NumericFilter({
  property: "age",
  op: NumericFilterOperation.GreaterThan,
  value: 25,
});

Boolean Filter

Boolean Filter is able to make boolean/option filtering;

Possible Operations

export enum BooleanFilterOperation {
  Equals = "Equals",
  NotEqual = "NotEqual",
}

// example
const filter = new BooleanFilter({
  property: "isPremium",
  op: BooleanFilterOperation.Equals,
  value: true,
});

Date Filter

Date Filter is able to make date filtering;

Possible Operations

export enum DateFilterOperation {
  Equals = "Equals",
  NotEqual = "NotEqual",
  GreaterThan = "GreaterThan",
  GreaterThanOrEqual = "GreaterThanOrEqual",
  LessThan = "LessThan",
  LessThanOrEqual = "LessThanOrEqual",
}

// example
const filter = new DateFilter({
  property: "birthDate",
  op: DateFilterOperation.GreaterThan,
  value: moment("12/25/1995", "MM-DD-YYYY"),
});

// NOTE: Moment library is required for dynamic query builder

Sorting

Sorting can be done by creating SortField instance;

const field = new SortField({
  property: "age",
  by: SortDirection.DESC,
});
// The default by parameter is Ascending

Pagination

Pagination can be done by creating Pagination instance;

const pagination = new Pagination({
  offset: 10, // default value is 0
  count: 25, // default value is 25
});

Best Practices

  • Always use QueryBuilder instance build() function for building query string.
  • Do not use filter.build(), sortfield.build() or pagination.build() separately