blow-query
v0.1.11
Published
Programmatically build queries which can be returned as JSON Object and used to fetch data from many sources.
Downloads
7
Maintainers
Readme
blow-query
Query object allows to to programmatically build queries which can be returned as JSON Object and used to fetch data from database, api endpoint etc.
import {Query} from 'blow-query';
const query = new Query();
query
.greaterThanOrEqual('age', 21)
.equal('countryCode', 'POL')
.ascending('age')
.limit(10);
console.log(query.toJSON()); // {where: {age: {$gte: 21}, countryCode: 'POL'}, sort: {age: 1}, limit: 10}
Query class
class Query {
equal(field: string, value: any): Query;
notEqual(field: string, value: any): Query;
lessThan(field: string, value: any): Query;
lessThanOrEqual(field: string, value: any): Query;
greaterThan(field: string, value: any): Query;
greaterThanOrEqual(field: string, value: any): Query;
containedIn(field: string, values: any[]): Query;
notContainedIn(field: string, values: any[]): Query;
regex(field: string, value: RegExp): Query;
contains(field: string, value: string): Query;
startsWith(field: string, value: string): Query;
endsWith(field: string, value: string): Query;
ascending(field: string): Query;
descending(field: string): Query;
skip(skip: number): Query;
limit(limit: number): Query;
select(fields: string[] | string): Query;
or(query: Query): Query;
toJSON(): QueryObject;
}
Interfaces
export interface QueryWhere {
[key: string]: any;
}
export interface QuerySort {
[key: string]: number;
}
export interface QueryObject {
where?: QueryWhere;
limit?: number;
skip?: number;
sort?: QuerySort;
select?: string[];
}