Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RethinkDB @ Sydney Node Ninjas
Search
Josh Hunt
August 06, 2015
Programming
2
140
RethinkDB @ Sydney Node Ninjas
Josh Hunt
August 06, 2015
Tweet
Share
More Decks by Josh Hunt
See All by Josh Hunt
Using Webpack At Scale
joshhunt
2
760
Other Decks in Programming
See All in Programming
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
150
XP, Testing and ninja testing
m_seki
2
140
カクヨムAndroidアプリのリブート
numeroanddev
0
440
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
1
360
GoのGenericsによるslice操作との付き合い方
syumai
3
680
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
210
DroidKnights 2025 - 다양한 스크롤 뷰에서의 영상 재생
gaeun5744
3
300
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
1
360
Perplexity Slack Botを作ってAI活用を進めた話 / AI Engineering Summit プレイベント
n3xem
0
670
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
1
210
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
770
WindowInsetsだってテストしたい
ryunen344
1
190
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
5.6k
Automating Front-end Workflow
addyosmani
1370
200k
Optimizing for Happiness
mojombo
379
70k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
34k
Documentation Writing (for coders)
carmenintech
71
4.9k
What's in a price? How to price your products and services
michaelherold
246
12k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Scaling GitHub
holman
459
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
For a Future-Friendly Web
brad_frost
179
9.8k
Transcript
RethinkDB Josh Hunt Developer @ Mi9
What is RethinkDB?
RethinkDB is… Open Source Database for the realtime web Schemaless
‘JSON’ document store Easy to develop, easy to deploy
RethinkDB is MongoDB that doesn't suck
Realtime Changefeeds Subscribe to any query and receive notification when
data changes No need to poll for updates Eliminates infra and plumbing code
Easy development Modern, native query language in Javascript/Python/Ruby/Go/whatever Query features
like join, map, reduce Web UI for testing queries (with docs!)
Easy deployment Scale with web UI for hassle-free sharding and
replication Hosted options with Compose.io Support contracts available
Demo
Rethink(ing) Queries
ReQL 101 Native to your programming language by chaining functions
Secure - injection attacks are minimised Queries are executed AND distributed by the server
Sample queries r.table('shows') .get('e346caa5...') r.table('actors') .pluck('lastName') .distinct().count()
More sample queries r.table('actors') .filter(r.row('age').gt(20)) r.table('characters') .filter({species: 'hobbit'}) .update({species: 'halfling'})
Demo
(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
Compound indexes r.table('cast').indexCreate('fullName', [r.row('firstName'), r.row('lastName')] )
Compound indexes r.table('cast').indexCreate('fullName', [r.row('firstName'), r.row('lastName')] ) r.table('cast').getAll( ['Josh', 'Hunt'], {index:
'fullName'} )
More ReQL Store (sub)queries into variables Location aware: query on
geospatial data Accessing external APIs
Store subexpressions in variables to reuse or for subqueries expired
= r.row('expiryDate').lt(r.row()) r.table('episodes') .filter(expired) .delete()
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)
Adding geospatial data r.table('cities').insert([ {name: 'Sydney', location: r.point(-33.865, 151.209)}, {name:
'Melbourne', location: r.point(-37.813, 144.963)} ])
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'))
Accessing external APIs r.http('https://api.github.com/repos/joshhunt/uber') r.table('repos') .insert( r.http('https://api.github.com/repos/joshhunt/uber') .pluck('name', 'ssh_url', 'language',
'description', 'watchers_count') )
Realtime with Changefeeds
Realtime changefeeds Subscribe to change notifications on a database query
Receive old and new value Works great with Javascript because its async
Get notified of every change to the show table r.table('shows')
.changes() .run((update) => { update.each(console.log) })
Change for new document { old_val: null new_val: {name: 'Bilbo
Baggins'} }
Change for updated document { old_val: {name: 'Gandalf the Grey'}
new_val: {name: 'Gandalf the White'} }
Change for deleted document { old_val: {name: 'Saruman'} new_val: null
}
Get notified when the top shows (by streams) changes r.table('shows')
.orderBy({index: r.desc('streams')}) .limit(5) .changes()
Supported queries Subscribe to changes to get, between, filter, map,
min, max, orderBy, limit
Use cases Anything realtime - chat, scores, etc To avoid
making DB requests on every request Decouple and send analytics
Using Rethink with Node.js
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); })
RethinkDB Dash Alternate driver with ‘advanced features’ Connection pooling -
don’t worry about conn.close() Cursors are coerced to arrays by default
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
Thinky ORM Very light models for RethinkDB Handles creating indexes
for you Automatically join tables (based on model definition)
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');
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); })
Retrieving and joining with Thinky models Episode.get(req.params.episodeId) .getJoin().run() .then((episode) =>
{ console.log(episode); assert(episode.show.title); })
Resources rethinkdb.com - Indepth API/query documentation - FAQ (rethinkdb vs
mongodb) - Stability Report - Limitations of RethinkDB
Questions?