Slide 1

Slide 1 text

Making Relational Cool Again

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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.

Slide 6

Slide 6 text

... it’s really good for webapps!

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

What about relational?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Sequelize node-orm2 node-mysql node-pg node-sqlite3 node-mariasql JugglinDB

Slide 13

Slide 13 text

• 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

Slide 21

Slide 21 text

could we combine them?

Slide 22

Slide 22 text

Query builder Eloquent Promises Models, Collections, Events

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

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()

Slide 41

Slide 41 text

Association Types • One-To-One • hasOne • belongsTo • One-To-Many • hasMany • Many-To-Many • belongsToMany

Slide 42

Slide 42 text

Association Types • One-To-One • hasOne • belongsTo • One-To-Many • hasMany • Many-To-Many • belongsToMany

Slide 43

Slide 43 text

Association Types • One-To-One • hasOne • belongsTo • One-To-Many • hasMany • Many-To-Many • belongsToMany

Slide 44

Slide 44 text

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

Slide 56

Slide 56 text

Promises

Slide 57

Slide 57 text

Knex.js Connection Pooling Query Builder Schema Builder Query Runner

Slide 58

Slide 58 text

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

Slide 64

Slide 64 text

Thanks • Freenode IRC: #bookshelf • Twitter @tgriesser • http://knexjs.org • http://bookshelfjs.org