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

@krishnadaspc/jsonquery

v1.3.6

Published

Library for Querying Javascript object in ORM manner.

Downloads

76

Readme

JSONQuery

A library for querying javascript objects in ORM manner supporting deep nested objects.

npm Version TypeScript

Installation

npm i @krishnadaspc/jsonquery

overview of all available functions

most methods can be chained and to get the final result call the get() method at the end.

Currently available methods

  • get()
  • select(columns: Array | [''])*
  • limit(limit: Number=0, offset: Number=0)
  • where(column: keyof DataType, condition: "==" | "<" | ">" | "<=" | ">=" | "!=", value: any)
  • orderBy(column: keyof DataType, sortOrder: "asc" | "desc")
  • distinct(column: keyof DataType)
  • fetchOnly(column: keyof DataType) This is not chainable as it returns a single column values as plain array
  • in(column: keyof DataType, values:Array)
  • notIn(column: keyof DataType, values:Array)
  • between(column: keyof DataType, startValue: any, endValue: any)
  • nestedWhere(column: string, condition: ComparisonOperator, value: any)
import {JSONQuery} from "@krishnadaspc/JSONQuery"

var data = {
    people: [
        { name: 'Matt', country: 'NZ', age: 34 },
        { name: 'Pete', country: 'AU', age: 20 },
        { name: 'Mikey', country: 'NZ', age: 31 },
        { name: 'Kevin', country: 'AU', age: 40 },
        { name: 'Joseph', country: 'AU', age: 43 },

    ]
}

const qObj = new JSONQuery(data.people)

 qObj
    .select(['name', 'age', "country"])
    .where("age", ">", 30)
    .orderBy("age", "asc")
    .limit(5)
    .get()

Examples of available functions

import {JSONQuery} from "@krishnadaspc/JSONQuery"

var data = {
    people: [
        { name: 'Matt', country: 'NZ', age: 34 },
        { name: 'Pete', country: 'AU', age: 20 },
        { name: 'Mikey', country: 'NZ', age: 31 },
        { name: 'Kevin', country: 'AU', age: 40 },
        { name: 'Joseph', country: 'AU', age: 43 },

    ]
}

const qObj = new JSONQuery(data.people)

1. Selecting selected columns

qObj
  .select(['name', 'age'])
  .get()

2. Selecting all columns

qObj
  .select(['*'])
  .get()

1. using single where condition

import {JSONQuery} from "./index"
const data = {
    people: [
        { name: 'Matt', country: 'NZ', age: 34 },
        { name: 'Pete', country: 'AU', age: 20 },
        { name: 'Mikey', country: 'NZ', age: 31 },
        { name: 'Kevin', country: 'AU', age: 40 },
        { name: 'Joseph', country: 'AU', age: 43 },

    ]
}

const qObj = new JSONQuery(data.people)
console.log(
    qObj
    .select(['name', 'age'])
    .where("age", ">", 30)
    .get()
)

/**output
[
  { name: 'Matt', age: 34 },
  { name: 'Mikey', age: 31 },
  { name: 'Kevin', age: 40 },
  { name: 'Joseph', age: 43 }
]
*/

2. using multiple where condition

const data = {
    people: [
        { name: 'Matt', country: 'NZ', age: 34 },
        { name: 'Pete', country: 'AU', age: 20 },
        { name: 'Mikey', country: 'NZ', age: 31 },
        { name: 'Kevin', country: 'AU', age: 40 },
        { name: 'Joseph', country: 'AU', age: 43 },

    ]
}

const qObj = new JSONQuery(data.people)
console.log(
    qObj
    .select(['name', 'age', "country"])
    .where("age", ">", 30)
    .where("country", "==", "AU")
    .get()
)
/**output
[
  { name: 'Kevin', age: 40, country: 'AU' },
  { name: 'Joseph', age: 43, country: 'AU' }
]
*/

Querying deep nested objects

