-
Notifications
You must be signed in to change notification settings - Fork 6
/
query.ts
143 lines (122 loc) · 3.79 KB
/
query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { IFilteringCriterion } from "../api/dataops/filteringApi";
import { ICondition } from "../api/dataops/filteringCondition";
import { MESSAGE } from "../config/message";
/* Sort direction
*/
export type Direction = "asc" | "desc";
/**
* @internal
*/
type KeyOfObject<T> = Extract<keyof T, string>;
/**
* Object to be passed as aggregation field
* <T> - generic dataset object
*/
export interface IAggField<T = any> {
fn: "COUNT" | "MIN" | "MAX" | "AVG" | "SUM" | "EVERY";
col: KeyOfObject<T> | "*";
}
/* Data sorting
* list of fields + direction
*/
interface ISort<K> {
fields: K[];
direction: Direction;
}
/* Array of data sorting
fields should be in inherited generic dataset model (if it's been set)
*/
type SortedFields<T> = Array<ISort<KeyOfObject<T>>>;
export interface ICompiledQuery<T> {
data: T;
conditions: Array<object>;
fields: string[];
orders: SortedFields<T>;
range: { limit?: number, offset?: number };
relations: {[key: string]: Partial<ICompiledQuery<T>>};
}
export class Query<T = any> {
public fields: Array<KeyOfObject<T> | IAggField<T>>;
public limit: number;
public offset: number;
public data: T;
private relations: Query[] = [];
private filteringConditions: ICondition;
private orders: SortedFields<T> = [];
constructor(public readonly dataset: string) {
}
/*
* This method is here to encapsulate the translation of filter settings
* between the API layer of the SDK (IFilteringCriterion) and the internal
* logic for compiling filters into JSON (ICondition).
*/
public setFilterCriteria(filter: IFilteringCriterion) {
this.filteringConditions = (filter as any).lowLevelCondition;
}
public addSortCondition<K extends Extract<keyof T, string>>(direction: "asc" | "desc", ...fields: K[]) {
if (fields.length === 0) {
throw new Error(MESSAGE.QUERY.MUST_PROVIDE_SORTING_FIELD);
}
this.orders.push({ fields, direction });
}
public addRelation(relation: Query): void {
this.relations.push(relation);
}
/* Collect all conditionals inside one object
* to pass it to the API. Some conditionals can be skipped
*/
public compile(): Partial<ICompiledQuery<T>> {
let compiledQueryOptions: Partial<ICompiledQuery<T>> = {};
/* Compile conditions
*/
if (this.filteringConditions) {
compiledQueryOptions.conditions = [this.filteringConditions.compile()];
}
/* Compile limit and offset options
*/
if (this.limit || this.offset) {
compiledQueryOptions.range = {
...this.limit ? { limit: this.limit } : {},
...this.offset ? { offset: this.offset } : {},
};
}
/* Compile fields
*/
if (this.fields) {
compiledQueryOptions.fields = this.fields.map((field) => typeof field === "object"
? this.compileAggregation(field) : field);
}
/* Compile sort options
*/
if (this.orders.length) {
compiledQueryOptions.orders = this.orders;
}
/* Compile data
*/
if (this.data) {
compiledQueryOptions.data = this.data;
}
/* Compile relations
*/
if (this.relations.length) {
compiledQueryOptions.relations = this.relations.reduce((relations, relation) => (
{ ...relations, [relation.dataset]: relation.compile() }
), {});
}
return compiledQueryOptions;
}
/**
* Compile aggregation object to the string
* for COUNT function replace asterisk with i field
* @param {IAggField} agg an aggregation object
* @returns {string} compiled aggregation function
*/
private compileAggregation(agg: IAggField): string {
if (agg.fn === "COUNT" && agg.col === "*") {
agg.col = "id";
} else if (agg.col === "*") {
throw new Error(`Field name should be provided with the ${agg.fn} function`);
}
return `${agg.fn}(${agg.col})`;
}
}