Skip to content

Commit

Permalink
Add manifest (#678)
Browse files Browse the repository at this point in the history
* Add manifest
* Add type property for reachdown (Level/community#82)
  • Loading branch information
vweevers authored Sep 30, 2019
1 parent 89d4a51 commit 2a98997
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ db.put('name', 'levelup', function (err) {
## API

- <a href="#ctor"><code><b>levelup()</b></code></a>
- <a href="#supports"><code>db.<b>supports</b></code></a>
- <a href="#open"><code>db.<b>open()</b></code></a>
- <a href="#close"><code>db.<b>close()</b></code></a>
- <a href="#put"><code>db.<b>put()</b></code></a>
Expand Down Expand Up @@ -137,6 +138,22 @@ db.get('foo', function (err, value) {
})
```

<a name="supports"></a>

### `db.supports`

A read-only [manifest](https://github.com/Level/supports). Not [widely supported yet](https://github.com/Level/community/issues/83). Might be used like so:

```js
if (!db.supports.permanence) {
throw new Error('Persistent storage is required')
}

if (db.supports.bufferKeys && db.supports.promises) {
await db.put(Buffer.from('key'), 'value')
}
```

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

### `db.open([options][, callback])`
Expand Down
22 changes: 22 additions & 0 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var DeferredLevelDOWN = require('deferred-leveldown')
var IteratorStream = require('level-iterator-stream')
var Batch = require('./batch')
var errors = require('level-errors')
var supports = require('level-supports')
var assert = require('assert')
var promisify = require('./promisify')
var getCallback = require('./common').getCallback
Expand Down Expand Up @@ -55,6 +56,25 @@ function LevelUP (db, options, callback) {
this._db = db
this.db = new DeferredLevelDOWN(db)
this.open(callback)

// Create manifest based on deferred-leveldown's
this.supports = supports(this.db.supports, {
status: false,
deferredOpen: true,
openCallback: true,
promises: true,
streams: true
})

// Experimental: enrich levelup interface
Object.keys(this.supports.additionalMethods).forEach(function (method) {
if (this[method] != null) return

// Don't do this.db[method].bind() because this.db is dynamic.
this[method] = function () {
return this.db[method].apply(this.db, arguments)
}
}, this)
}

LevelUP.prototype.emit = EventEmitter.prototype.emit
Expand Down Expand Up @@ -324,6 +344,8 @@ LevelUP.prototype.toString = function () {
return 'LevelUP'
}

LevelUP.prototype.type = 'levelup'

function maybeError (db, callback) {
if (!db._isOpening() && !db.isOpen()) {
process.nextTick(callback, new ReadError('Database is not open'))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"deferred-leveldown": "~5.2.1",
"level-errors": "~2.0.0",
"level-iterator-stream": "~4.0.0",
"level-supports": "~1.0.0",
"xtend": "~4.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function suite (options) {
var test = testCommon.test

require('./argument-checking-test')(test, testCommon)
require('./manifest-test')(test, testCommon)
require('./batch-test')(test, testCommon)
if (testCommon.encodings) require('./binary-test')(test, testCommon)
if (testCommon.clear) require('./clear-test')(test)
Expand Down
18 changes: 18 additions & 0 deletions test/manifest-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var suite = require('level-supports/test')

module.exports = function (test, testCommon) {
suite(test, testCommon)

// TODO (once manifest lands in other modules): add integration tests.
test('manifest has expected properties', function (t) {
var db = testCommon.factory()

t.is(db.supports.status, false)
t.is(db.supports.deferredOpen, true)
t.is(db.supports.openCallback, true)
t.is(db.supports.promises, true)
t.is(db.supports.streams, true)

db.close(t.end.bind(t))
})
}

0 comments on commit 2a98997

Please sign in to comment.