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
Better Code Design in PHP
afilina
PRO
0
120
CSC509 Lecture 12
javiergs
PRO
0
140
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
500
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
240
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.3k
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
550
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
190
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
470
みんなでプロポーザルを書いてみた
yuriko1211
0
230
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
250
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Jakarta EE meets AI
ivargrimstad
0
200
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Bash Introduction
62gerente
608
210k
KATA
mclloyd
29
14k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Designing Experiences People Love
moore
138
23k
Code Review Best Practice
trishagee
64
17k
Adopting Sorbet at Scale
ufuk
73
9.1k
GitHub's CSS Performance
jonrohan
1030
460k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
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?