Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
/ levelup Public archive

Commit 9071230

Browse files
achingbrainvweevers
authored andcommitted
Support options passed to open() (#660)
1 parent 0059842 commit 9071230

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ db.get('foo', function (err, value) {
138138

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

141-
### `db.open([callback])`
141+
### `db.open([options][, callback])`
142142

143143
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>.
144144

lib/levelup.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,24 @@ LevelUP.prototype.emit = EventEmitter.prototype.emit
6161
LevelUP.prototype.once = EventEmitter.prototype.once
6262
inherits(LevelUP, EventEmitter)
6363

64-
LevelUP.prototype.open = function (callback) {
64+
LevelUP.prototype.open = function (opts, callback) {
6565
var self = this
6666
var promise
6767

68+
if (typeof opts === 'function') {
69+
callback = opts
70+
opts = null
71+
}
72+
6873
if (!callback) {
6974
callback = promisify()
7075
promise = callback.promise
7176
}
7277

78+
if (!opts) {
79+
opts = this.options
80+
}
81+
7382
if (this.isOpen()) {
7483
process.nextTick(callback, null, self)
7584
return promise
@@ -82,7 +91,7 @@ LevelUP.prototype.open = function (callback) {
8291

8392
this.emit('opening')
8493

85-
this.db.open(this.options, function (err) {
94+
this.db.open(opts, function (err) {
8695
if (err) {
8796
return callback(new OpenError(err))
8897
}

test/init-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,24 @@ buster.testCase('Init & open()', {
5858
return done()
5959
}
6060
throw new Error('did not throw')
61+
},
62+
63+
'support open options': function (done) {
64+
var down = memdown()
65+
66+
levelup(down, (err, up) => {
67+
refute(err, 'no error')
68+
69+
up.close(() => {
70+
down.open = (opts) => {
71+
assert.equals(opts.foo, 'bar')
72+
done()
73+
}
74+
75+
up.open({
76+
foo: 'bar'
77+
})
78+
})
79+
})
6180
}
6281
})

0 commit comments

Comments
 (0)