const people = [
    {
        "fullName": "Bessie Smith",
        "address": {
            "street": "27640 Glen Burgs",
            "city": "Brades"
        },
        "email": "[email protected]",
        "phone": {
            "home": "600-933-0626",
            "office": "774-586-4275"
        },
        "jobInfo": {
            "basic": 10384,
            "department": "Kids"
        }
    },
    {
        "fullName": "Maria Russel",
        "address": {
            "street": "8217 Krajcik Throughway",
            "city": "Lospalos"
        },
        "email": "[email protected]",
        "phone": {
            "home": "212-868-6900",
            "office": "061-060-8362"
        },
        "jobInfo": {
            "basic": 2385,
            "department": "Beauty"
        }
    },
    {
        "fullName": "Jonathan Koss",
        "address": {
            "street": "52449 Elinore Oval",
            "city": "Becicherecu Mic"
        },
        "email": "[email protected]",
        "phone": {
            "home": "595-125-5573",
            "office": "361-043-8944"
        },
        "jobInfo": {
            "basic": 3423,
            "department": "Sports"
        }
    },
    {
        "fullName": "Ivan McDermott",
        "address": {
            "street": "632 Gabriella Corners",
            "city": "Bajadero"
        },
        "email": "[email protected]",
        "phone": {
            "home": "474-381-5468",
            "office": "113-620-8681"
        },
        "jobInfo": {
            "basic": 2329,
            "department": "Sports"
        }
    },
    {
        "fullName": "Wilbert Schmitt",
        "address": {
            "street": "7524 Eliezer Village",
            "city": "Hongseong"
        },
        "email": "[email protected]",
        "phone": {
            "home": "239-644-0940",
            "office": "826-517-5449"
        },
        "jobInfo": {
            "basic": 21204,
            "department": "Kids"
        }
    },
    {
        "fullName": "Teri Schamberger",
        "address": {
            "street": "72716 Marquise Falls",
            "city": "Cruz Bay"
        },
        "email": "[email protected]",
        "phone": {
            "home": "477-684-6061",
            "office": "547-193-1703"
        },
        "jobInfo": {
            "basic": 1146,
            "department": "Garden"
        }
    },
    {
        "fullName": "Catherine Watsica",
        "address": {
            "street": "42987 Greenholt Neck",
            "city": "Barber"
        },
        "email": "[email protected]",
        "phone": {
            "home": "318-025-1780",
            "office": "799-976-0109"
        },
        "jobInfo": {
            "basic": 10103,
            "department": "Automotive"
        }
    },
    {
        "fullName": "Leslie Schmitt",
        "address": {
            "street": "82783 Leannon Hills",
            "city": "Burao"
        },
        "email": "[email protected]",
        "phone": {
            "home": "570-107-6840",
            "office": "363-354-5809"
        },
        "jobInfo": {
            "basic": 3753,
            "department": "Toys"
        }
    }
]
const qObj = new JSONQuery(people)
console.dir(
    qObj
    .select(['fullName', 'address', 'email', 'phone', 'jobInfo'])
    .nestedWhere("jobInfo.department", "==", "Sports")
    .orderBy("fullName", "desc")
    .get()
)
/** output
 [
  {
    fullName: 'Jonathan Koss',
    address: { street: '52449 Elinore Oval', city: 'Becicherecu Mic' },
    email: '[email protected]',
    phone: { home: '595-125-5573', office: '361-043-8944' },
    jobInfo: { basic: 3423, department: 'Sports' }
  },
  {
    fullName: 'Ivan McDermott',
    address: { street: '632 Gabriella Corners', city: 'Bajadero' },
    email: '[email protected]',
    phone: { home: '474-381-5468', office: '113-620-8681' },
    jobInfo: { basic: 2329, department: 'Sports' }
  }
]
 */

Typescript Example fetching from live dummyjson api using axios

import { JSONQuery } from "@krishnadaspc/JSONQuery"
import axios from "axios"

interface IProduct {
    id: Number,
    title: string,
    price: Number,
    rating: Number
}

axios.get('https://dummyjson.com/products')
    .then(response => {
        const qObj = new JSONQuery<IProduct>(response.data.products)
        const filteredProducts = qObj
            .select(['id', "title", 'price', "rating"])
            .where("price", ">", 100)
            .orderBy("rating", "desc")
            .limit(5)
            .get()
        console.log(filteredProducts)
    })
    .catch(error => {
        console.log(error);
    });

Output of filteredProducts will be

[
  { id: 1, title: 'iPhone 9', price: 549, rating: 4.69 },
  { id: 6, title: 'MacBook Pro', price: 1749, rating: 4.57 },
  { id: 9, title: 'Infinix INBOOK', price: 1099, rating: 4.54 },
  { id: 2, title: 'iPhone X', price: 899, rating: 4.44 },
  {
    id: 8,
    title: 'Microsoft Surface Laptop 4',
    price: 1499,
    rating: 4.43
  }
]