Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RethinkDB @ Sydney Node Ninjas

Josh Hunt
August 06, 2015

RethinkDB @ Sydney Node Ninjas

Josh Hunt

August 06, 2015
Tweet

More Decks by Josh Hunt

Other Decks in Programming

Transcript

  1. RethinkDB is… Open Source Database for the realtime web Schemaless

    ‘JSON’ document store Easy to develop, easy to deploy
  2. Realtime Changefeeds Subscribe to any query and receive notification when

    data changes No need to poll for updates Eliminates infra and plumbing code
  3. Easy deployment Scale with web UI for hassle-free sharding and

    replication Hosted options with Compose.io Support contracts available
  4. ReQL 101 Native to your programming language by chaining functions

    Secure - injection attacks are minimised Queries are executed AND distributed by the server
  5. (Secondary) Indexes Indexes makes queries faster RethinkDB has auto GUID

    primary index Can have multiple secondary indexes, using any field, or multiple fields with compound indexes
  6. Store subexpressions in variables to reuse or for subqueries expired

    = r.row('expiryDate').lt(r.row()) r.table('episodes') .filter(expired) .delete()
  7. Store subexpressions in variables to remove boilerplate // models/Shows.js module.exports

    = r.table('shows') // routes/shows.js Shows = require('../models/Shows') Shows.get(req.params.showId)
  8. Querying geospatial data home = r.point(-33.42, 149.57) r.table('cities') .getNearest(home, {index:

    'location'}) r.table('cities') .get(1)('location') .distance(r.table(‘cities').get(2)('location'))
  9. Realtime changefeeds Subscribe to change notifications on a database query

    Receive old and new value Works great with Javascript because its async
  10. Get notified of every change to the show table r.table('shows')

    .changes() .run((update) => { update.each(console.log) })
  11. Get notified when the top shows (by streams) changes r.table('shows')

    .orderBy({index: r.desc('streams')}) .limit(5) .changes()
  12. Use cases Anything realtime - chat, scores, etc To avoid

    making DB requests on every request Decouple and send analytics
  13. Official driver r = require('rethinkdb'); r.connect() .then((conn) => { return

    r.table('show') .insert({name: 'The Block'}) .run() .finally( => { conn.close() }) }) .then((output) => { console.log(output); })
  14. RethinkDB Dash Alternate driver with ‘advanced features’ Connection pooling -

    don’t worry about conn.close() Cursors are coerced to arrays by default
  15. r.connect() .then((conn) => { return r.table('actors') .filter(r.row('age').gt(20)) .run() .finally( =>

    { conn.close() }) }) .then((cursor) => { return cursor.toArray(); }) .then((result) => { assert(Array.isArray(result)); }) r = require('rethinkdbdash')(); r.table('actors') .filter(r.row('age').gt(20)) .run() .then((result) => { assert(Array.isArray(result)); }) Official driver vs rethinkdbdash
  16. Thinky ORM Very light models for RethinkDB Handles creating indexes

    for you Automatically join tables (based on model definition)
  17. Creating Thinky models Show = thinky.createModel('Show', { id: type.string(), title:

    type.string(), drm: type.boolean() }) Episode = thinky.createModel('Episode', { id: type.string(), title: type.string().min(2), description: type.string() }) Episode.belongsTo(Show, 'show', 'showId', 'id');
  18. Creating documents with Thinky models show = new Show({title: 'The

    Block', drm: false}); episode = new Episode({title: 'Episode 455', description: ‘...'}); episode.show = show; episode.saveAll().then((savedEpisode) => { console.log(savedEpisode.id); })
  19. Resources rethinkdb.com - Indepth API/query documentation - FAQ (rethinkdb vs

    mongodb) - Stability Report - Limitations of RethinkDB