Skip to content

Commit

Permalink
Use reachdown package (Level/community#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Sep 14, 2019
1 parent 992353b commit 9e84881
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
21 changes: 5 additions & 16 deletions leveldown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var inherits = require('inherits')
var abstract = require('abstract-leveldown')
var wrap = require('level-option-wrap')
var reachdown = require('reachdown')
var matchdown = require('./matchdown')

var rangeOptions = 'start end gt gte lt lte'.split(' ')
var defaultClear = abstract.AbstractLevelDOWN.prototype._clear
Expand Down Expand Up @@ -83,13 +85,13 @@ SubDown.prototype._open = function (opts, cb) {
this.db.open(function (err) {
if (err) return cb(err)

var subdb = down(self.db, 'subleveldown')
var subdb = reachdown(self.db, 'subleveldown')

if (subdb && subdb.prefix) {
self.prefix = subdb.prefix + self.prefix
self.leveldown = down(subdb.db)
self.leveldown = reachdown(subdb.db, matchdown, false)
} else {
self.leveldown = down(self.db)
self.leveldown = reachdown(self.db, matchdown, false)
}

if (self._beforeOpen) self._beforeOpen(cb)
Expand Down Expand Up @@ -179,16 +181,3 @@ SubDown.prototype._iterator = function (opts) {
}

module.exports = SubDown

function down (db, type) {
if (typeof db.down === 'function') return db.down(type)
if (type && db.type === type) return db
if (isLooseAbstract(db.db)) return down(db.db, type)
if (isLooseAbstract(db._db)) return down(db._db, type)
return type ? null : db
}

function isLooseAbstract (db) {
if (!db || typeof db !== 'object') { return false }
return typeof db._batch === 'function' && typeof db._iterator === 'function'
}
8 changes: 8 additions & 0 deletions matchdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function matchdown (db, type) {
// Skip layers that we handle ourselves
if (type === 'levelup') return false
if (type === 'encoding-down') return false
if (type === 'deferred-leveldown') return false

return true
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"encoding-down": "^6.2.0",
"inherits": "^2.0.3",
"level-option-wrap": "^1.1.0",
"levelup": "^4.2.0"
"levelup": "^4.2.0",
"reachdown": "^1.0.0"
},
"devDependencies": {
"after": "^0.8.2",
Expand Down

0 comments on commit 9e84881

Please sign in to comment.