// Type definitions for Rethinkdb 1.10.0
// Project: http://rethinkdb.com/
// Definitions by: Sean Hess
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Reference: http://www.rethinkdb.com/api/#js
// TODO: Document manipulation and below
///
declare module "rethinkdb" {
export function connect(host: ConnectionOptions, cb?: (err: Error, conn: Connection) => void): Promise;
export function dbCreate(name: string): Operation;
export function dbDrop(name: string): Operation;
export function dbList(): Operation;
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;
export function expr(stuff: any): Expression;
export function now(): Time;
// Control Structures
export function branch(test: Expression, trueBranch: Expression, falseBranch: Expression): Expression;
export class Cursor {
hasNext(): boolean;
each(cb: (err: Error, row: any) => void, done?: () => void): void;
each(cb: (err: Error, row: T) => void, done?: () => void): void;
each(cb: (err: Error, row: any) => boolean, done?: () => void): void; // returning false stops iteration
each(cb: (err: Error, row: T) => boolean, done?: () => void): void; // returning false stops iteration
next(cb: (err: Error, row: any) => void): void;
next(cb: (err: Error, row: T) => void): void;
toArray(cb: (err: Error, rows: any[]) => void): void;
toArray(cb: (err: Error, rows: T[]) => void): void;
toArray(): Promise;
toArray(): Promise;
close(cb: (err: Error) => void): void;
close(): Promise;
}
interface ConnectionOptions {
host: string;
port: number;
db?: string;
authKey?: string;
}
interface Connection {
close(cb: (err: Error) => void): void;
close(opts: { noreplyWait: boolean }, cb: (err: Error) => void): void;
close(): Promise;
close(opts: { noreplyWait: boolean }): Promise;
reconnect(cb?: (err: Error, conn: Connection) => void): Promise;
use(dbName: string): void;
addListener(event: string, cb: Function): void;
on(event: string, cb: Function): void;
}
interface Db {
tableCreate(name: string, options?: TableOptions): Operation;
tableDrop(name: string): Operation;
tableList(): Operation;
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;
replace(obj: Object, options?: UpdateOptions): Operation;
replace(expr: ExpressionFunction): Operation;
delete(options?: UpdateOptions): Operation;
}
interface Table extends Sequence {
indexCreate(name: string, index?: ExpressionFunction): Operation;
indexDrop(name: string): Operation;
indexList(): Operation;
insert(obj: any[], options?: InsertOptions): Operation;
insert(obj: any, options?: InsertOptions): Operation;
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, Writeable {
between(lower: any, upper: any, index?: Index): Sequence;
filter(rql: ExpressionFunction): Sequence;
filter(rql: Expression): Sequence;
filter(obj: { [key: string]: any }): Sequence;
// Join
// these return left, right
innerJoin(sequence: Sequence, join: JoinFunction): Sequence;
outerJoin(sequence: Sequence, join: JoinFunction): Sequence;
eqJoin(leftAttribute: string, rightSequence: Sequence, index?: Index): Sequence;
eqJoin(leftAttribute: ExpressionFunction, rightSequence: Sequence, index?: Index): Sequence;
zip(): Sequence;
// Transform
map(transform: ExpressionFunction): Sequence;
withFields(...selectors: any[]): Sequence;
concatMap(transform: ExpressionFunction): 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;
indexesOf(obj: any): Sequence;
isEmpty(): Expression;
union(sequence: Sequence): Sequence;
sample(n: number): Sequence;
// Aggregate
reduce(r: ReduceFunction, base?: any): Expression;
count(): Expression;
distinct(): Sequence;
groupedMapReduce(group: ExpressionFunction, map: ExpressionFunction, reduce: ReduceFunction, base?: any): Sequence;
groupBy(...aggregators: Aggregator[]): Expression