@mas-soft/mas-core-server
Version:
main application
88 lines (87 loc) • 3.42 kB
TypeScript
import { AnyObject } from './common-types';
export declare type Operators = 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'inq' | 'nin' | 'between' | 'exists' | 'and' | 'or' | 'like' | 'nlike' | 'ilike' | 'nilike' | 'regexp';
export declare type PredicateComparison<PT> = {
eq?: PT;
neq?: PT;
gt?: PT;
gte?: PT;
lt?: PT;
lte?: PT;
inq?: PT[];
nin?: PT[];
between?: [PT, PT];
exists?: boolean;
like?: PT;
nlike?: PT;
ilike?: PT;
nilike?: PT;
regexp?: string | RegExp;
};
export declare type ShortHandEqualType = string | number | boolean | Date;
export declare type KeyOf<MT extends object> = Exclude<Extract<keyof MT, string>, Operators>;
export declare type Condition<MT extends object> = {
[P in KeyOf<MT>]?: PredicateComparison<MT[P]> | (MT[P] & ShortHandEqualType);
};
export declare type Where<MT extends object = AnyObject> = Condition<MT> | AndClause<MT> | OrClause<MT>;
export interface AndClause<MT extends object> {
and: Where<MT>[];
}
export interface OrClause<MT extends object> {
or: Where<MT>[];
}
export declare type Direction = 'ASC' | 'DESC';
export declare type Order<MT = AnyObject> = {
[P in keyof MT]: Direction;
};
export declare type Fields<MT = AnyObject> = {
[P in keyof MT]?: boolean;
};
export interface Inclusion<MT extends object = AnyObject> {
relation: string;
scope?: Filter<MT>;
}
export interface Filter<MT extends object = AnyObject> {
where?: Where<MT>;
fields?: Fields<MT>;
order?: string[];
limit?: number;
skip?: number;
offset?: number;
include?: Inclusion<MT>[];
}
export declare function isFilter<MT extends object>(candidate: any): candidate is Filter<MT>;
export declare class WhereBuilder<MT extends object = AnyObject> {
where: Where<MT>;
constructor(w?: Where<MT>);
private add;
cast(clause: AndClause<MT> | OrClause<MT> | Condition<MT>): Where<MT>;
and(...w: (Where<MT> | Where<MT>[])[]): this;
or(...w: (Where<MT> | Where<MT>[])[]): this;
eq<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
neq<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
gt<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
gte<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
lt<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
lte<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
inq<K extends KeyOf<MT>>(key: K, val: MT[K][]): this;
nin<K extends KeyOf<MT>>(key: K, val: MT[K][]): this;
between<K extends KeyOf<MT>>(key: K, val1: MT[K], val2: MT[K]): this;
exists<K extends KeyOf<MT>>(key: K, val?: boolean): this;
impose(where: Where<MT>): this;
build(): Where<MT>;
}
export declare class FilterBuilder<MT extends object = AnyObject> {
filter: Filter<MT>;
constructor(f?: Filter<MT>);
limit(limit: number): this;
offset(offset: number): this;
skip(skip: number): this;
fields(...f: (Fields<MT> | (keyof MT)[] | keyof MT)[]): this;
private validateOrder;
order(...o: (string | string[] | Order<MT>)[]): this;
include(...i: (string | string[] | Inclusion<MT>)[]): this;
where(w: Where<MT>): this;
impose(constraint: Filter<MT> | Where<MT>): this;
build(): Filter<MT>;
}
export declare function filterTemplate(strings: TemplateStringsArray, ...keys: any[]): (ctx: AnyObject) => any;