/
odata-query.ts
209 lines (175 loc) · 9.71 KB
/
odata-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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
AjaxFuncs, AjaxOptions, AjaxResponse,
Ctor, Func1, IQueryBase, IQueryPart, IQueryProvider,
PartArgument, Predicate, QueryPart, Result,
} from "jinqu";
import { InlineCountInfo } from "jinqu";
export class ODataQuery<T extends object, TResponse = any, TExtra = {}> implements IODataQuery<T, TExtra> {
constructor(public readonly provider: IQueryProvider, public readonly parts: IQueryPart[] = []) {
}
public withOptions(options: AjaxOptions): ODataQuery<T, TResponse, TExtra> {
return this.create(QueryPart.create(AjaxFuncs.options, [PartArgument.literal(options)])) as any;
}
public setParameter(key: string, value: any): ODataQuery<T, TResponse, TExtra> {
return this.withOptions({ params: [{ key, value }] });
}
public includeResponse(): ODataQuery<T, TResponse, TExtra & AjaxResponse<TResponse>> {
const part = new QueryPart(AjaxFuncs.includeResponse, []);
return this.create(part) as any;
}
public inlineCount(): IODataQuery<T, TExtra & InlineCountInfo> {
return this.create(QueryPart.inlineCount());
}
public where(predicate: Predicate<T>, ...scopes): IODataQuery<T, TExtra> {
const part = new QueryPart(ODataFuncs.filter, [PartArgument.identifier(predicate, scopes)]);
return this.create(part);
}
public orderBy(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra> {
return this.createOrderedQuery(QueryPart.orderBy(keySelector, scopes));
}
public orderByDescending(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra> {
return this.createOrderedQuery(QueryPart.orderByDescending(keySelector, scopes));
}
public expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(nav: K1, selector?: K2[])
: IExpandedODataQuery<T, AU<T[K1]>, TExtra>;
public expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(nav: K1, filter: Predicate<AU<T[K1]>>, ...scopes)
: IExpandedODataQuery<T, AU<T[K1]>, TExtra>;
public expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(
// tslint:disable-next-line:unified-signatures
nav: K1, selector: K2[], filter: Predicate<AU<T[K1]>>, ...scopes)
: IExpandedODataQuery<T, AU<T[K1]>, TExtra>;
public expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(
nav: K1, prm1?: K2[] | Predicate<AU<T[K1]>>, prm2?: Predicate<AU<T[K1]>>, ...scopes)
: IExpandedODataQuery<T, AU<T[K1]>, TExtra> {
const args = createExpandArgs(nav, prm1, prm2, ...scopes);
return this.createExpandedQuery<AU<T[K1]>>(new QueryPart(ODataFuncs.expand, args));
}
public skip(count: number): IODataQuery<T, TExtra> {
return this.create(QueryPart.skip(count));
}
public take(count: number): IODataQuery<T, TExtra> {
const part = new QueryPart(ODataFuncs.top, [PartArgument.literal(count)]);
return this.create(part);
}
public select<K extends keyof T>(...names: K[]): PromiseLike<Result<Array<Pick<T, K>>, TExtra>> {
const part = new QueryPart(ODataFuncs.oDataSelect, [PartArgument.literal(names)]);
return this.provider.executeAsync([...this.parts, part]);
}
public groupBy<TKey extends object, TResult extends object>(
keySelector: Func1<T, TKey>,
elementSelector?: Func1<T[] & TKey, TResult>,
...scopes: any[]): PromiseLike<Result<TResult[], TExtra>> {
const args = [new PartArgument(keySelector, null, scopes)];
if (elementSelector) {
args.push(new PartArgument(elementSelector, null, scopes));
}
const part = new QueryPart(ODataFuncs.apply, args);
return this.provider.executeAsync([...this.parts, part]) as any;
}
public count(predicate?: Predicate<T>, ...scopes): PromiseLike<Result<number, TExtra>> {
return this.provider.executeAsync([...this.parts, QueryPart.count(predicate, scopes)]);
}
public cast(ctor: Ctor<T>): IODataQuery<T, TExtra> {
return this.create(QueryPart.cast(ctor));
}
public toArrayAsync(ctor?: Ctor<T>): PromiseLike<Result<T[], TExtra>> {
const query = ctor ? this.cast(ctor) : this;
return (query.provider as any).executeAsync([...query.parts, QueryPart.toArray()]);
}
protected create<TResult extends object = T, TNewExtra = TExtra>(part: IQueryPart)
: IODataQuery<TResult, TNewExtra> {
return new ODataQuery<TResult, TResponse, TNewExtra>(this.provider, [...this.parts, part]);
}
protected createOrderedQuery(part: IQueryPart): IOrderedODataQuery<T, TExtra> {
return new OrderedODataQuery<T, TResponse, TExtra>(this.provider, [...this.parts, part]);
}
protected createExpandedQuery<TNav>(part: IQueryPart): IExpandedODataQuery<T, TNav, TExtra> {
return new ExpandedODataQuery<T, TNav, TResponse, TExtra>(this.provider, [...this.parts, part]);
}
}
// tslint:disable-next-line:max-classes-per-file
class OrderedODataQuery<T extends object, TResponse = any, TExtra = {}>
extends ODataQuery<T, TResponse, TExtra> implements IOrderedODataQuery<T, TExtra> {
public thenBy(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra> {
return this.createOrderedQuery(QueryPart.thenBy(keySelector, scopes));
}
public thenByDescending(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra> {
return this.createOrderedQuery(QueryPart.thenByDescending(keySelector, scopes));
}
}
// tslint:disable-next-line:max-classes-per-file
class ExpandedODataQuery<TEntity extends object, TProperty, TResponse = any, TExtra = {}>
extends ODataQuery<TEntity, TResponse, TExtra> implements IExpandedODataQuery<TEntity, TProperty, TExtra> {
public thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(nav: K1, selector?: K2[])
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra>;
public thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(
nav: K1, filter: Predicate<AU<TProperty[K1]>>, ...scopes)
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra>;
public thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(
nav: K1, selector: K2[], filter: Predicate<AU<TProperty[K1]>>, ...scopes)
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra>;
public thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(
nav: K1, prm1?: K2[] | Predicate<AU<TProperty[K1]>>, prm2?: Predicate<AU<TProperty[K1]>>, ...scopes)
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra> {
const args = createExpandArgs(nav, prm1, prm2, ...scopes);
return this.createExpandedQuery<any>(new QueryPart(ODataFuncs.thenExpand, args));
}
}
export interface IODataQuery<T, TExtra = {}> extends IQueryBase {
inlineCount(value?: boolean): IODataQuery<T, TExtra & InlineCountInfo>;
where(predicate: Predicate<T>, ...scopes): IODataQuery<T, TExtra>;
orderBy(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra>;
orderByDescending(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra>;
expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(nav: K1, selector?: K2[])
: IExpandedODataQuery<T, AU<T[K1]>, TExtra>;
expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(nav: K1, filter: Predicate<AU<T[K1]>>, ...scopes)
: IExpandedODataQuery<T, AU<T[K1]>, TExtra>;
expand<K1 extends keyof T, K2 extends keyof AU<T[K1]>>(
nav: K1, selector: K2[], filter: Predicate<AU<T[K1]>>, ...scopes)
: IExpandedODataQuery<T, AU<T[K1]>, TExtra>;
skip(count: number): IODataQuery<T, TExtra>;
take(count: number): IODataQuery<T, TExtra>;
cast(ctor: Ctor<T>): IODataQuery<T, TExtra>;
select<K extends keyof T>(...names: K[]): PromiseLike<Result<Array<Pick<T, K>>, TExtra>>;
groupBy<TKey extends object, TResult extends object>(
keySelector: Func1<T, TKey>, elementSelector?: Func1<T[] & TKey, TResult>, ...scopes: any[])
: PromiseLike<Result<TResult[], TExtra>>;
count(predicate?: Predicate<T>, ...scopes): PromiseLike<Result<number, TExtra>>;
toArrayAsync(ctor?: Ctor<T>): PromiseLike<Result<T[], TExtra>>;
}
export interface IOrderedODataQuery<T, TExtra = {}> extends IODataQuery<T, TExtra> {
thenBy(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra>;
thenByDescending(keySelector: Func1<T>, ...scopes): IOrderedODataQuery<T, TExtra>;
}
export interface IExpandedODataQuery<TEntity, TProperty, TExtra = {}> extends IODataQuery<TEntity, TExtra> {
thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(nav: K1, selector?: K2[])
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra>;
thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(
nav: K1, filter: Predicate<AU<TProperty[K1]>>, ...scopes)
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra>;
thenExpand<K1 extends keyof TProperty, K2 extends keyof AU<TProperty[K1]>>(
nav: K1, selector: K2[], filter: Predicate<AU<TProperty[K1]>>, ...scopes)
: IExpandedODataQuery<TEntity, AU<TProperty[K1]>, TExtra>;
}
export const ODataFuncs = {
apply: "apply",
expand: "expand",
filter: "filter",
oDataSelect: "oDataSelect",
thenExpand: "thenExpand",
top: "top",
};
// Array Unwrapper
type AU<T> = T extends any[] ? T[0] : T;
function createExpandArgs(nav: any, prm1?: any, prm2?: any, ...scopes) {
let selector;
let filter;
if (typeof prm1 !== "function" && typeof prm1 !== "string") {
selector = prm1;
filter = prm2;
} else {
filter = prm1;
scopes = prm2 ? [prm2, ...scopes] : scopes;
}
return [PartArgument.literal(nav), PartArgument.literal(selector), PartArgument.identifier(filter, scopes)];
}