@@ -256,8 +256,7 @@ def __setitem__(self, id, content):
256
256
documents
257
257
"""
258
258
result = self .resource .put (id , content = content )
259
- content ['_id' ] = result ['id' ]
260
- content ['_rev' ] = result ['rev' ]
259
+ content .update ({'_id' : result ['id' ], '_rev' : result ['rev' ]})
261
260
262
261
def _get_name (self ):
263
262
if self ._name is None :
@@ -301,7 +300,7 @@ def get(self, id, default=None, **options):
301
300
return default
302
301
return Document (row )
303
302
304
- def query (self , code , ** options ):
303
+ def query (self , code , content_type = 'text/javascript' , ** options ):
305
304
"""Execute an ad-hoc query against the database.
306
305
307
306
>>> server = Server('http://localhost:8888/')
@@ -330,17 +329,56 @@ def query(self, code, **options):
330
329
>>> del server['python-tests']
331
330
332
331
:param code: the code of the view function
332
+ :param content_type: the MIME type of the code, which determines the
333
+ language the view function is written in
333
334
:return: an iterable over the resulting `Row` objects
334
335
:rtype: ``generator``
335
336
"""
336
337
for name , value in options .items ():
337
338
if name in ('key' , 'startkey' , 'endkey' ) \
338
339
or not isinstance (value , basestring ):
339
340
options [name ] = json .dumps (value )
340
- data = self .resource .post ('_temp_view' , content = code , ** options )
341
+ headers = {}
342
+ if content_type :
343
+ headers ['Content-Type' ] = content_type
344
+ data = self .resource .post ('_temp_view' , content = code , headers = headers ,
345
+ ** options )
341
346
for row in data ['rows' ]:
342
347
yield Row (row ['id' ], row ['key' ], row ['value' ])
343
348
349
+ def update (self , documents ):
350
+ """Perform a bulk update or insertion of the given documents using a
351
+ single HTTP request.
352
+
353
+ >>> server = Server('http://localhost:8888/')
354
+ >>> db = server.create('python-tests')
355
+ >>> for doc in db.update([
356
+ ... Document(type='Person', name='John Doe'),
357
+ ... Document(type='Person', name='Mary Jane'),
358
+ ... Document(type='City', name='Gotham City')
359
+ ... ]):
360
+ ... print repr(doc) #doctest: +ELLIPSIS
361
+ <Document u'...'@u'...' {'type': 'Person', 'name': 'John Doe'}>
362
+ <Document u'...'@u'...' {'type': 'Person', 'name': 'Mary Jane'}>
363
+ <Document u'...'@u'...' {'type': 'City', 'name': 'Gotham City'}>
364
+
365
+ >>> del server['python-tests']
366
+
367
+ :param documents: a sequence of dictionaries or `Document` objects
368
+ :return: an iterable over the resulting documents
369
+ :rtype: ``generator``
370
+
371
+ :since: version 0.2
372
+ """
373
+ documents = list (documents )
374
+ content = {'_bulk_docs' : documents }
375
+ data = self .resource .post (content = content )
376
+ for idx , result in enumerate (data ['results' ]):
377
+ assert 'ok' in result # FIXME: how should error handling work here?
378
+ doc = documents [idx ]
379
+ doc .update ({'_id' : result ['id' ], '_rev' : result ['rev' ]})
380
+ yield doc
381
+
344
382
def view (self , name , ** options ):
345
383
"""Execute a predefined view.
346
384
@@ -369,8 +407,8 @@ class Document(dict):
369
407
`id` and `rev`, which contain the document ID and revision, respectively.
370
408
"""
371
409
372
- def __init__ (self , content ):
373
- dict .__init__ (self , content )
410
+ def __init__ (self , * args , ** kwargs ):
411
+ dict .__init__ (self , * args , ** kwargs )
374
412
375
413
def __repr__ (self ):
376
414
return '<%s %r@%r %r>' % (type (self ).__name__ , self .id , self .rev ,
0 commit comments