couchdb-python icon indicating copy to clipboard operation
couchdb-python copied to clipboard

Wrapper Decorator for Reduce function

Open twheys opened this issue 7 years ago • 0 comments

Fixes #105

Added a mutator function for the ViewField decorator which allows a reduce function to be set on a ViewField using a second decorator. This is just some sugar for creation a view with a map and reduce function using an explicit syntax.

Example

class Item(Document):
     sku = TextField()
     color = IntegerField()

     stock = ViewField('people')

     @stock.map
     def map(doc):
         yield doc['sku'], doc

     @stock.reduce
     def reduce(keys, values, rereduce):
         if rereduce:
             yield sum(values)
         else:
             yield len(values)

twheys avatar Jul 16 '17 21:07 twheys