Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manifest #351

Merged
merged 4 commits into from Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
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
@@ -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
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
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
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
@@ -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
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