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

querier-ts

v0.0.0

Published

Query tool for analysing arrays of objects

Downloads

3

Readme

query-ts

A package that allows you to query data from an array of objects.

Introduction

The following data will be used in the samples of this documentation:

interface UserPermissions {
  useCookies: boolean;
  sendNotifications: boolean;
}

class User {
  id: number;
  name: string;
  permissions: UserPermissions;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;

  isAdmin(): boolean {
    ...
  }
}

const users: User[] = [
  ...
];

Query

A Query can be created this way:

const usersQuery = Query.from(users);

TypeScript will automatically assume that the query data type is User. You can make it explicit:

const usersQuery = Query.from<User>(users);

Getting results

all()

Returns all results.

first()

Returns the first result.

last()

Returns the last result.

count()

Returns the number of results.

exists()

Returns a boolean indicating whether any results exist.

Filtering data

where(condition)

There are two types of parameters:

  1. An object where each property represents an attribute to be validated.
  2. A callback function that returns a boolean validating the object.

When the condition is an object, you can pass literal values or callback functions to each attribute that needs to be validated.

const activeGmailUsers = Query.from(users)
  .where({
    isActive: true,
    email: (email) => email.endsWith('@gmail.com'),
  })
  .where((user) => (
    !user.isAdmin()
  ))
  .all();

It also works on inner objects:

  .where({
    permissions: {
      sendNotifications: true,
    },
  })

filterWhere(condition)

The difference of filterWhere is that it only accepts an object and it ignores conditions whose values are null or undefined.

let isActive: bool;

const filteredUsers = Query.from(users)
  .filterWhere({
    id: 1,
    isActive: isActive, // this condition will be skipped
  })
  .all();

Remember: if you want to check if a value is really null or undefined, use where().

Selecting specific data

select(columns)

This method can be combined with scalar(), column(), or values(). It determines which columns should be selected.

It accepts a string containing a single column name or an array of column names.

  .select('id')

scalar()

It returns the value of the first property of the first object.

const firstId = Query.from(users).scalar();

As mentioned above, it is possible to combine it with select() in order to get the value of another property.

const firstEmail = Query.from(users)
  .select('email')
  .scalar();

false is returned where there is no value.

column()

It returns the values of the first properties of all objects.

const ids = Query.from(users).column();

You can also use it with select():

const emails = Query.from(users)
  .select('email')
  .column();

values()

It returns the values of all objects as arrays.

const data = Query.from(users)
  .select(['id', 'email'])
  .values();

data would be something like this:

[
  [1, '[email protected]'],
  [2, '[email protected]']
]

Ordering results

orderBy(...columns)

Sorts the results. You can pass multiple arguments to it.

  .orderBy('name', 'id')

In the example above, name will have more priority than id.

It is also possible to apply descending order:

const lastId = Query.from(users)
  .select('id')
  .orderBy('-id')
  .scalar();

Limiting results

limit(limit)

This method can be used to set a limit of results.

  .limit(100)

Passing a float or a negative number will throw an InvalidArgumentError.

skip(numberOfRows)

Skips the first results.

  .skip(5)

Example:

const secondId = Query.from(users)
  .select('id')
  .skip(1) // skips the first user
  .scalar();

Passing a float or a negative number will throw an InvalidArgumentError.