Class: CouchPotato::Database
- Inherits:
-
Object
- Object
- CouchPotato::Database
- Defined in:
- lib/couch_potato/database.rb
Defined Under Namespace
Classes: ValidationsFailedError
Instance Attribute Summary collapse
-
#cache ⇒ Object
Pass in a cache to enable caching #load calls.
-
#couchrest_database ⇒ Object
readonly
returns the underlying CouchRest::Database instance.
-
#name ⇒ Object
readonly
the (unresolved) name of the database unless this is the default database.
Instance Method Summary collapse
- #destroy_document(document) ⇒ Object (also: #destroy)
-
#first(spec) ⇒ Object
returns the first result from a #view query or nil.
-
#first!(spec) ⇒ Object
returns th first result from a #view or raises CouchPotato::NotFound.
-
#initialize(couchrest_database, name: nil) ⇒ Database
constructor
A new instance of Database.
-
#inspect ⇒ Object
:nodoc:.
-
#load!(id) ⇒ Object
loads one or more documents by its id(s) behaves like #load except it raises a CouchPotato::NotFound if any of the documents could not be found.
-
#load_document(id) ⇒ Object
(also: #load)
loads a document by its id(s) id - either a single id or an array of ids returns either a single document or an array of documents (if an array of ids was passed).
-
#save_document(document, validate = true, retries = 0, &block) ⇒ Object
(also: #save)
saves a document.
-
#save_document!(document) ⇒ Object
(also: #save!)
saves a document, raises a CouchPotato::Database::ValidationsFailedError on failure.
-
#switch_to(database_name) ⇒ Object
returns a new database instance connected to the CouchDB database with the given name.
-
#switch_to_default ⇒ Object
returns a new database instance connected to the default CouchDB database.
-
#view(spec) ⇒ Object
executes a view and return the results.
-
#view_in_batches(spec, batch_size: default_batch_size) ⇒ Object
Same as #view but instead of returning the results, it yields them to a given block in batches of the given size, making multiple requests with according skip/limit params sent to CouchDB.
Constructor Details
#initialize(couchrest_database, name: nil) ⇒ Database
Returns a new instance of Database.
13 14 15 16 |
# File 'lib/couch_potato/database.rb', line 13 def initialize(couchrest_database, name: nil) @name = name @couchrest_database = couchrest_database end |
Instance Attribute Details
#cache ⇒ Object
Pass in a cache to enable caching #load calls. A cache needs to respond to #[], #[]= and #clear (just use a Hash).
8 9 10 |
# File 'lib/couch_potato/database.rb', line 8 def cache @cache end |
#couchrest_database ⇒ Object (readonly)
returns the underlying CouchRest::Database instance
177 178 179 |
# File 'lib/couch_potato/database.rb', line 177 def couchrest_database @couchrest_database end |
#name ⇒ Object (readonly)
the (unresolved) name of the database unless this is the default database
11 12 13 |
# File 'lib/couch_potato/database.rb', line 11 def name @name end |
Instance Method Details
#destroy_document(document) ⇒ Object Also known as: destroy
130 131 132 133 134 135 136 137 |
# File 'lib/couch_potato/database.rb', line 130 def destroy_document(document) cache&.clear begin destroy_document_without_conflict_handling document rescue CouchRest::Conflict retry if document = document.reload end end |
#first(spec) ⇒ Object
returns the first result from a #view query or nil
95 96 97 98 |
# File 'lib/couch_potato/database.rb', line 95 def first(spec) spec.view_parameters = spec.view_parameters.merge({ limit: 1 }) view(spec).first end |
#first!(spec) ⇒ Object
returns th first result from a #view or raises CouchPotato::NotFound
101 102 103 |
# File 'lib/couch_potato/database.rb', line 101 def first!(spec) first(spec) || raise(CouchPotato::NotFound) end |
#inspect ⇒ Object
:nodoc:
172 173 174 |
# File 'lib/couch_potato/database.rb', line 172 def inspect #:nodoc: "#<CouchPotato::Database @root=\"#{couchrest_database.root}\">" end |
#load!(id) ⇒ Object
loads one or more documents by its id(s) behaves like #load except it raises a CouchPotato::NotFound if any of the documents could not be found
164 165 166 167 168 169 170 |
# File 'lib/couch_potato/database.rb', line 164 def load!(id) doc = load(id) missing_docs = id - doc.map(&:id) if id.is_a?(Array) raise(CouchPotato::NotFound, missing_docs.try(:join, ', ')) if doc.nil? || missing_docs.try(:any?) doc end |
#load_document(id) ⇒ Object Also known as: load
loads a document by its id(s) id - either a single id or an array of ids returns either a single document or an array of documents (if an array of ids was passed). returns nil if the single document could not be found. when passing an array and some documents could not be found these are omitted from the returned array
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/couch_potato/database.rb', line 145 def load_document(id) cached = cache && id.is_a?(String) && cache[id] if cache if cached ActiveSupport::Notifications.instrument('couch_potato.load.cached') do cached end else cache[id] = load_document_without_caching(id) cache[id] end else load_document_without_caching(id) end end |
#save_document(document, validate = true, retries = 0, &block) ⇒ Object Also known as: save
saves a document. returns true on success, false on failure. if passed a block will:
-
yield the object to be saved to the block and run if once before saving
-
on conflict: reload the document, run the block again and retry saving
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/couch_potato/database.rb', line 109 def save_document(document, validate = true, retries = 0, &block) cache&.clear begin block&.call document save_document_without_conflict_handling(document, validate) rescue CouchRest::Conflict if block handle_write_conflict document, validate, retries, &block else raise CouchPotato::Conflict end end end |
#save_document!(document) ⇒ Object Also known as: save!
saves a document, raises a CouchPotato::Database::ValidationsFailedError on failure
125 126 127 |
# File 'lib/couch_potato/database.rb', line 125 def save_document!(document) save_document(document) || raise(ValidationsFailedError, document.errors.) end |
#switch_to(database_name) ⇒ Object
returns a new database instance connected to the CouchDB database with the given name. the name is passed through the additional_databases configuration to resolve it to a database configured there. if the current database has a cache, the new database will receive a cleared copy of it.
185 186 187 188 189 190 191 |
# File 'lib/couch_potato/database.rb', line 185 def switch_to(database_name) resolved_database_name = CouchPotato.resolve_database_name(database_name) self .class .new(CouchPotato.couchrest_database_for_name(resolved_database_name), name: database_name) .tap(©_clear_cache_proc) end |
#switch_to_default ⇒ Object
returns a new database instance connected to the default CouchDB database. if the current database has a cache, the new database will receive a cleared copy of it.
196 197 198 199 200 201 |
# File 'lib/couch_potato/database.rb', line 196 def switch_to_default self .class .new(CouchPotato.couchrest_database) .tap(©_clear_cache_proc) end |
#view(spec) ⇒ Object
executes a view and return the results. you pass in a view spec which is usually a result of a SomePersistentClass.some_view call.
Example:
class User
include CouchPotato::Persistence
property :age
view :all, key: :age
end
db = CouchPotato.database
db.view(User.all) # => [user1, user2]
You can pass the usual parameters you can pass to a couchdb view to the view:
db.view(User.all(limit: 5, startkey: 2, reduce: false))
For your convenience when passing a hash with only a key parameter you can just pass in the value
db.view(User.all(key: 1)) == db.view(User.all(1))
Instead of passing a startkey and endkey you can pass in a key with a range:
db.view(User.all(key: 1..20)) == db.view(startkey: 1, endkey: 20) == db.view(User.all(1..20))
You can also pass in multiple keys:
db.view(User.all(keys: [1, 2, 3]))
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/couch_potato/database.rb', line 47 def view(spec) id = view_cache_id(spec) cached = cache && id.is_a?(String) && cache[id] if cache if cached ActiveSupport::Notifications.instrument('couch_potato.view.cached') do cached end else cache[id] = view_without_caching(spec) cache[id] end else view_without_caching(spec) end end |
#view_in_batches(spec, batch_size: default_batch_size) ⇒ Object
Same as #view but instead of returning the results, it yields them to a given block in batches of the given size, making multiple requests with according skip/limit params sent to CouchDB.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/couch_potato/database.rb', line 67 def view_in_batches(spec, batch_size: default_batch_size) rows = nil batch = 0 loop do spec.view_parameters = spec .view_parameters .merge({limit: batch_size}) .merge( if rows { startkey: rows&.last&.dig('key'), startkey_docid: rows&.last&.dig('id'), skip: 1 } else {} end ) result = raw_view(spec) rows = result['rows'] yield process_view_results(result, spec) break if rows.size < batch_size batch += 1 end end |