@@ -194,17 +194,40 @@ def rev(self):
194
194
rev = property (rev )
195
195
196
196
def load (cls , db , id ):
197
+ """Load a specific document from the given database."""
197
198
return cls .wrap (db .get (id ))
198
199
load = classmethod (load )
199
200
200
201
def store (self , db ):
202
+ """Store the document in the given database."""
201
203
if getattr (self ._data , 'id' , None ) is None :
202
204
docid = db .create (self ._data )
203
205
self ._data = db .get (docid )
204
206
else :
205
207
db [self ._data .id ] = self ._data
206
208
return self
207
209
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
+
208
231
def view (cls , db , viewname , eager = False , ** options ):
209
232
"""Execute a CouchDB named view and map the result values back to
210
233
objects of this schema.
0 commit comments