couchdbcouchdb-nanocouchdb-fauxton

What happens in CouchDB when I create an index repeatedly?


To implement sorting, in CouchDB we have to create an index (otherwise the corresponding mango query fails). I haven't found a way to do this in Fauxton (if I have missed something, please comment in Github), so I've decided to create it programmatically. As I'm using couchdb-nano, I've added:

this.clientAuthPromise.then(async () => {
    try {
        await this.client.use('test_polling_storage').createIndex({
            index: {
                fields: [
                    'isoDate',
                ],
            },
            name: 'test_polling_storage--time_index',
        })
        console.log('index created?')
    } catch (error) {
        console.log(`failed to create index:`, error)
    }
})

into the storage class constructor, where

this.clientAuthPromise = this.client.auth(connectionParams.auth.user, connectionParams.auth.password)

Now, on each run of the server, I'm getting index created?, so the createIndex method (which presumably POSTs to /db/_index) doesn't fail (and sorting works, too). But as I haven't found indexes viewer in Fauxton either, I wonder what actually happens on each call of createIndex: does it create a new index? Does it rebuild the index? Or sees that the index with such name already exists and doesn't do anything? It's annoying to deal with this in a blind fashion, so please clarify or suggest a way to clarify.


Solution

  • Ok, as the docs suggest that the response will contain "created" or "exists", I've tried

    const result = await this.client.use('test_polling_storage').createIndex({
    ...
    console.log('index created?', result.result)
    

    got index created? exists and concluded that if the index was created before, it won't be re-created. It's not clear what will happen if I try to change the index, but at least now I have a mean to find out.