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

Support options passed to .open #660

Merged
merged 1 commit into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
})
})
})
}
})