pythonflaskcouchdbpython-cloudant

How to create views with cloudant


In docs https://python-cloudant.readthedocs.io/en/latest/database.html show how to create_documents and database, but doesn't show how to create views

can somebody help me?. I'm using cloudant with python(Flask)...

class TestContext(unittest.TestCase):
def setUp(self):
    self.client_couchdb = CouchDB(
        user='admin',
        auth_token='token123',
        url='https://couchbk.123',
        connect=True
    )

    self.doc_test = {
        '_id': 'julia102',
        'name': 'Julia',
        'age': 30,
        'type': 'event'
    }

    self.db = self.client_couchdb.create_database('test')
    self.db.create_document(self.doc_test)

Solution

  • Thanks to Alexis https://stackoverflow.com/users/5236185/alexis-c%C3%B4t%C3%A9

    that's the right solution:

    https://python-cloudant.readthedocs.io/en/latest/design_document.html#cloudant.design_document.DesignDocument.add_view

    That's the dirty solution

        self.new_view = {
            '_id': '_design/myname',
            '_rev': 'rev-code',
            'views': {
                'by_client': {
                    'map': '''function (doc) {\nif(
                        doc.type === "myname" && doc.client_id
                    ){\nemit(doc.client_id);\n}\n}
                '''
                }
            },
            'language': 'javascript'
        }
    
        # create new view like a doc
        self.db.create_document(self.new_view)