[rethinkdb] Adds hasFields()

This commit is contained in:
Alex Gorbatchev
2016-10-02 13:52:38 -07:00
parent dd6c2c88d7
commit 4601d7808d
2 changed files with 51 additions and 10 deletions
+20 -5
View File
@@ -2,17 +2,27 @@
import * as r from "rethinkdb";
r.connect({ host: "localhost", port: 28015 }, function(err, conn) {
function errorAndCursorCallback(err: Error, cursor: r.Cursor): void {}
function cursorCallback(cursor: r.Cursor): void {}
r.connect({ host: "localhost", port: 28015 }, function(err: Error, conn: r.Connection) {
console.log("HI", err, conn);
const testDb = r.db("test");
r.table("players").hasFields("games_won").run(conn, errorAndCursorCallback);
r.table("players").hasFields({ "games_won": { "championships": true } }).run(conn, errorAndCursorCallback);
r.table("players").filter(r.row.hasFields("games_won").not()).run(conn, errorAndCursorCallback);
r.table("players").filter(r.row.hasFields({ "games_won": { "championships": true } }).not()).run(conn, errorAndCursorCallback);
testDb.tableCreate("users").run(conn, function(err, stuff) {
const users = testDb.table("users");
users.insert({ name: "bob" }).run(conn, function() {
});
users.hasFields("foo_bar").run(conn, () => {});
users.filter(function(doc?) {
return doc("henry").eq("bob");
})
@@ -36,12 +46,17 @@ r.connect({ host: "localhost", port: 28015 }, function(err, conn) {
});
// use promises instead of callbacks
r.connect({ host: "localhost", port: 28015 }).then(function(conn) {
r.connect({ host: "localhost", port: 28015 }).then(function(conn: r.Connection) {
console.log("HI", conn);
const testDb = r.db("test");
testDb.tableCreate("users").run(conn).then(function(stuff) {
r.table("players").hasFields("games_won").run(conn).then(cursorCallback);
r.table("players").hasFields({ "games_won": { "championships": true } }).run(conn).then(cursorCallback);
r.table("players").filter(r.row.hasFields("games_won").not()).run(conn).then(cursorCallback);
r.table("players").filter(r.row.hasFields({ "games_won": { "championships": true } }).not()).run(conn).then(cursorCallback);
testDb.tableCreate("users").run(conn).then(function() {
const users = testDb.table("users");
users.insert({ name: "bob" }).run(conn, function() {
@@ -53,8 +68,8 @@ r.connect({ host: "localhost", port: 28015 }).then(function(conn) {
.between("james", "beth")
.limit(4)
.run(conn)
.then(cursor => {
cursor.toArray().then(rows => {
.then((cursor: r.Cursor) => {
cursor.toArray().then((rows: any[]) => {
console.log(rows);
});
});
+31 -5
View File
@@ -41,7 +41,7 @@ declare module "rethinkdb" {
export function sum(prop: string): Aggregator;
export function avg(prop: string): Aggregator;
export function row(name: string): Expression<any>;
export const row: Row;
export function expr(stuff: any): Expression<any>;
export function now(): Expression<Time>;
@@ -79,6 +79,10 @@ declare module "rethinkdb" {
close(): Promise<void>;
}
interface Row extends Expression<any> {
(name: string): Expression<any>;
}
/**
* Connection options.
*
@@ -199,7 +203,27 @@ declare module "rethinkdb" {
includeTypes: boolean;
}
interface Table extends Sequence {
interface HasFields<T> {
/**
* Test if an object has one or more fields. An object has a field if it has that key and the key has a non-null value.
*
* `hasFields` lets you test for nested fields in objects. If the value of a field is itself a set of key/value pairs, you can test for the presence of specific keys.
*
* See: https://rethinkdb.com/api/javascript/has_fields/
*/
hasFields(selector: BooleanMap): T;
/**
* Test if an object has one or more fields. An object has a field if it has that key and the key has a non-null value. For instance, the object `{'a': 1,'b': 2,'c': null}` has the fields `a` and `b`.
*
* When applied to a single object, `hasFields` returns `true` if the object has the fields and `false` if it does not. When applied to a sequence, it will return a new sequence (an array or stream) containing the elements that have the specified fields.
*
* See: https://rethinkdb.com/api/javascript/has_fields/
*/
hasFields(...fields: string[]): T;
}
interface Table extends Sequence, HasFields<Sequence> {
indexCreate(name: string, index?: ExpressionFunction<any>): Operation<CreateResult>;
indexDrop(name: string): Operation<DropResult>;
indexList(): Operation<string[]>;
@@ -321,7 +345,11 @@ declare module "rethinkdb" {
right_bound?: string; // 'open'
}
interface Expression<T> extends Writeable, Operation<T> {
interface BooleanMap {
[ key: string ]: Boolean | BooleanMap;
}
interface Expression<T> extends Writeable, Operation<T>, HasFields<Expression<number>> {
(prop: string): Expression<any>;
merge(query: Expression<Object>): Expression<Object>;
append(prop: string): Expression<Object>;
@@ -366,8 +394,6 @@ declare module "rethinkdb" {
div(n: number): Expression<number>;
mod(n: number): Expression<number>;
hasFields(...fields: string[]): Expression<boolean>;
default(value: T): Expression<T>;
}