30 second quickstart in Dart

Dart isn’t just for the front-end, it’s also a powerful server-side platform to build api’s and command line tools.

One great example of how powerful Dart is on the server-side is by using it with an equally powerful database RethinkDB.

Yes, you can use RethinkDB with Dart!

The driver is updated to support the latest version of RethinkDB and using it is very simple.

I’ll be using Webstorm in this tutorial, which I think is the best IDE for Dart.

First, you’ll need to create a new project.

We’re going to use a console application for this, since the driver is intended to run on a server.

Next you want to add the dependency to pub.yaml. You can find the github repo for the driver if you ever want to contribute or build from source. https://github.com/billysometimes/rethinkdb. Props to BillySometimes for building this.

The current version is 0.7.0 and works with the latest version of RethinkDB, which at the time of writing is 1.16

Notice the rethinkdb_driver entry under dependecies

Now that all the boring stuff is out of the way, it’s time to write some code.

To keep things simple lets take the Javascript driver example and see how we do the same in Dart. This will give you an easy understanding of how to write RethinkDB queries in Dart when looking at RethinkDB’s documentation and examples.

http://rethinkdb.com/docs/install-drivers/javascript/

r = require('rethinkdb')

r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {

if(err) throw err;

r.db('test').tableCreate('tv_shows').run(conn, function(err, res) {

if(err) throw err;

console.log(res);

r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn, function(err, res)

{

if(err) throw err;

console.log(res);

});

});

});

Typical Javascript, nothing fancy or new.

Lets see how we can acheive the same in Dart.

First you’ll need to import the Dart driver in your main.dart file.

import 'package:rethinkdb_driver/rethinkdb_driver.dart';

Next to keep things aligned with the recommended RethinkDB patterns, create a variable r and assign it to the RethinkDB object

Rethinkdb r = new Rethinkdb();

Excellent. We’re making progress.

Next create a method called connectToDriverAndQuery. You can and should come up with a better name but for the sake of the demo we’ll stick to this.

connectToDriverAndQuery(){}

Straight forward Dart.

One thing I want to add though is async/await support. If you want to learn more about how async/await works, check out https://www.dartlang.org/articles/await-async/

To make a method support async/await, just add the async keyword after the method declaration.

connectToDriverAndQuery() async {}

Boom. It’s that simple.

Next up, we want to connect to our RethinkDB server. Since the method is async enabled, we can connect to our server like such.

var connection = await r.connect(db:"test");

No need for callbacks.

The connection variable is important as it will be passed into all our queries. You can also add host: and port: parameters to the method like such

var connection = await r.connect(db:"test", host:"192.168.99.100", port:49154);

That funky port is thanks to the docker instance I run with RethinkDB. www.kitematic.com

Next up, staying true to the 30 second javascript example above, we want to create a table called tv_shows.

var tableCreate = await r.tableCreate("tv_shows").run(connection);

Notice that we pass in the connection variable from above.

Not only is RethinkDB’s syntax clear, but async/await makes it too easy.

Lastly, following the Javascript example, we want to insert a tv show.

var insertRow = await r.table("tv_shows").insert({ "name": 'Star Trek TNG' }).run(connection);

It is that simple. No need for messy callbacks and it’s all asynchrnous.

If you print(insertRow) you’ll see the same output as you would in the javascript driver.

print(insertRow); {deleted: 0, errors: 0, generated_keys: [f6690740-cb9f-4901-9b2f-243f67744418], inserted: 1, replaced: 0, skipped: 0, unchanged: 0}

Lets take a look at the entire main.dart for reference.

import 'package:rethinkdb_driver/rethinkdb_driver.dart';



Rethinkdb r = new Rethinkdb();



main() {

connectToDriver();

}



connectToDriver() async { var connection = await r.connect(db:"test", host:"192.168.99.100", port:49154); var insertRow = await r.table("tv_shows").insert({ "name": 'Star Trek TNG' }).run(connection); print(insertRow); }

Dart and RethinkDB work great together. Using Dart’s async/await syntax, I personally find it much better to develop in than the Node.js driver.

Want to learn more? Check out the docs at http://rethinkdb.com/docs/ and the great documentation at Dart https://www.dartlang.org/docs/