Skip to content

Instantly share code, notes, and snippets.

@thejsj
Created May 26, 2015 23:35
Show Gist options
  • Save thejsj/fd7c9b3f8b1738d0ecfc to your computer and use it in GitHub Desktop.
Save thejsj/fd7c9b3f8b1738d0ecfc to your computer and use it in GitHub Desktop.
A simple script showing RethinkDB's JavaScript API
var r = require('rethinkdb');
var assert = require('assert');
// Sample table names
var sampleTableName1 = 'sample_table_' + Math.floor(Math.random() * 10000);
var sampleTableName2 = 'sample_table_' + Math.floor(Math.random() * 10000);
// Create a table using callbacks
r.connect(function (err, conn) {
r.db('test').tableCreate(sampleTableName1)
.run(conn, function (err, result) {
// There should be no error and result should be an object
assert.equal(err, undefined);
assert.equal(typeof result, "object");
// Make it throw an error by creating a table with the same name
r.db('test').tableCreate(sampleTableName1)
.run(conn, function (err, result) {
// Should pass an error to the callback and not throw an error
// There should be no `result`
assert.ok(err instanceof Error);
assert.equal(result, undefined);
r.db('test').tableDrop(sampleTableName1).run(conn, function (err, result) {
// Make sure we didn't get an error
assert.equal(err, undefined);
assert.equal(result.tables_dropped, 1);
console.log('All Callback tests O.K.');
});
});
});
});
r.connect()
.then(function (conn) {
return Promise.resolve()
.then(function () {
return r.db('test').tableCreate(sampleTableName2).run(conn);
})
.then(function (result) {
assert.equal(typeof result, "object");
return r.db('test').tableCreate(sampleTableName2).run(conn);
})
.catch(function (err) {
assert.ok(err instanceof Error);
})
.then(function () {
return r.db('test').tableDrop(sampleTableName2).run(conn);
})
.then(function (result) {
assert.equal(result.tables_dropped, 1);
console.log('All Promise tests O.K.');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment