All That Jazz


Published on

An intro to JazzRecord ORM for JavaScript, an agnostic library for managing databases using an ActiveRecord implementation based on Rails

Published in: Technology, Business
  • Be the first to comment

All That Jazz

  1. All That Jazz An intro to Databases with JazzRecord ORM
  2. A Little About JazzRecord <ul><li>- Library agnostic (originally a MooTools project)   </li></ul><ul><li>- ORM inspired by ActiveRecord in Rails. Most behaviors directly portable.   </li></ul><ul><li>- Current Platforms: </li></ul><ul><ul><li>- Gears - AIR - Titanium PR1 (and beta thanks to @jhaynie!) </li></ul></ul>
  3. Core Features <ul><li>- Finders </li></ul><ul><li>- Validation </li></ul><ul><li>- Association loading </li></ul><ul><ul><li>- Preloading - Post load </li></ul></ul><ul><ul><li>  </li></ul></ul><ul><li>- Fixtures and Migrations </li></ul>
  4. Creating Models - Basics <ul><li>var JazzLegend = new Model({ table: &quot;jazz_legends&quot;, columns: { name: &quot;text&quot;, primary_instrument: &quot;text&quot;, years_active: &quot;text&quot; } }); </li></ul>
  5. Creating Models - Associations <ul><li>//new version of JazzLegend model JazzLegend = new JazzRecord.Model({ table: &quot;jazz_legends&quot;, foreignKey: &quot;jazz_legend_id&quot;, hasMany: {albums: &quot;albums&quot;}, columns: { name: &quot;text&quot;, primary_instrument: &quot;text&quot;, years_active: &quot;text&quot; } }); //associated model Album = new JazzRecord.Model({ table: &quot;albums&quot;, belongsTo: {artist: &quot;jazz_legends&quot;}, columns: { title: &quot;text&quot;, year: &quot;text&quot;, jazz_legend_id: &quot;number&quot; } }); </li></ul>
  6. Finders and Record Data <ul><li>var miles = JazzLegend.findBy(&quot;name&quot;, &quot;Miles&quot;); var louis = JazzLegend.first(); var dizzy = JazzLegend.find(2); miles.primary_instrument //trumpet louis.years_active // = &quot;Miles Dewey Davis III&quot;;; </li></ul><ul><li>  </li></ul>
  7. Finders and Record Data - Destroy <ul><li>dizzy.destroy(); //someone must've grown tired of Dizzy! Album.count(); //3 albums so far, all Miles Album.destroyAll(); //like that time you lost all your CDs// when your car was broken into... Album.count(); //0, no more Miles!! </li></ul>
  8. Validations <ul><li>var Programmer = new JazzRecord.Model({ //... validate: { atSave: function() { this.validatesIsString(&quot;name&quot;, &quot;You must have a name!&quot;); this.validatesIsFloat(&quot;income&quot;, &quot;We will gladly pay you a null salary!&quot;); } } }); </li></ul>- Callbacks run at time of create or save (or both) - Several built in validators (based on Rails') - Write your own
  9. Validation Errors <ul><li>p = Programmer.newRecord({name: 42,     income: &quot;spare change&quot;});; //false [&quot;You must have a name!&quot;]p.errors.income [&quot;We will gladly pay you null salary&quot;] = &quot;Nick&quot;; p.income = null;p.isValid() //true </li></ul>
  10. Association Preloading, Postloading <ul><li>- Defaulted to 1 level deep </li></ul><ul><li>- Can be overridden at global level or per query </li></ul><ul><li>- Immediately associated records loaded automatically </li></ul><ul><li>- Additional records loaded with call to load(assocName) </li></ul><ul><li>var milesRecords = miles.albums; //all of Miles' albums milesRecords.length //3 milesRecords[0].titlemilesRecords[0].load(&quot;artist&quot;) //miles again </li></ul>
  11. Autolinking <ul><li>miles.albums.push(     Album.newRecord({title: &quot;made up ish&quot;,         year: 2009     }) );; </li></ul>Any record objects pushed into a hasMany array or pushed directly onto a hasOne/belongsTo property will be auto linked on save of the base record object.
  12. Auto Unlinking <ul><li>  miles.albums.pop() //so long, fake; </li></ul>Any records that were previously associated/loaded which are removed become dissociated on save of base record object
  13. Future of JazzRecord <ul><li>- Unavoidable Asynchronous </li></ul><ul><li>- HTML5 Difficulties, deeply nested structures </li></ul><ul><li>- Will open up webOS, iPhone, async in AIR/Titanium </li></ul><ul><li>- Everything callback-based </li></ul><ul><li>- Initial work in 1 or 2 weeks - no association preloading </li></ul><ul><li>- No timetable for full preloading </li></ul><ul><li>- Need Contributors : coders, docs translators </li></ul>
  14. Project: Me: [email_address] thynctank (AIM/GTalk/IRC/etc)
  15. JazzFusion BYOM (bring your own model)
  16. Rails inspired, but dead simple <ul><li>Provide a series of controllers, view templates and helper code in a standard file structure and sit back to watch it work. </li></ul><ul><li>  </li></ul><ul><li>Initially targeting browsers, AIR, Titanium </li></ul><ul><li>Info on writing your own routing mechanism (remote templates, server-generated dynamic content) </li></ul><ul><li>Info on writing your own view template parsers. Out of box will allow inline script tags + simple string substitution, and/or markdown </li></ul>
  17. Some Nice Features <ul><ul><li>AppController's features inherited across all Controller objects </li></ul></ul><ul><ul><li>Automatic Hijax for forms/links, passing data to router </li></ul></ul><ul><ul><li>Before/after filters for keeping code DRY </li></ul></ul>
  18. When? Maybe June. That Burning Question