type QueryTypesSingle = string | number | boolean; export type QueryTypesList = string[] | number[] | boolean[] | Query[]; export type QueryTypes = QueryTypesSingle | QueryTypesList; type AttributesTypes = string | string[]; export class Query { method: string; attribute: AttributesTypes | undefined; values: QueryTypesList | undefined; constructor( method: string, attribute?: AttributesTypes, values?: QueryTypes ) { this.method = method; this.attribute = attribute; if (values !== undefined) { if (Array.isArray(values)) { this.values = values; } else { this.values = [values] as QueryTypesList; } } } toString(): string { return JSON.stringify({ method: this.method, attribute: this.attribute, values: this.values, }); } static equal = (attribute: string, value: QueryTypes): Query => new Query("equal", attribute, value); static notEqual = (attribute: string, value: QueryTypes): Query => new Query("notEqual", attribute, value); static lessThan = (attribute: string, value: QueryTypes): Query => new Query("lessThan", attribute, value); static lessThanEqual = (attribute: string, value: QueryTypes): Query => new Query("lessThanEqual", attribute, value); static greaterThan = (attribute: string, value: QueryTypes): Query => new Query("greaterThan", attribute, value); static greaterThanEqual = (attribute: string, value: QueryTypes): Query => new Query("greaterThanEqual", attribute, value); static isNull = (attribute: string): Query => new Query("isNull", attribute); static isNotNull = (attribute: string): Query => new Query("isNotNull", attribute); static between = ( attribute: string, start: string | number, end: string | number ) => new Query("between", attribute, [start, end] as QueryTypesList); static startsWith = (attribute: string, value: string): Query => new Query("startsWith", attribute, value); static endsWith = (attribute: string, value: string): Query => new Query("endsWith", attribute, value); static select = (attributes: string[]): Query => new Query("select", undefined, attributes); static search = (attribute: string, value: string): Query => new Query("search", attribute, value); static orderDesc = (attribute: string): Query => new Query("orderDesc", attribute); static orderAsc = (attribute: string): Query => new Query("orderAsc", attribute); static cursorAfter = (documentId: string): Query => new Query("cursorAfter", undefined, documentId); static cursorBefore = (documentId: string): Query => new Query("cursorBefore", undefined, documentId); static limit = (limit: number): Query => new Query("limit", undefined, limit); static offset = (offset: number): Query => new Query("offset", undefined, offset); static contains = (attribute: string, value: string | string[]): Query => new Query("contains", attribute, value); static or = (...queries: Query[]) => { return new Query( "or", undefined, queries ); }; static and = (...queries: Query[]) => { return new Query( "and", undefined, queries ); }; }