// 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; // TODO: reduction object contains(prop: string): Expression; // Manipulation pluck(...props: string[]): Sequence; without(...props: string[]): Sequence; } interface ExpressionFunction { (doc: Expression): Expression; } interface JoinFunction { (left: Expression, right: Expression): Expression; } interface ReduceFunction { (acc: Expression, val: Expression): Expression; } 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 extends Writeable, Operation { (prop: string): Expression; merge(query: Expression): Expression; append(prop: string): Expression; contains(prop: string): Expression; and(b: boolean): Expression; or(b: boolean): Expression; eq(v: any): Expression; ne(v: any): Expression; not(): Expression; gt(value: T): Expression; ge(value: T): Expression; lt(value: T): Expression; le(value: T): Expression; add(n: number): Expression; sub(n: number): Expression; mul(n: number): Expression; div(n: number): Expression; mod(n: number): Expression; hasFields(...fields: string[]): Expression; default(value: T): Expression; } interface Operation { run(conn: Connection, cb?: (err: Error, result: T) => void): Promise; } interface Aggregator { } interface Sort { } interface Time { } // http://www.rethinkdb.com/api/#js // TODO control structures }