Created
May 26, 2015 23:35
-
-
Save thejsj/fd7c9b3f8b1738d0ecfc to your computer and use it in GitHub Desktop.
A simple script showing RethinkDB's JavaScript API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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