CouchRest Casted: Enable polymorphic CouchDB queries
Pull properly-casted documents from a CouchDB/CouchBase database, directly from CouchRest queries. Uses CouchRest::Model as a modelling framework.
Installation
(depends on couchrest and couchrest_model gems)
Gem
$ gem install couchrest_casted
Bundler
Bundler users can add this line to their Gemfile:
gem 'couchrest_casted'
Usage
The following is a simple example of using a CouchDB view to load documents of different CouchRest::Model classes, casted automatically:
require 'rubygems'
require 'couchrest_casted'
# connect to the CouchDB server
cr = CouchRest.new('http://localhost:5984')
DB = cr.database('casted_test')
# create the database
DB.recreate!
# define a couple of similar document models
class Service < CouchRest::Model::Base
use_database DB
property :name
end
class Person < CouchRest::Model::Base
use_database DB
property :name
end
# create some documents (services and users)
['Socialiting', 'Window Washing',
'Keynoting', 'Table Architecture'].each do |name|
Service.new(:name => name).save!
end
['Zark Muckerberg', 'Gill Bates',
'Jeve Stobs', 'Ellarry Lison'].each do |name|
Person.new(:name => name).save!
end
# a simple view that splits the 'name' field
# into words and emits one row per word
DB.save_doc({
"_id" => "_design/generic",
:views => {
:by_word => {
:map => " function(doc) {\n if (doc.name && doc.name.length > 0) {\n var words = doc.name.split(/\\\\W/);\n words.forEach(function(word){\n if (word.length > 0) emit(word, 1);\n });\n }\n }\n JS\n } } })\n\n# query the generic view\nrows = DB.casted_view('generic/by_word')['rows']\n\n# each returned document should be casted as the\n# correct CouchRest::Model class\nrows.each do |row|\n doc = row['doc']\n key = row['key']\n value = row['value']\n puts [doc.id, doc.class.to_s, doc.name, key, value].join(', ')\nend\n"
Contact
Bugs, suggestions, and such can be posted to https://github.com/m104/couchrest_casted/issues.