Package couchable :: Module core :: Class CouchableDb
[frames] | no frames]

Class CouchableDb

source code

object --+
         |
        CouchableDb

Currently, though it is not documented here, the .db parameter is part of the public API of CouchableDb; it is required for use with views, etc. Please see the couchdb documentation for details:

http://packages.python.org/CouchDB/

It is possible that in the future couchdbkit could also be used:

http://couchdbkit.org/

Instance Methods
 
__init__(self, name=None, url='http://localhost:5984/', db=None)
Creates a CouchableDb wrapper around a couchdb.Database object.
source code
str
addClassView(self, cls, name, keys=None, multikeys=None, value='1', reduce=None)
Creates a view that only emits records for documents of the specified class.
source code
 
__deepcopy__(self, memo) source code
str or list
store(self, what)
Stores the documents in the what parameter in CouchDB.
source code
obj or list
load(self, what, loaded=None)
Loads the indicated object(s) out of CouchDB.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, name=None, url='http://localhost:5984/', db=None)
(Constructor)

source code 

Creates a CouchableDb wrapper around a couchdb.Database object. If the database does not yet exist, it will be created.

Parameters:
  • name (str) - Name of the CouchDB database to connect to.
  • url (str) - The URL of the CouchDB server. Uses the couchdb default of http://localhost:5984/
  • db (couchdb.Database) - An instance of couchdb.Database that has already been instantiated. Overrides the name and url params.
Overrides: object.__init__

addClassView(self, cls, name, keys=None, multikeys=None, value='1', reduce=None)

source code 

Creates a view that only emits records for documents of the specified class. Each record also emits keys based on the parameters given, which can be used for things like "get all Foo instances with bar between 3 and 7."

The view code resembles the following:

   function(doc) {
       if ('couchable:' in doc) {
           var info = doc['couchable:'];
           if (info.module == '$module' && info.class == '$cls') {
               $emit
           }
       }
   }

This behavior may change during the course of the 0.x.x series of releases.

Parameters:
  • cls (type) - The class of objects that the view should be restricted to. Note that sub/superclasses are not considered.
  • name (string) - The string to suffix the name of the view with (byclass:module.class:name).
  • keys (list of strings) - A list of unescaped javascript expressions to use as the key for the view.
  • multikeys (list of list of strings) - A list of keys (see above). Each key will get a separate emit.
  • value (string) - A string of unescaped javascript used as the value of each emit.
  • reduce (string) - A CouchDB reduce function. Can be None, javascript, or the built-in '_sum' kind of reduce function.
Returns: str
The full name of the view (byclass:module.class:name).

store(self, what)

source code 

Stores the documents in the what parameter in CouchDB. If a ._id does not yet exist on the object, it will be added. If the ._id is present, it will be used instead. The ._rev of the object(s) must match what is already in the database.

Any attachments for the document will also be uploaded. As of the current revision (0.0.1b2), each attachment will be uploaded each time the document is stored.

This behavior is expected to change during the course of the 0.x.x series of releases.

Any objects referenced by the object(s) in what will also be stored. If those objects are registered as document types, then they will also be stored as top level objects, even if they exist in the database already, and have not changed.

This behavior may change during the course of the 0.x.x series of releases.

Any cycles comprised entirely of non-document classes will cause the store call to raise an exception. Cycles where at least one object in the cycle is to be stored as a top-level document are fine.

This behavior may change during the course of the 0.x.x series of releases.

Parameters:
  • what (obj or list) - The object or list of objects to store in CouchDB.
Returns: str or list
The ._id of the what parameter, or the list of such IDs if what was a list.

load(self, what, loaded=None)

source code 

Loads the indicated object(s) out of CouchDB.

Loading an ID multiple times will result in getting the same object returned each time. Subsequent loads will return the same object again, but with an updated __dict__. Note that this means it is impossible to have both the current version of the object and an older revision loaded at the same time.

Behavior of loading old document revisions is untested at this time.

If what is a dict or a couchdb.client.Row, then the values will be used from that object rather than re-fetching from the database. Likewise, the loaded parameter can be used to prevent multiple DB hits. This can be useful when loading multiple documents returned by a view, etc.

Example use:

   cdb.load(cdb.db.view('couchable/' + viewName, include_docs=True, startkey=[...], endkey=[..., {}]).rows)
Parameters:
  • what (str, dict, couchdb.client.Row or list of same) - A document _id, a dict with an '_id' key, a couchdb.client.Row instance, or a list of any of the preceding.
  • loaded (dict, couchdb.client.Row or list of same) - A mapping of document _ids to documents that have already been loaded out of the database.
Returns: obj or list
The object indicated by the what parameter, or a list of such objects if what was a list.