calvinmetcalf / crypto-pouch

plugin for encrypted pouchdb/couchdb databases
MIT License
243 stars 44 forks source link

Design document returning 404 on query, creation OK #56

Closed ranoble closed 3 years ago

ranoble commented 7 years ago

The plugin seems awesome, so lets start with that.

I put together the snippet below to show the issue (removed error handling for brevity).

I am using the browserify version, as this is running in browser.

Steps, create a DB, crypto it, add a document, add a design document view, and try and query it. {status: 404, name: "not_found", message: "missing", error: true, reason: "missing"}, and seems to indicate that the view can't be found.

Any ideas would be most welcome.

var designDoc = {
  _id: '_design/test4',
  views: {
    'view4': {
      map: function(doc) {
        emit(doc.type);
      }.toString()
    }
  }
};

  var dbName = 'db';
  db = new PouchDB(dbName);
  db.crypto('password');
  db.put({foo: 'bar', _id: 'baz', type: 'blah'}).then(function () {
    db.put(designDoc).then(function(){
        db.query('view4').then(function(results){
            console.log(results);
        });
    });
  });
calvinmetcalf commented 7 years ago

ah foo, looks like we don't have a test for designDocs

garbados commented 3 years ago

Howdy! It looks like this issue has been fixed. You can demonstrate it with this code:

const assert = require('assert').strict
const PouchDB = require('pouchdb')
PouchDB.plugin(require('crypto-pouch'))

const NAME = '.test'
const PASSWORD = 'hello world'
const DOC = { _id: 'a', hello: 'world' }
const DDOC = {
  _id: '_design/test',
  views: {
    test: {
      map: function(doc) {
        emit(doc.hello)
      }.toString()
    }
  }
}

const db = new PouchDB(NAME)
db.crypto(PASSWORD).then(async () => {
  await db.put(DOC)
  await db.put(DDOC)
  const result = await db.query('test')
  assert.equal(result.total_rows, 1)
  assert.equal(result.offset, 0)
  assert.equal(result.rows.length, 1)
  assert.equal(result.rows[0].key, 'world')
  assert.equal(result.rows[0].id, 'a')
  assert.equal(result.rows[0].value, null)
  console.log('ok')
}).catch((err) => {
  console.error(err)
}).then(async () => {
  await db.destroy()
})

Running this should print this result:

ok

Since it's been a few years, I'm going to close this issue for now. Let me know if you're still having trouble!