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

Nodevember - Making Relational Cool Again

Tim Griesser
November 16, 2014

Nodevember - Making Relational Cool Again

Tim Griesser

November 16, 2014
Tweet

More Decks by Tim Griesser

Other Decks in Technology

Transcript

  1. Node.js® is a platform built on Chrome's JavaScript runtime for

    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. 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…
  3. •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)
  4. • 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
  5. 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
  6. Other Builder features • Aggregate queries (min, max, sum) •

    Column, subquery aliasing ".as()" • Normalizes missing data in inserts • Tries hard to not let you screw up
  7. 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
  8. Schema Building • 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
  9. Migrations $ knex migrate:make migration-name $ knex migrate:latest $ knex

    migrate:rollback • Provides the ability to modify database structure in a consistent / versioned way
  10. Bookshelf - ORM • Quick refresher: • Object-Relational-Mapper • Values

    in data-store (MySQL, PostgreSQL, SQLite3) mapped to javascript objects (models) or arrays of models (collections)
  11. • Takes care of standard SQL queries for you, especially

    for common CRUD operations • model.fetch() • model.fetchAll() • model.save() • model.destroy() Object Relational Mapper
  12. Object Relational Mapper • Models common patterns of relational DB’s

    • One-to-One • One-to-Many • Many-to-Many
  13. Lifecycle events fetching creating updating saving destroying ! fetched created

    updated saved destroyed Useful for validations, logging, etc.
  14. Association Types • One-To-One • hasOne • belongsTo • One-To-Many

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

    • Many-To-Many • belongsToMany Association Types
  16. Association Types • One-To-One • hasOne • belongsTo • One-To-Many

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

    types • Picture for employees, for products
  18. .through • Creates a relation where a model can be

    selected through another model • Example, a “doctor” hasMany “appointments”, and an appointment hasOne “patient” • doctor.hasMany(Patients).through(Appointment)
  19. 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.
  20. 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.
  21. "Tapping into" the query chain Allows for adding additional query

    parameters dynamically to the model's query
  22. Transactions • Trickier when everything is async • Need to

    pass the same connection along to each query • 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
  23. •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 Promises
  24. Knex.js Connection Pooling Query Builder Schema Builder Query Runner Bookshelf.js

    Relations Base Model & Collection Query Mapper Lifecycle Events
  25. 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
  26. Next Steps: • Better debuggability, error types • knex.introspect() /

    Auto-Migrations • Built-in support for Postgres-specific features • Continue to refine Bookshelf's API