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

Commit

Permalink
Add manifest (Level/community#83) (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers authored Sep 30, 2019
1 parent d7ba459 commit 539abd0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ A read-only property. An `abstract-leveldown` compliant store can be in one of t
- `'closing'` - waiting for the store to be closed
- `'closed'` - store has been successfully closed, should not be used.

### `db.supports`

A read-only [manifest](https://github.com/Level/supports). 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')
}
```

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

Open the store. The `callback` function will be called with no arguments when the store has been successfully opened, or with a single error argument if the open operation failed for any reason.
Expand Down Expand Up @@ -294,9 +308,17 @@ Support of other key and value types depends on the implementation as well as it

Each of these methods will receive exactly the number and order of arguments described. Optional arguments will receive sensible defaults. All callbacks are error-first and must be asynchronous. If an operation within your implementation is synchronous, be sure to invoke the callback on a next tick using `process.nextTick(callback, ..)`, `setImmediate` or some other means of micro- or macrotask scheduling.

### `db = AbstractLevelDOWN()`
### `db = AbstractLevelDOWN([manifest])`

The constructor takes no parameters. Sets the `.status` to `'new'`.
The constructor. Sets the `.status` to `'new'`. Optionally takes a [manifest](https://github.com/Level/supports) object which `abstract-leveldown` will enrich:

```js
AbstractLevelDOWN.call(this, {
bufferKeys: true,
snapshots: true,
// ..
})
```

### `db._open(options, callback)`

Expand Down Expand Up @@ -497,6 +519,8 @@ This also serves as a signal to users of your implementation. The following opti
- Snapshots are created asynchronously
- `createIfMissing` and `errorIfExists`: set to `false` if `db._open()` does not support these options.

This metadata will be moved to manifests (`db.supports`) in the future.

### Setup and teardown

To perform (a)synchronous work before or after each test, you may define `setUp` and `tearDown` functions:
Expand Down
8 changes: 7 additions & 1 deletion abstract-leveldown.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
var xtend = require('xtend')
var supports = require('level-supports')
var AbstractIterator = require('./abstract-iterator')
var AbstractChainedBatch = require('./abstract-chained-batch')
var hasOwnProperty = Object.prototype.hasOwnProperty
var rangeOptions = 'start end gt gte lt lte'.split(' ')

function AbstractLevelDOWN () {
function AbstractLevelDOWN (manifest) {
this.status = 'new'

// TODO (next major): make this mandatory
this.supports = supports(manifest, {
status: true
})
}

AbstractLevelDOWN.prototype.open = function (options, callback) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"level-concat-iterator": "~2.0.0",
"level-supports": "~1.0.0",
"xtend": "~4.0.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ function testCommon (options) {
return {
test: test,
factory: factory,

// TODO (next major): remove
setUp: options.setUp || noopTest(),
tearDown: options.tearDown || noopTest(),

// TODO (next major): use db.supports instead
bufferKeys: options.bufferKeys !== false,
createIfMissing: options.createIfMissing !== false,
errorIfExists: options.errorIfExists !== false,
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function suite (options) {
var test = testCommon.test

require('./factory-test')(test, testCommon)
require('./manifest-test')(test, testCommon)

require('./leveldown-test')(test, testCommon)
require('./open-test').all(test, testCommon)
Expand Down
15 changes: 15 additions & 0 deletions test/manifest-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var suite = require('level-supports/test')

module.exports = function (test, testCommon) {
test('setUp common', testCommon.setUp)

suite(test, testCommon)

test('manifest has status', function (t) {
var db = testCommon.factory()
t.is(db.supports.status, true)
db.close(t.end.bind(t))
})

test('tearDown', testCommon.tearDown)
}
2 changes: 1 addition & 1 deletion test/self.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var legacyRangeOptions = ['start', 'end']
// excluding noop operations that can't pass the test suite.

require('./leveldown-test')(test, testCommon)

require('./manifest-test')(test, testCommon)
require('./open-test').all(test, testCommon)

require('./open-create-if-missing-test').setUp(test, testCommon)
Expand Down

0 comments on commit 539abd0

Please sign in to comment.