node.jsdatabasemigrationdatabase-migrationcloudant

List All Docs with @ibm-cloud/cloudant


So recently IBM deprecated @cloudant/cloudant and have moved on to @ibm-cloud/cloudant. I was in the process of doing a todo app to learn Cloudant along with some other things and so had to migrate to the new package.

enter image description here

As you can see from my Cloudant dashboard, I have a database with name "todo-vue-hapi" and there are 3 docs in the db. According to the migration docs to list all the documents before you would use db.list now you should use service.postDesignDocs so I connected everything and used the following code:

const response = await db.postDesignDocs({
  db: 'todo-vue-hapi',
})

This gives back a response object with the following result property:

{result: { total_rows: 0, offset: 0, rows: []}

The 3 docs are not there! I'm wondering how I can query everything in the database "todo-vue-hapi". Help with this issue would be greatly appreciated.


Solution

  • The postDesignDocs function only returns design documents, which are special Cloudant documents containing index definitions.

    To return all documents in a database you need to use the _all_docs API endpoint which is implemented as the postAllDocs function:

    const response = await db.postAllDocs({
      db: 'todo-vue-hapi',
      includeDocs: true,
      limit: 10
    })
    

    This should return you all documents in the database, including regular and design documents.