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

Making Relational Cool Again (or: JavaScript on...

Making Relational Cool Again (or: JavaScript on ACID)

JSConf 2015

Tim Griesser

May 27, 2015
Tweet

More Decks by Tim Griesser

Other Decks in Technology

Transcript

  1. SQL

  2. 42-

  3. 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…
  4. 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
  5. 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
  6. 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
  7. 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
  8. "Batteries Included" • Pooling • Schema Building • Migrations •

    Seeding • Callbacks (.asCallback) • Streams (.pipe, .stream) • Events • .toString()
  9. Object Relational Mapper • Takes care of standard SQL queries

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

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

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

    • Many-To-Many • belongsToMany Association Types
  13. • Mapping a single model to multiple model types •

    Picture for employees, for products Polymorphic Associations
  14. 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.
  15. 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.
  16. "Tapping into" the query chain Allows for adding additional query

    parameters dynamically to the model's query
  17. 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