Tim Griesser
Twitter: @tgriesser
IRC: tgriesser
IRC Room: #bookshelf
Slide 4
Slide 4 text
No content
Slide 5
Slide 5 text
...easily building fast, scalable network
applications. Node.js uses an event-driven,
non-blocking I/O model that makes it
lightweight and efficient, perfect for data-
intensive real-time applications that run
across distributed devices.
• Lack of separation between query builder & ORM layer
• Lack of transaction support
• “Build queries by hand”
• Inconsistent APIs among core libs
• “Jack Of All Trades”
Existing RBDMS Abstractions:
Slide 14
Slide 14 text
• Still a relatively young ecosystem
• Lots of examples with NoSQL
• Touted as great for: streaming, realtime applications,
with buffers and websockets...
• Not so much for model heavy applications as you’d see
in Rails, Django, Laravel,
$insert_language_framework_here
Slide 15
Slide 15 text
...oh well, guess I’ll stick
to what I know
Slide 16
Slide 16 text
No content
Slide 17
Slide 17 text
• Well written & architected PHP framework by Taylor Otwell
• Makes working in PHP not terrible
• Uses Symfony2’s Components
• Contains a great Query Builder, Schema Builder & ORM
(Eloquent)
Slide 18
Slide 18 text
• Models, Collections, Views, Events, Routing for webapps
• Solid conventions for RESTful data syncing
• Flexible, easily extended, concise codebase
• Lightweight, doesn’t add features without a number of solid
real world use cases
• For the 90% use case
Slide 19
Slide 19 text
Hmm…
Slide 20
Slide 20 text
So if Backbone is a library with Models,
Collections, and Events in Javascript
...and Laravel has a nice Schema Builder, Query
Builder, and ORM in PHP
Knex Query Builder
• Use javascript statements to build SQL expressions
Slide 27
Slide 27 text
Knex Query Builder
• Query support for:
• joins, where, having, union, aggregates, insert,
update, delete, group by, order by, etc.
• knex.raw(sql, [bindings])
• Foundation of Bookshelf.js
Slide 28
Slide 28 text
Knex Query Builder
• Supports arbitrarily complex queries, with
proper raw & subquery interpolation
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
No content
Slide 31
Slide 31 text
No content
Slide 32
Slide 32 text
• Code to help create and edit the database
structure
• Maintains consistency between datatypes in
different query dialects
• Used in combination with migrations to keep
database properly structured
Knex Schema Builder
Slide 33
Slide 33 text
No content
Slide 34
Slide 34 text
Migrations
• Provides the ability to modify database structure
in a consistent / versioned way
• $ knex migrate:make migration-name
• $ knex migrate:latest
• $ knex migrate:rollback
Slide 35
Slide 35 text
Transactions
Slide 36
Slide 36 text
Transactions
Slide 37
Slide 37 text
Additional Features
• Events API
• “query” event
• “row” event (coming soon)
• Streams API
• knex.query.stream(options)
• knex.query.pipe(writableStream)
Slide 38
Slide 38 text
No content
Slide 39
Slide 39 text
Bookshelf - ORM
• Quick refresher:
• Object-Relational-Mapper
• Values in data-store (MySQL, PostgreSQL,
SQLite3) mapped to javascript objects (models)
or arrays of models (collections)
Slide 40
Slide 40 text
Bookshelf - ORM
• Takes care of standard SQL queries for you, especially
for common CRUD operations
• model.fetch()
• model.fetchAll()
• model.save()
• model.destroy()
Polymorphic Associations
• Mapping a single
model to multiple
model types
• Picture for employees,
for products
Slide 45
Slide 45 text
No content
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
Eager Loading
• Avoids the N+1 Query Problem
• Find a user with all associated accounts and for
each of those accounts find the associated
documents
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
No content
Slide 50
Slide 50 text
Eager Loading
•Avoids the N+1 Query Problem
•Find a user with all associated accounts and
for each of those accounts find the associated
documents, where the document status is
published
Slide 51
Slide 51 text
Dynamically constrain
relations
Slide 52
Slide 52 text
Eager Loading (.load())
Slide 53
Slide 53 text
“Tapping into” the query chain
• Allows for adding additional query parameters
dynamically to the model’s query
Slide 54
Slide 54 text
Callback Hell
Source: http://callbackhell.com/
Slide 55
Slide 55 text
Promises
• Promises aren’t about a better syntax than callbacks
• Promises are about error handling and making
async code feel synchronous (try / catch / finally)
• https://github.com/petkaantonov/bluebird#what-are-
promises-and-why-should-i-use-them
Knex.js
Connection
Pooling
Query
Builder
Schema
Builder
Query
Runner
Bookshelf.js
Relations
Base Model
& Collection
Query
Mapper
Lifecycle
Events
Slide 59
Slide 59 text
Knex.js
Connection
Pooling
Query
Builder
Schema
Builder
Query
Runner
Bookshelf.js
Relations
Base Model
& Collection
Query
Mapper
Lifecycle
Events
Active Record-ish Library
Scopes Validations
Automatic
Schema
Other Neat
Helpers
Slide 60
Slide 60 text
No content
Slide 61
Slide 61 text
In The Wild
Slide 62
Slide 62 text
Next Steps
• Finish up events API for Knex
• knex.introspect() / Auto-Migrations
• Refactoring internals of Bookshelf.js for more
configurable and cleaner relations
Slide 63
Slide 63 text
Next Steps
• Dropping Backbone dependency for Bookshelf.js
• Minor API changes to Bookshelf - .save(options)
• Integration with Checkit.js
• Better Examples