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
130
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
見た目から始める生産性向上
ikumatadokoro
10
1.3k
Next.js App Router
quramy
11
1.6k
Tailwind CSSを本気でカスタマイズする方法
fsubal
14
5.5k
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.2k
What We Can Learn From OSS
inouehi
0
430
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
700
ServerAction で Progressive Enhancement はどこまで頑張れるか? / progressive-enhancement-with-server-action
takefumiyoshii
6
400
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
280
Site Reliability Engineering for GMO
pyama86
8
1.1k
GitHub Copilotのススメ
marcy731
1
220
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
110
Git Lint
bkuhlmann
4
760
Featured
See All Featured
In The Pink: A Labor of Love
frogandcode
138
21k
GraphQLの誤解/rethinking-graphql
sonatard
55
9.3k
Web Components: a chance to create the future
zenorocha
306
41k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
65
14k
A Philosophy of Restraint
colly
197
16k
YesSQL, Process and Tooling at Scale
rocio
165
13k
Atom: Resistance is Futile
akmur
260
25k
Stop Working from a Prison Cell
hatefulcrawdad
267
19k
Scaling GitHub
holman
457
140k
Navigating Team Friction
lara
179
13k
Producing Creativity
orderedlist
PRO
338
39k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
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?