pouchdb-adapter-leveldown does not assure folders

Created on 10 Sep 2016  路  5Comments  路  Source: pouchdb/pouchdb

Issue

I would assume that if I create a new database containing a path separator like new PouchDB('foo/bar') then it鈥檚 the responsibility of the adapters to make sure that the foo folder gets created before it tries to write files, but it doesn鈥檛 seem to be the case right now?

Would you accept a PR to fix that, or is that by design?

Info

  • Environment: Node.js
  • PouchDB Version: 6.0.4
  • Adapter: LevelDB

    Reproduce

var PouchDB = require('pouchdb-core').plugin(require('pouchdb-adapter-leveldb'))
new PouchDB('bar/baz').info().catch(console.log)
// OpenError: IO error: bar/baz/LOCK: No such file or directory
//     at ./node_modules/levelup/lib/levelup.js:119:34
//     at ./node_modules/leveldown/node_modules/abstract-leveldown/abstract-leveldown.js:39:16

Workaround

var pathResolve = require('path').resolve
var mkdirp = require('mkdirp')
var PouchDB = require('pouchdb-core')
  .plugin(require('pouchdb-adapter-leveldb'))
  .defaults({prefix: 'path/to/data/'})

var db = new state.PouchDB(name)
db.info()
.catch(function (error) {
  if (error.name === 'OpenError') {
    var path = db.__opts.prefix ? db.__opts.prefix + name : name

    return new Promise(function (resolve, reject) {
      mkdirp(pathResolve(path), function (error) {
        if (error) {
          return reject(error)
        }

        resolve()
      })
    })

    .then(function () {
      // reusing the db instance from above does not work for me
      return new state.PouchDB(name).info()
    })
  }

  throw error
})

Most helpful comment

I don't see any reason not to do the mkdirp. :+1:

All 5 comments

I don't see any reason not to do the mkdirp. :+1:

It has come up a few times before but I purposefully left it out, I think its the responsibility of the backend to ensure this type of functionality, in particular whoever chooses where to store the data should be the one to ensure that it exists, otherwise we will be creating directories when not needed / things go out of sync etc

I think added support for this should be done in leveldown, or at least we should understand why leveldown doesnt do it (maybe it could be an option we set by default)

Yeah I'm also fine with just punting on this. However I think a defensive mkdirp is pretty costless.

It has come up a few times before but I purposefully left it out, I think its the responsibility of the backend to ensure this type of functionality, in particular whoever chooses where to store the data should be the one to ensure that it exists, otherwise we will be creating directories when not needed / things go out of sync etc

On second thought, I disagree when it comes to database names with with a /. This is not about the prefix option, it鈥檚 when people want to create a database like user/uuid567. CouchDB will also create a folder user in that case and a uuid567.couch file inside it. I can鈥檛 think of a case where we would create directories when not needed?

I agree that we should investigate why this is not part of leveldown though

Users can do this fairly easily themselves, not having to include mkdirp is one less dependency for us, gonna close for now

Was this page helpful?
0 / 5 - 0 ratings