@krishnadaspc/jsonquery
v1.3.6
Published
Library for Querying Javascript object in ORM manner.
Downloads
61
Readme
JSONQuery
A library for querying javascript objects in ORM manner supporting deep nested objects.
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
}
]