Class CouchPotato::Database
In: lib/couch_potato/database.rb
Parent: Object

Methods

Classes and Modules

Class CouchPotato::Database::ValidationsFailedError

Public Class methods

Public Instance methods

returns the underlying CouchRest::Database instance

destroy(document)

Alias for destroy_document

returns the first result from a view query or nil

returns th first result from a view or raises CouchPotato::NotFound

load(id)

Alias for load_document

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

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

save(document, validate = true, retries = 0, &block)

Alias for save_document

save!(document)

Alias for save_document!

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

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]))

[Validate]