forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rethinkdb.d.ts
236 lines (188 loc) · 6.9 KB
/
rethinkdb.d.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Type definitions for Rethinkdb 1.10.0
// Project: http://rethinkdb.com/
// Definitions by: Sean Hess <https://seanhess.github.io/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Reference: http://www.rethinkdb.com/api/#js
// TODO: Document manipulation and below
///<reference path="../bluebird/bluebird.d.ts"/>
declare module "rethinkdb" {
export function connect(host:ConnectionOptions, cb?:(err:Error, conn:Connection)=>void):Promise<Connection>;
export function dbCreate(name:string):Operation<CreateResult>;
export function dbDrop(name:string):Operation<DropResult>;
export function dbList():Operation<string[]>;
export function db(name:string):Db;
export function table(name:string, options?:{useOutdated:boolean}):Table;
export function asc(property:string):Sort;
export function desc(property:string):Sort;
export var count:Aggregator;
export function sum(prop:string):Aggregator;
export function avg(prop:string):Aggregator;
export function row(name:string):Expression<any>;
export function expr(stuff:any):Expression<any>;
export function now():Time;
// Control Structures
export function branch(test:Expression<boolean>, trueBranch:Expression<any>, falseBranch:Expression<any>):Expression<any>;
export class Cursor {
hasNext():boolean;
each(cb:(err:Error, row:any)=>void, done?:()=>void);
each(cb:(err:Error, row:any)=>boolean, done?:()=>void); // returning false stops iteration
next(cb:(err:Error, row:any) => void);
toArray(cb:(err:Error, rows:any[]) => void);
close();
}
interface ConnectionOptions {
host:string;
port:number;
db?:string;
authKey?:string;
}
interface Connection {
close();
reconnect(cb?:(err:Error, conn:Connection)=>void):Promise<Connection>;
use(dbName:string);
addListener(event:string, cb:Function);
on(event:string, cb:Function);
}
interface Db {
tableCreate(name:string, options?:TableOptions):Operation<CreateResult>;
tableDrop(name:string):Operation<DropResult>;
tableList():Operation<string[]>;
table(name:string, options?:GetTableOptions):Table;
}
interface TableOptions {
primary_key?:string; // 'id'
durability?:string; // 'soft'
cache_size?:number;
datacenter?:string;
}
interface GetTableOptions {
useOutdated: boolean;
}
interface Writeable {
update(obj:Object, options?:UpdateOptions):Operation<WriteResult>;
replace(obj:Object, options?:UpdateOptions):Operation<WriteResult>;
replace(expr:ExpressionFunction<any>):Operation<WriteResult>;
delete(options?:UpdateOptions):Operation<WriteResult>;
}
interface Table extends Sequence {
indexCreate(name:string, index?:ExpressionFunction<any>):Operation<CreateResult>;
indexDrop(name:string):Operation<DropResult>;
indexList():Operation<string[]>;
insert(obj:any[], options?:InsertOptions):Operation<WriteResult>;
insert(obj:any, options?:InsertOptions):Operation<WriteResult>;
get(key:string):Sequence; // primary key
getAll(key:string, index?:Index):Sequence; // without index defaults to primary key
getAll(...keys:string[]):Sequence;
}
interface Sequence extends Operation<Cursor>, Writeable {
between(lower:any, upper:any, index?:Index):Sequence;
filter(rql:ExpressionFunction<boolean>):Sequence;
filter(rql:Expression<boolean>):Sequence;
filter(obj:{[key:string]:any}):Sequence;
// Join
// these return left, right
innerJoin(sequence:Sequence, join:JoinFunction<boolean>):Sequence;
outerJoin(sequence:Sequence, join:JoinFunction<boolean>):Sequence;
eqJoin(leftAttribute:string, rightSequence:Sequence, index?:Index):Sequence;
eqJoin(leftAttribute:ExpressionFunction<any>, rightSequence:Sequence, index?:Index):Sequence;
zip():Sequence;
// Transform
map(transform:ExpressionFunction<any>):Sequence;
withFields(...selectors:any[]):Sequence;
concatMap(transform:ExpressionFunction<any>):Sequence;
orderBy(...keys:string[]):Sequence;
orderBy(...sorts:Sort[]):Sequence;
skip(n:number):Sequence;
limit(n:number):Sequence;
slice(start:number, end?:number):Sequence;
nth(n:number):Expression<any>;
indexesOf(obj:any):Sequence;
isEmpty():Expression<boolean>;
union(sequence:Sequence):Sequence;
sample(n:number):Sequence;
// Aggregate
reduce(r:ReduceFunction<any>, base?:any):Expression<any>;
count():Expression<number>;
distinct():Sequence;
groupedMapReduce(group:ExpressionFunction<any>, map:ExpressionFunction<any>, reduce:ReduceFunction<any>, base?:any):Sequence;
groupBy(...aggregators:Aggregator[]):Expression<Object>; // TODO: reduction object
contains(prop:string):Expression<boolean>;
// Manipulation
pluck(...props:string[]):Sequence;
without(...props:string[]):Sequence;
}
interface ExpressionFunction<U> {
(doc:Expression<any>):Expression<U>;
}
interface JoinFunction<U> {
(left:Expression<any>, right:Expression<any>):Expression<U>;
}
interface ReduceFunction<U> {
(acc:Expression<any>, val:Expression<any>):Expression<U>;
}
interface InsertOptions {
upsert: boolean; // true
durability: string; // 'soft'
return_vals: boolean; // false
}
interface UpdateOptions {
non_atomic: boolean;
durability: string; // 'soft'
return_vals: boolean; // false
}
interface WriteResult {
inserted: number;
replaced: number;
unchanged: number;
errors: number;
deleted: number;
skipped: number;
first_error: Error;
generated_keys: string[]; // only for insert
}
interface JoinResult {
left:any;
right:any;
}
interface CreateResult {
created: number;
}
interface DropResult {
dropped: number;
}
interface Index {
index: string;
left_bound?: string; // 'closed'
right_bound?: string; // 'open'
}
interface Expression<T> extends Writeable, Operation<T> {
(prop:string):Expression<any>;
merge(query:Expression<Object>):Expression<Object>;
append(prop:string):Expression<Object>;
contains(prop:string):Expression<boolean>;
and(b:boolean):Expression<boolean>;
or(b:boolean):Expression<boolean>;
eq(v:any):Expression<boolean>;
ne(v:any):Expression<boolean>;
not():Expression<boolean>;
gt(value:T):Expression<boolean>;
ge(value:T):Expression<boolean>;
lt(value:T):Expression<boolean>;
le(value:T):Expression<boolean>;
add(n:number):Expression<number>;
sub(n:number):Expression<number>;
mul(n:number):Expression<number>;
div(n:number):Expression<number>;
mod(n:number):Expression<number>;
hasFields(...fields:string[]):Expression<boolean>;
default(value:T):Expression<T>;
}
interface Operation<T> {
run(conn:Connection, cb?:(err:Error, result:T)=>void):Promise<T>;
}
interface Aggregator {}
interface Sort {}
interface Time {}
// http://www.rethinkdb.com/api/#js
// TODO control structures
}