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

getAllDocuments() queryObj with 'keys'

Open piwo1984 opened this issue 6 years ago • 2 comments

I'm trying to use getAllDocuments('my_db', {keys: ['key1', 'key2']}). The keys array is an array containing several keys. I'm struggeling with the serialization of this array. How does it look like?

piwo1984 avatar Aug 09 '18 13:08 piwo1984

$ cat foo.js

const db = require('couchdb-promises')({
  baseUrl: 'http://localhost:5984'
})
const dbName = 'testdb'

db.createDatabase(dbName)
.then(() => db.createDocument(dbName, {name: 'Bob'}, 'doc1'))
.then(() => db.createDocument(dbName, {name: 'Alice'}, 'doc2'))
.then(() => db.createDocument(dbName, {name: 'Jane'}, 'doc3'))
.then(() => db.createDocument(dbName, {name: 'Kate'}, 'doc4'))
.then(() => db.getAllDocuments(dbName, {
  include_docs: true,
  keys: ['doc1', 'doc3']
}))
.then(response => { console.log(response.data) })
.then(() => db.deleteDatabase(dbName))
.catch(console.error)

$ node foo.js

{ total_rows: 4,
  offset: null,
  rows: 
   [ { id: 'doc1', key: 'doc1', value: [Object], doc: [Object] },
     { id: 'doc3', key: 'doc3', value: [Object], doc: [Object] } ] }

pmorjan avatar Aug 09 '18 17:08 pmorjan

Thanks for thte hint. But this is exactly the same what I've done before. Haven't mentioned it - sorry. I've tryed your code within my tests and get the same error message: Error: request timed out. No matter how big the requestTimeout is. I trust in you, that this code is working. So this error is triggered somewhere else. Maybe it relates to PouchDB, which I use for my tests, and this issue: https://github.com/pmorjan/couchdb-promises/issues/8

If I use this code it is working as expected:

let couch = RestifyClients.createJsonClient({
        url: baseUrl,
        requestTimeout: 10000
      });
      couch.post(`/${dbName}/_all_docs?include_docs=true`, {keys: idArray}, function (err, req, res, obj) {
        if (err) { return reject(err); }
        let resultObj = {};
        obj.rows.forEach(row => {
          if (row.doc) {
            resultObj[row.doc._id] = row.doc;
          }
        });
        return resolve(resultObj);
      });

piwo1984 avatar Aug 10 '18 05:08 piwo1984