Skip to content

Commit 8c173f4

Browse files
committed
Add a query() classmethod to the Document class.
--HG-- extra : convert_revision : svn%3A7a298fb0-333a-0410-83e7-658617cd9cf3/trunk%4057
1 parent 173981f commit 8c173f4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

couchdb/schema.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,40 @@ def rev(self):
194194
rev = property(rev)
195195

196196
def load(cls, db, id):
197+
"""Load a specific document from the given database."""
197198
return cls.wrap(db.get(id))
198199
load = classmethod(load)
199200

200201
def store(self, db):
202+
"""Store the document in the given database."""
201203
if getattr(self._data, 'id', None) is None:
202204
docid = db.create(self._data)
203205
self._data = db.get(docid)
204206
else:
205207
db[self._data.id] = self._data
206208
return self
207209

210+
def query(cls, db, code, content_type='text/javascript', eager=False,
211+
**options):
212+
"""Execute a CouchDB temporary view and map the result values back to
213+
objects of this schema.
214+
215+
Note that by default, any properties of the document that are not
216+
included in the values of the view will be treated as if they were
217+
missing from the document. If you'd rather want to load the full
218+
document for every row, set the `eager` option to `True`, but note that
219+
this will initiate a new HTTP request for every document.
220+
"""
221+
def _wrapper(row):
222+
if eager:
223+
return cls.load(db, row.id)
224+
data = row.value
225+
data['_id'] = row.id
226+
return cls.wrap(data)
227+
return db.query(code, content_type=content_type, wrapper=_wrapper,
228+
**options)
229+
query = classmethod(query)
230+
208231
def view(cls, db, viewname, eager=False, **options):
209232
"""Execute a CouchDB named view and map the result values back to
210233
objects of this schema.

0 commit comments

Comments
 (0)