Страници

събота, 6 март 2010 г.

CouchDB EAV notes

Here are my notes about the EAV problem

=begin
function(doc) {
for (property in doc.properties) {
emit([property, doc.properties[property]], doc._id)
}
}
function(keys, values, rereduce) {
if(rereduce) {
return union(values)
}

return values
}
=end


You have to turn the warning off in the couchdb conf for the reduce function in order to get this to work.

require 'rubygems'
require 'couchrest'
SERVER = CouchRest.new
DB = SERVER.database('re')

class Offer < CouchRest::ExtendedDocument
use_database DB
property :type
property :properties
# property :url
# property :title
# property :street
# property :square
# property :zip
# property :floor
# property :heating
# property :age
timestamps!

view_by :title
end


100.times do |i|
offer = Offer.new(
:type => rand(5),
:_id => "#{i}",
:properties => {
:url => Faker::Internet.domain_name,
:title => Faker::Name.first_name,
:street => Faker::Address.street_address,
:zip => Faker::Address.zip_code,
:heating => ["Solar", "Gaz", "Electiricty", "Coals", "Oil"].rand,
:floor => rand(6),
:age => rand(20),
}
)
offer.save
end

--- Some notes not related to EAV
DB.view('m/all')['rows'].inspect

all_view = {
:map = 'function(doc) { if(doc["couchrest-type"] == "Bookmark") emit(doc._id, doc); }'
}
DB.delete_doc DB.get("_design/bookmark") rescue nil
DB.save_doc({
"_id" => "_design/bookmark",
:views => {
:all => all_view
}
})
puts DB.view('bookmark/all')['rows'].inspect

Няма коментари:

Публикуване на коментар