Multiple Databases with Merb

2,580 views

Published on

CouchDB is a RESTful JSON eating beast of a document-oriented database. It is great for a lot of things, but traditional databases are a better choice in other places. DataMapper removes the need to choose by allowing easy integration of multiple databases in a single Merb app. I will explain the pros/cons of using CouchDB and show where it excels and when you can fall back to a regular database.

Multiple Databases with Merb

  1. 1. Using Multiple Databases in Merb With special guest CouchDB Wesley Beary | Software Engineer wesley@plinky.com
  2. 2. DataMapper! • database.yml maps first connection to the :default repository. • database.yml is a hash, each environment can have many keys. • Assign the primary database to the :default key and other databases to distinct meaningful keys (or not). Wesley Beary | Software Engineer | wesley@plinky.com
  3. 3. # TODO: use them? • run a specific command: repository(:key) { some_command(s) } or Foo.all(:repository => repository(:key)) • set a model’s storage location: class Bar def self.default_repository_name :key end end Wesley Beary | Software Engineer | wesley@plinky.com
  4. 4. Dear Sir or Madam, Why should I care? Thanks, The Audience Wesley Beary | Software Engineer | wesley@plinky.com
  5. 5. one_size.should_not fit(:all) Wesley Beary | Software Engineer | wesley@plinky.com
  6. 6. Example, Pretty Please? • MySQL • CouchDB • +/- schema based • +/- schema-less • + complex joins/queries • + map/reduce • + established security • - security TBA • - not good at blobs • + good at blobs Wesley Beary | Software Engineer | wesley@plinky.com
  7. 7. CouchDB? • RESTful • javascript queries • json data • file attachments Wesley Beary | Software Engineer | wesley@plinky.com
  8. 8. How about a use case? Muddle: merb + couchdb == tumblelog Wesley Beary | Software Engineer | wesley@plinky.com
  9. 9. STI: Icky Tables • Single table inheritance gets gross. • Usually the fields for the parent and every child go on EVERY record. • CouchDB lets you store just what you need on a case by case basis. Wesley Beary | Software Engineer | wesley@plinky.com
  10. 10. Tumbling • Each posts may have vastly different content/fields from other posts. • There are many different child classes derived from Post. • Some posts have associated files, attachments help keep it organized. Wesley Beary | Software Engineer | wesley@plinky.com
  11. 11. Random Posts Wesley Beary | Software Engineer | wesley@plinky.com
  12. 12. Direct From CouchDB! Wesley Beary | Software Engineer | wesley@plinky.com
  13. 13. Wesley Beary | Software Engineer | wesley@plinky.com
  14. 14. Details? • User data is safely stored in mysql. • Files save and load from CouchDB. • Mostly a proof of concept, needs work to flesh out the details still. • No load/performance testing just yet, hard to say how production-worthy parts might be. Wesley Beary | Software Engineer | wesley@plinky.com
  15. 15. Caution Associations Ahead • define cross repository belongs repository(:key) do belongs_to :model end • define cross repository has has n, :models, :repository => repository(:key) Wesley Beary | Software Engineer | wesley@plinky.com
  16. 16. Play along at home! http://github.com/monki/muddle Wesley Beary | Software Engineer | wesley@plinky.com
  17. 17. Questions? http://github.com/monki/muddle Wesley Beary | Software Engineer | wesley@plinky.com

×