multi-key support for views, temp-views, and alldocs
This commit is contained in:
parent
d7e3c7931f
commit
e2f7163816
3 changed files with 36 additions and 13 deletions
|
@ -108,8 +108,8 @@ module CouchRest
|
|||
JSON.parse(RestClient.delete(uri))
|
||||
end
|
||||
|
||||
def paramify_url url, params = nil
|
||||
if params
|
||||
def paramify_url url, params = {}
|
||||
if params && !params.empty?
|
||||
query = params.collect do |k,v|
|
||||
v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
|
||||
"#{k}=#{CGI.escape(v.to_s)}"
|
||||
|
|
|
@ -29,21 +29,33 @@ module CouchRest
|
|||
end
|
||||
|
||||
# Query the <tt>_all_docs</tt> view. Accepts all the same arguments as view.
|
||||
def documents params = nil
|
||||
def documents params = {}
|
||||
keys = params.delete(:keys)
|
||||
url = CouchRest.paramify_url "#{@root}/_all_docs", params
|
||||
CouchRest.get url
|
||||
if keys
|
||||
CouchRest.post(url, {:keys => keys})
|
||||
else
|
||||
CouchRest.get url
|
||||
end
|
||||
end
|
||||
|
||||
# POST a temporary view function to CouchDB for querying. This is not recommended, as you don't get any performance benefit from CouchDB's materialized views. Can be quite slow on large databases.
|
||||
def temp_view funcs, params = nil
|
||||
def temp_view funcs, params = {}
|
||||
keys = params.delete(:keys)
|
||||
funcs = funcs.merge({:keys => keys}) if keys
|
||||
url = CouchRest.paramify_url "#{@root}/_temp_view", params
|
||||
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
|
||||
end
|
||||
|
||||
# Query a CouchDB view as defined by a <tt>_design</tt> document. Accepts paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi
|
||||
def view name, params = nil
|
||||
def view name, params = {}
|
||||
keys = params.delete(:keys)
|
||||
url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
|
||||
CouchRest.get url
|
||||
if keys
|
||||
CouchRest.post(url, {:keys => keys})
|
||||
else
|
||||
CouchRest.get url
|
||||
end
|
||||
end
|
||||
|
||||
# GET a document from CouchDB, by id. Returns a Ruby Hash.
|
||||
|
|
|
@ -22,17 +22,21 @@ describe CouchRest::Database do
|
|||
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
||||
end
|
||||
it "should work with a range" do
|
||||
rs = @db.temp_view(@temp_view,{:startkey => "b", :endkey => "z"})
|
||||
rs = @db.temp_view(@temp_view, :startkey => "b", :endkey => "z")
|
||||
rs['rows'].length.should == 2
|
||||
end
|
||||
it "should work with a key" do
|
||||
rs = @db.temp_view(@temp_view,{:key => "wild"})
|
||||
rs = @db.temp_view(@temp_view, :key => "wild")
|
||||
rs['rows'].length.should == 1
|
||||
end
|
||||
it "should work with a count" do
|
||||
rs = @db.temp_view(@temp_view,{:count => 1})
|
||||
rs = @db.temp_view(@temp_view, :count => 1)
|
||||
rs['rows'].length.should == 1
|
||||
end
|
||||
it "should work with multi-keys" do
|
||||
rs = @db.temp_view(@temp_view, :keys => ["another", "wild"])
|
||||
rs['rows'].length.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "map/reduce query with _temp_view in Javascript" do
|
||||
|
@ -98,17 +102,21 @@ describe CouchRest::Database do
|
|||
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
||||
end
|
||||
it "should work with a range" do
|
||||
rs = @db.view('first/test',{:startkey => "b", :endkey => "z"})
|
||||
rs = @db.view('first/test', :startkey => "b", :endkey => "z")
|
||||
rs['rows'].length.should == 2
|
||||
end
|
||||
it "should work with a key" do
|
||||
rs = @db.view('first/test',{:key => "wild"})
|
||||
rs = @db.view('first/test', :key => "wild")
|
||||
rs['rows'].length.should == 1
|
||||
end
|
||||
it "should work with a count" do
|
||||
rs = @db.view('first/test',{:count => 1})
|
||||
rs = @db.view('first/test', :count => 1)
|
||||
rs['rows'].length.should == 1
|
||||
end
|
||||
it "should work with multi-keys" do
|
||||
rs = @db.view('first/test', :keys => ["another", "wild"])
|
||||
rs['rows'].length.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET (document by id) when the doc exists" do
|
||||
|
@ -426,6 +434,9 @@ describe CouchRest::Database do
|
|||
ds['rows'].length.should == 4
|
||||
ds = @db.documents(:key => 'doc0')
|
||||
ds['rows'].length.should == 1
|
||||
|
||||
rs = @db.documents :keys => ["doc0", "doc7"]
|
||||
rs['rows'].length.should == 2
|
||||
end
|
||||
|
||||
describe "deleting a database" do
|
||||
|
|
Loading…
Reference in a new issue