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

Commit 2a98997

Browse files
authored
Add manifest (#678)
* Add manifest * Add type property for reachdown (Level/community#82)
1 parent 89d4a51 commit 2a98997

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ db.put('name', 'levelup', function (err) {
8282
## API
8383

8484
- <a href="#ctor"><code><b>levelup()</b></code></a>
85+
- <a href="#supports"><code>db.<b>supports</b></code></a>
8586
- <a href="#open"><code>db.<b>open()</b></code></a>
8687
- <a href="#close"><code>db.<b>close()</b></code></a>
8788
- <a href="#put"><code>db.<b>put()</b></code></a>
@@ -137,6 +138,22 @@ db.get('foo', function (err, value) {
137138
})
138139
```
139140

141+
<a name="supports"></a>
142+
143+
### `db.supports`
144+
145+
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:
146+
147+
```js
148+
if (!db.supports.permanence) {
149+
throw new Error('Persistent storage is required')
150+
}
151+
152+
if (db.supports.bufferKeys && db.supports.promises) {
153+
await db.put(Buffer.from('key'), 'value')
154+
}
155+
```
156+
140157
<a name="open"></a>
141158

142159
### `db.open([options][, callback])`

lib/levelup.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var DeferredLevelDOWN = require('deferred-leveldown')
55
var IteratorStream = require('level-iterator-stream')
66
var Batch = require('./batch')
77
var errors = require('level-errors')
8+
var supports = require('level-supports')
89
var assert = require('assert')
910
var promisify = require('./promisify')
1011
var getCallback = require('./common').getCallback
@@ -55,6 +56,25 @@ function LevelUP (db, options, callback) {
5556
this._db = db
5657
this.db = new DeferredLevelDOWN(db)
5758
this.open(callback)
59+
60+
// Create manifest based on deferred-leveldown's
61+
this.supports = supports(this.db.supports, {
62+
status: false,
63+
deferredOpen: true,
64+
openCallback: true,
65+
promises: true,
66+
streams: true
67+
})
68+
69+
// Experimental: enrich levelup interface
70+
Object.keys(this.supports.additionalMethods).forEach(function (method) {
71+
if (this[method] != null) return
72+
73+
// Don't do this.db[method].bind() because this.db is dynamic.
74+
this[method] = function () {
75+
return this.db[method].apply(this.db, arguments)
76+
}
77+
}, this)
5878
}
5979

6080
LevelUP.prototype.emit = EventEmitter.prototype.emit
@@ -324,6 +344,8 @@ LevelUP.prototype.toString = function () {
324344
return 'LevelUP'
325345
}
326346

347+
LevelUP.prototype.type = 'levelup'
348+
327349
function maybeError (db, callback) {
328350
if (!db._isOpening() && !db.isOpen()) {
329351
process.nextTick(callback, new ReadError('Database is not open'))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"deferred-leveldown": "~5.2.1",
1818
"level-errors": "~2.0.0",
1919
"level-iterator-stream": "~4.0.0",
20+
"level-supports": "~1.0.0",
2021
"xtend": "~4.0.0"
2122
},
2223
"devDependencies": {

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function suite (options) {
77
var test = testCommon.test
88

99
require('./argument-checking-test')(test, testCommon)
10+
require('./manifest-test')(test, testCommon)
1011
require('./batch-test')(test, testCommon)
1112
if (testCommon.encodings) require('./binary-test')(test, testCommon)
1213
if (testCommon.clear) require('./clear-test')(test)

test/manifest-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var suite = require('level-supports/test')
2+
3+
module.exports = function (test, testCommon) {
4+
suite(test, testCommon)
5+
6+
// TODO (once manifest lands in other modules): add integration tests.
7+
test('manifest has expected properties', function (t) {
8+
var db = testCommon.factory()
9+
10+
t.is(db.supports.status, false)
11+
t.is(db.supports.deferredOpen, true)
12+
t.is(db.supports.openCallback, true)
13+
t.is(db.supports.promises, true)
14+
t.is(db.supports.streams, true)
15+
16+
db.close(t.end.bind(t))
17+
})
18+
}

0 commit comments

Comments
 (0)