var r = require('rethinkdb'), async = require('async');
console.log('=== Open a connection'); r.connect({host: process.env.RDB_PORT_8080_TCP_ADDR, port: 28015}, function(err,conn){ if (err) throw err; async.waterfall([ function(callback){ console.log('=== Drop a table'); r.db('test').tableDrop('authors').run(conn,callback); }, function(res,callback) { console.log('=== Create a table'); r.db('test').tableCreate('authors').run(conn,callback); }, function(res,callback){ console.log('=== Insert data'); r.table('authors').insert([ { name: "William Adama", tv_show: "Battlestar Galactica", posts: [ {title: "Decommissioning speech", content: "The Cylon War is long over..."}, {title: "We are at war", content: "Moments ago, this ship received word..."}, {title: "The new Earth", content: "The discoveries of the past few days..."} ] }, { name: "Laura Roslin", tv_show: "Battlestar Galactica", posts: [ {title: "The oath of office", content: "I, Laura Roslin, ..."}, {title: "They look like us", content: "The Cylons have the ability..."} ] }, { name: "Jean-Luc Picard", tv_show: "Star Trek TNG", posts: [ {title: "Civil rights", content: "There are some words I've known since..."} ] } ]).run(conn,callback); }, function(res,callback){ console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback){ console.log('=== All documents in a table'); r.table('authors').run(conn,callback); }, function(cursor,callback){ cursor.toArray(callback); }, function(res,callback) { console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback){ console.log('=== Filter documents based on a condition'); r.table('authors') .filter(r.row('name').eq("William Adama")) .run(conn,callback); }, function(cursor,callback){ cursor.toArray(callback); }, function(res,callback) { console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback) { console.log('=== Retrieve all authors who have more than two posts'); r.table('authors') .filter(r.row('posts').count().gt(2)) .run(conn,callback); }, function(cursor,callback){ cursor.toArray(callback); }, function(res,callback) { console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback) { console.log('=== Update documents'); r.table('authors') .update({type: "fictional"}) .run(conn,callback); },function(res,callback) { console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback){ console.log('=== Update a subset of documents by filtering the table first'); r.table('authors') .filter(r.row("name").eq("William Adama")) .update({rank: "Admiral"}) .run(conn,callback); },function(res,callback){ console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback){ console.log('=== add an additional post'); r.table('authors') .filter(r.row("name").eq("Jean-Luc Picard")) .update({posts: r.row("posts").append({ title: "Shakespeare", content: "What a piece of work is man..."})}) .run(conn,callback); },function(res,callback){ console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback){ console.log('=== Delete documents'); r.table('authors') .filter(r.row('posts').count().lt(3)) .delete() .run(conn,callback); },function(res,callback){ console.log(JSON.stringify(res, null, 2)); callback(null); }, function(callback){ console.log('=== Close a connection'); conn.close(callback); } ],function(err, result){ if (err) throw err; }); });
|