Skip to content

Instantly share code, notes, and snippets.

@deontologician
Created April 20, 2015 20:00
Show Gist options
  • Save deontologician/a769827f06a593e60064 to your computer and use it in GitHub Desktop.
Save deontologician/a769827f06a593e60064 to your computer and use it in GitHub Desktop.
connection
var r = require('rethinkdb'),
connection;
r.connect( {host: 'localhost', port: 28015} )
.then(function(conn) {
console.log('connected to RethinkDB');
connection = conn;
return r.dbList().run(conn);
}).then(function(dbs){
console.log('dbs: ' + dbs);
if (dbs.indexOf('aqua') == -1) {
console.log('about to create db');
return r.dbCreate('aqua').run(connection)
.catch(function(err) {
console.log('Cannot create database.');
throw err;
}).then(function(dbCreationResult) {
connection.use('aqua');
return r.tableCreate('products').run(connection);
}).catch(function(err) {
console.log('Cannot create table <products>.');
throw err;
}).then(function(conn) {
return r.table('products').insert({product: 'Novac'}).run(connection);
});
}
}).error(function(err) {
console.log('RethinkDB seems unavailable. Aborting.');
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment