diff --git a/index.js b/index.js index b1bd592..5e031fe 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,35 @@ var AbstractIterator = require('abstract-leveldown').AbstractIterator var inherits = require('inherits') var Codec = require('level-codec') var EncodingError = require('level-errors').EncodingError +var rangeMethods = ['approximateSize', 'compactRange'] module.exports = DB.default = DB function DB (db, opts) { if (!(this instanceof DB)) return new DB(db, opts) - AbstractLevelDOWN.call(this, '') + + var manifest = db.supports || {} + var additionalMethods = manifest.additionalMethods || {} + + AbstractLevelDOWN.call(this, manifest) + + this.supports.encodings = true + this.supports.additionalMethods = {} + + rangeMethods.forEach(function (m) { + // TODO (future major): remove this fallback + var fallback = typeof db[m] === 'function' + + if (additionalMethods[m] || fallback) { + this.supports.additionalMethods[m] = true + + this[m] = function (start, end, opts, cb) { + start = this.codec.encodeKey(start, opts) + end = this.codec.encodeKey(end, opts) + return this.db[m](start, end, opts, cb) + } + } + }, this) opts = opts || {} if (typeof opts.keyEncoding === 'undefined') opts.keyEncoding = 'utf8' @@ -84,12 +107,6 @@ DB.prototype._clear = function (opts, callback) { this.db.clear(opts, callback) } -DB.prototype.approximateSize = function (start, end, opts, cb) { - start = this.codec.encodeKey(start, opts) - end = this.codec.encodeKey(end, opts) - return this.db.approximateSize(start, end, opts, cb) -} - function Iterator (db, opts) { AbstractIterator.call(this, db) this.codec = db.codec diff --git a/package.json b/package.json index ec8dbb3..8dbe0ff 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "prepublishOnly": "npm run dependency-check" }, "dependencies": { - "abstract-leveldown": "^6.1.1", + "abstract-leveldown": "^6.2.1", "inherits": "^2.0.3", "level-codec": "^9.0.0", "level-errors": "^2.0.0" diff --git a/test/index.js b/test/index.js index 3d815f1..ba5d013 100644 --- a/test/index.js +++ b/test/index.js @@ -579,17 +579,68 @@ test('iterator catches decoding error from valueEncoding', function (t) { }) }) -test('approximateSize() encodes start and end', function (t) { - t.plan(2) +test('proxies approximateSize() if it exists', function (t) { + t.is(typeof encdown({ approximateSize: noop }).approximateSize, 'function') + t.ok(encdown({ approximateSize: noop }).supports.additionalMethods.approximateSize) + t.is(encdown({}).approximateSize, undefined) + t.notOk(encdown({}).supports.additionalMethods.approximateSize) + t.end() +}) - var down = { +test('proxies compactRange() if it exists', function (t) { + t.is(typeof encdown({ compactRange: noop }).compactRange, 'function') + t.ok(encdown({ compactRange: noop }).supports.additionalMethods.compactRange) + t.is(encdown({}).compactRange, undefined) + t.notOk(encdown({}).supports.additionalMethods.compactRange) + t.end() +}) + +test('encodes start and end of approximateSize()', function (t) { + var db = encdown({ approximateSize: function (start, end) { t.is(start, '1') t.is(end, '2') + t.end() } - } + }) + + db.approximateSize(1, 2, noop) +}) + +test('encodes start and end of compactRange()', function (t) { + var db = encdown({ + compactRange: function (start, end) { + t.is(start, '1') + t.is(end, '2') + t.end() + } + }) + + db.compactRange(1, 2, noop) +}) + +test('encodes start and end of approximateSize() with custom encoding', function (t) { + var db = encdown({ + approximateSize: function (start, end) { + t.is(start, '"a"') + t.is(end, '"b"') + t.end() + } + }) + + db.approximateSize('a', 'b', { keyEncoding: 'json' }, noop) +}) + +test('encodes start and end of compactRange() with custom encoding', function (t) { + var db = encdown({ + compactRange: function (start, end) { + t.is(start, '"a"') + t.is(end, '"b"') + t.end() + } + }) - encdown(down).approximateSize(1, 2, noop) + db.compactRange('a', 'b', { keyEncoding: 'json' }, noop) }) test('encodes seek target', function (t) {