Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Support options passed to open() (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored and vweevers committed Jun 28, 2019
1 parent 0059842 commit 9071230
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ db.get('foo', function (err, value) {

<a name="open"></a>

### `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 <a href="#ctor"><code>levelup()</code></a>.

Expand Down
13 changes: 11 additions & 2 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
Expand Down
19 changes: 19 additions & 0 deletions test/init-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
})
})
}
})

0 comments on commit 9071230

Please sign in to comment.