Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making Relational Cool Again

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Making Relational Cool Again

From the June 30th #BostonJS in the park

Tim Griesser

July 01, 2014

Transcript

64 slides
  1. ...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.
  2. • 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:
  3. • 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
  4. • 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)
  5. • 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
  6. 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
  7. 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
  8. • 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
  9. Migrations • Provides the ability to modify database structure in

    a consistent / versioned way • $ knex migrate:make migration-name • $ knex migrate:latest • $ knex migrate:rollback
  10. Additional Features • Events API • “query” event • “row”

    event (coming soon) • Streams API • knex.query.stream(options) • knex.query.pipe(writableStream)
  11. Bookshelf - ORM • Quick refresher: • Object-Relational-Mapper • Values

    in data-store (MySQL, PostgreSQL, SQLite3) mapped to javascript objects (models) or arrays of models (collections)
  12. Bookshelf - ORM • Takes care of standard SQL queries

    for you, especially for common CRUD operations • model.fetch() • model.fetchAll() • model.save() • model.destroy()
  13. Association Types • One-To-One • hasOne • belongsTo • One-To-Many

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

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

    • hasMany • Many-To-Many • belongsToMany
  16. Polymorphic Associations • Mapping a single model to multiple model

    types • Picture for employees, for products
  17. 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
  18. 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
  19. “Tapping into” the query chain • Allows for adding additional

    query parameters dynamically to the model’s query
  20. 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
  21. Knex.js Connection Pooling Query Builder Schema Builder Query Runner Bookshelf.js

    Relations Base Model & Collection Query Mapper Lifecycle Events
  22. 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
  23. Next Steps • Finish up events API for Knex •

    knex.introspect() / Auto-Migrations • Refactoring internals of Bookshelf.js for more configurable and cleaner relations
  24. Next Steps • Dropping Backbone dependency for Bookshelf.js • Minor

    API changes to Bookshelf - .save(options) • Integration with Checkit.js • Better Examples

More Decks by Tim Griesser

Other Decks in Programming