Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documents bulk retrieval #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/couchrest/model/document_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ def get!(id, db = database)
end
alias :find! :get!

# Load an array of documents from the database by an arrary of ids
# No exceptions will be raised if none of the documents were found
#
# ==== Returns
# [Object::] if the document was found
# or
# []
#
# === Parameters
# ids<Array>:: Documents IDs
# db<Database>:: optional option to pass a custom database to use
def get_bulk(ids, db = database)
get_bulk!(ids, db)
rescue CouchRest::Model::DocumentsNotFound
[]
end
alias :find_all :get_bulk

# Load an array of documents from the database by an arrary of ids
# An exception will be raised if none of the documents were found
#
# ==== Returns
# [Object::] if the document was found
# or
# Exception
#
# === Parameters
# ids<Array>:: Documents IDs
# db<Database>:: optional option to pass a custom database to use
def get_bulk!(ids, db = database)
docs = db.all_docs(:keys => ids, :include_docs => true)
raise CouchRest::Model::DocumentsNotFound if docs.blank?
docs["rows"].map{|row| build_from_database(row["doc"])}
end
alias :find_all! :get_bulk!

end

end
Expand Down
2 changes: 2 additions & 0 deletions lib/couchrest/model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(document)

class DocumentNotFound < Errors::CouchRestModelError; end

class DocumentsNotFound < Errors::CouchRestModelError; end

class DatabaseNotDefined < Errors::CouchRestModelError
def initialize(msg = nil)
msg ||= "Database must be defined in model or view!"
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
saved_obj.set_by_proc.should be_an_instance_of(Time)
end
end

describe "bulk retrieving" do
it "should retrieve multiple documents given an array of ids" do
@obj.name = "first object"
@obj.save!
@obj_2 = WithDefaultValues.new
@obj_2.name = "second object"
@obj_2.save!
saved_objs = WithDefaultValues.find_all([@obj.id, @obj_2.id])
saved_objs.first.name.should eq("first object")
saved_objs.last.name.should eq("second object")
saved_objs.length.should eq(2)
end
end

describe "creating a model" do

Expand Down