From 8e8764aeb5a84cc3b8abb634b405980d73746885 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 25 Jun 2019 16:42:00 +0200 Subject: [PATCH] Support options passed to .open LevelDown supports an [optional `options` argument](https://github.com/Level/abstract-leveldown#dbopenoptions-callback) passed the `db.open` method. Currently LevelUp fails if options are passed to `.open` so this PR adds support for that. --- README.md | 2 +- lib/levelup.js | 13 +++++++++++-- test/init-test.js | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1db4938e..0d718f49 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ db.get('foo', function (err, value) { -### `db.open([callback])` +### `db.open([options][, callback])` Opens the underlying store. In general you should never need to call this method directly as it's automatically called by levelup(). diff --git a/lib/levelup.js b/lib/levelup.js index 8e64c82a..a6853913 100644 --- a/lib/levelup.js +++ b/lib/levelup.js @@ -61,15 +61,24 @@ LevelUP.prototype.emit = EventEmitter.prototype.emit LevelUP.prototype.once = EventEmitter.prototype.once inherits(LevelUP, EventEmitter) -LevelUP.prototype.open = function (callback) { +LevelUP.prototype.open = function (opts, callback) { var self = this var promise + if (typeof opts === 'function') { + callback = opts + opts = null + } + if (!callback) { callback = promisify() promise = callback.promise } + if (!opts) { + opts = this.options + } + if (this.isOpen()) { process.nextTick(callback, null, self) return promise @@ -82,7 +91,7 @@ LevelUP.prototype.open = function (callback) { this.emit('opening') - this.db.open(this.options, function (err) { + this.db.open(opts, function (err) { if (err) { return callback(new OpenError(err)) } diff --git a/test/init-test.js b/test/init-test.js index 9fff7fe4..2967263e 100644 --- a/test/init-test.js +++ b/test/init-test.js @@ -58,5 +58,24 @@ buster.testCase('Init & open()', { return done() } throw new Error('did not throw') + }, + + 'support open options': function (done) { + var down = memdown() + + levelup(down, (err, up) => { + refute(err, 'no error') + + up.close(() => { + down.open = (opts) => { + assert.equals(opts.foo, 'bar') + done() + } + + up.open({ + foo: 'bar' + }) + }) + }) } })