–Matthew Daly, Stack Overflow User
“A degree of antipathy
toward relational
databases”
Slide 28
Slide 28 text
“Poorly supported
compared to non-
relational databases”
–Matthew Daly, Stack Overflow User
Slide 29
Slide 29 text
node-
node-
node-
node-
mysql
postgres
mariasql
sqlite3
Slide 30
Slide 30 text
Existing SQL Libs?
Common DBAPI?
Higher level abstractions
Slide 31
Slide 31 text
Existing SQL Libs?
Database specific
Mix of ORM layer and query layer
No transaction api!!
"Jack of All Trades"
DIY: gets you most of the way there, but…
Other Builder Features
• Raw queries
• Aggregate queries (min, max, sum)
• Column, subquery aliasing ".as()"
• Normalizes missing data in inserts
• Tries hard to not let you screw up
Slide 55
Slide 55 text
…or: JavaScript on ACID
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
Atomicity
Consistency
Isolation
Durability
Slide 58
Slide 58 text
Transactions
Snapshot state of the world
Ability to ROLLBACK
Prevent others from altering data as you’re working
with it (Locks)
A little trickier to pull off in Node
Slide 59
Slide 59 text
1. Register a user, giving them an ID
2. Send that to a third party service,
registering the user
3. Create additional account rows
4. Return response to the user
Slide 60
Slide 60 text
No content
Slide 61
Slide 61 text
1. Register a user, giving them an ID
2. Send that to a third party service, registering the user
catch, rollback 1
3. Create additional account rows
catch, rollback 1, 3?
4. Return response to the user
Object Relational Mapper
• Takes care of standard SQL queries for you,
especially for common CRUD operations
• model.fetch()
• model.fetchAll()
• model.save()
• model.destroy()
• Mapping a single model to
multiple model types
• Picture for employees, for
products
Polymorphic Associations
Slide 76
Slide 76 text
Builds on Knex
Slide 77
Slide 77 text
No content
Slide 78
Slide 78 text
No content
Slide 79
Slide 79 text
Eager Loading
• Avoids the N+1 Query Problem
• Find an account with associated posts,
the comments on those posts, and the
accounts that made the comments.
Slide 80
Slide 80 text
No content
Slide 81
Slide 81 text
Eager Loading
• Avoids the N+1 Query Problem
• Find an account with "approved"
associated posts, the comments on
those posts, and the accounts that
made the comments.
Slide 82
Slide 82 text
Dynamically constrain
relations
Slide 83
Slide 83 text
Eager Loading
Slide 84
Slide 84 text
"Tapping into" the query chain
Allows for adding additional query parameters
dynamically to the model's query
Slide 85
Slide 85 text
Transactions
In Bookshelf, each async call (fetch, save,
create, load, destroy), takes an “options”
object which may optionally take a
{transacting: t} - where “t” is the object in the
transaction closure
Slide 86
Slide 86 text
No content
Slide 87
Slide 87 text
No content
Slide 88
Slide 88 text
No content
Slide 89
Slide 89 text
Shared models:
Sounds great, not as
great in practice
Slide 90
Slide 90 text
No content
Slide 91
Slide 91 text
No content
Slide 92
Slide 92 text
…not the first to do this
Slide 93
Slide 93 text
…not the last to do this
Slide 94
Slide 94 text
No content
Slide 95
Slide 95 text
No content
Slide 96
Slide 96 text
No content
Slide 97
Slide 97 text
No content
Slide 98
Slide 98 text
No content
Slide 99
Slide 99 text
No content
Slide 100
Slide 100 text
No content
Slide 101
Slide 101 text
No content
Slide 102
Slide 102 text
No content
Slide 103
Slide 103 text
No content
Slide 104
Slide 104 text
Just some of what's
out there…
Slide 105
Slide 105 text
…this still isn't enough
Slide 106
Slide 106 text
Don't
Reinvent
the wheel
Slide 107
Slide 107 text
Don't
Reinvent
the wheel
Slide 108
Slide 108 text
!
Reinvent
the wheel
Slide 109
Slide 109 text
!
Reinvent
the wheel*
Slide 110
Slide 110 text
!
Reinvent
the wheel*
* just not on the client side…
Slide 111
Slide 111 text
!
Reinvent
the wheel*
* just not on the client side…
Slide 112
Slide 112 text
!
Reinvent
the wheel
Slide 113
Slide 113 text
Take a look at
other libraries
Slide 114
Slide 114 text
Take a look at
other languages
Slide 115
Slide 115 text
Node: a target for
traditional "CRUD"
web applications