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

Increase API parity with levelup #383

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7765471
Add `length` property to chained batch for API parity
vweevers Oct 2, 2021
d4531e4
Breaking: add promise support and throw if not operational
vweevers Oct 3, 2021
950bfe5
Breaking: remove `setUp` and `tearDown` from test suite
vweevers Oct 3, 2021
6260d12
Breaking: make `clear()` and `getMany()` mandatory
vweevers Oct 3, 2021
bc39001
Remove `promises` and `status` options from test suite
vweevers Oct 3, 2021
9f40183
Make `AbstractLevelDOWN` an `EventEmitter`
vweevers Oct 3, 2021
24dbb23
Make opening & closing idempotent and add events
vweevers Oct 3, 2021
7ddec3c
Breaking: set initial status to 'closed' instead of 'new'
vweevers Oct 8, 2021
833405d
fixup! Make opening & closing idempotent and add events
vweevers Oct 8, 2021
8df123b
Allow `iterator.end()` while `status` is closing
vweevers Oct 8, 2021
e6b9aac
Breaking: remove test suite options in favor of `db.supports`
vweevers Oct 8, 2021
90a1827
Breaking: end iterators on db close and make ending idempotent
vweevers Oct 8, 2021
d977023
Breaking: close chained batch on db close to prevent reuse
vweevers Oct 8, 2021
dbbf1d8
Normalize `limit` similar to `levelup`
vweevers Oct 9, 2021
cda2e60
Add `passive` option to `db.open()`
vweevers Oct 9, 2021
a0df5b8
Add `db.supports.events`
vweevers Oct 9, 2021
5e3630b
Add put, del, batch and clear events
vweevers Oct 9, 2021
b70b757
fixup! Normalize `limit` similar to `levelup`
vweevers Oct 9, 2021
e207096
Breaking: add deferred open
vweevers Oct 17, 2021
2d916ec
fixup! Breaking: add deferred open
vweevers Oct 18, 2021
c5e8678
Breaking: add encodings and support UInt8Array
vweevers Oct 26, 2021
413b828
Breaking: drop support of Node.js 10
vweevers Oct 27, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Breaking: add promise support and throw if not operational
  • Loading branch information
vweevers committed Oct 3, 2021
commit d4531e4397c9da3010c86eaa7c8704bc75a65d87
42 changes: 42 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@ This document describes breaking changes and how to upgrade. For a complete list

</details>

## Upcoming

All methods that take a callback now also support promises. They return a promise if no callback is provided, the same as `levelup`.

On any operation, `abstract-leveldown` now checks if it's open. If not, it will either throw an error (if the relevant API is synchronous) or asynchronously yield an error. For example:

```js
try {
db.iterator()
} catch (err) {
// Error: Database is not open
}
```

```js
try {
await db.get('example')
} catch (err) {
// Error: Database is not open
}
```

```js
db.get('example', function (err, value) {
// Error: Database is not open
})
```

This may be a breaking change downstream because it changes error messages for implementations that had their own safety checks (which will now be ineffective because `abstract-leveldown` checks are performed first).

Implementations that have additional methods, like `leveldown` that has an `approximateSize()` method which is not part of the `abstract-leveldown` interface, should add or align their own safety checks for consistency. Like so:

```js
LevelDOWN.prototype.approximateSize = function (start, end, callback) {
if (!this.isOperational()) {
throw new Error('Database is not open')
}

// ...
}
```

## 7.0.0

Legacy range options have been removed ([Level/community#86](https://github.com/Level/community/issues/86)). If you previously did:
Expand Down
34 changes: 25 additions & 9 deletions abstract-chained-batch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict'

const { fromCallback } = require('catering')
const { getCallback, getOptions } = require('./lib/common')

const emptyOptions = Object.freeze({})
const kLength = Symbol('length')
const kPromise = Symbol('promise')

function AbstractChainedBatch (db) {
if (typeof db !== 'object' || db === null) {
Expand Down Expand Up @@ -30,6 +34,10 @@ AbstractChainedBatch.prototype._checkWritten = function () {
}

AbstractChainedBatch.prototype.put = function (key, value, options) {
if (!this.db.isOperational()) {
throw new Error('Database is not open')
}

this._checkWritten()

const err = this.db._checkKey(key) || this.db._checkValue(value)
Expand All @@ -49,6 +57,10 @@ AbstractChainedBatch.prototype._put = function (key, value, options) {
}

AbstractChainedBatch.prototype.del = function (key, options) {
if (!this.db.isOperational()) {
throw new Error('Database is not open')
}

this._checkWritten()

const err = this.db._checkKey(key)
Expand All @@ -66,6 +78,10 @@ AbstractChainedBatch.prototype._del = function (key, options) {
}

AbstractChainedBatch.prototype.clear = function () {
if (!this.db.isOperational()) {
throw new Error('Database is not open')
}

this._checkWritten()
this._clear()
this[kLength] = 0
Expand All @@ -78,20 +94,20 @@ AbstractChainedBatch.prototype._clear = function () {
}

AbstractChainedBatch.prototype.write = function (options, callback) {
this._checkWritten()
callback = getCallback(options, callback)
callback = fromCallback(callback, kPromise)
options = getOptions(options)

if (typeof options === 'function') {
callback = options
}
if (typeof callback !== 'function') {
throw new Error('write() requires a callback argument')
}
if (typeof options !== 'object' || options === null) {
options = {}
if (!this.db.isOperational()) {
this._nextTick(callback, new Error('Database is not open'))
return callback[kPromise]
}

this._checkWritten()
this._written = true
this._write(options, callback)

return callback[kPromise]
}

AbstractChainedBatch.prototype._write = function (options, callback) {
Expand Down
31 changes: 19 additions & 12 deletions abstract-iterator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

const { fromCallback } = require('catering')
const kPromise = Symbol('promise')

function AbstractIterator (db) {
if (typeof db !== 'object' || db === null) {
throw new TypeError('First argument must be an abstract-leveldown compliant store')
Expand All @@ -26,6 +29,11 @@ AbstractIterator.prototype.next = function (callback) {
throw new Error('next() requires a callback argument')
}

if (!this.db.isOperational()) {
this._nextTick(callback, new Error('Database is not open'))
return ret
}

if (this._ended) {
this._nextTick(callback, new Error('cannot call next() after end()'))
return ret
Expand All @@ -50,9 +58,14 @@ AbstractIterator.prototype._next = function (callback) {
}

AbstractIterator.prototype.seek = function (target) {
if (!this.db.isOperational()) {
throw new Error('Database is not open')
}

if (this._ended) {
throw new Error('cannot call seek() after end()')
}

if (this._nexting) {
throw new Error('cannot call seek() before next() has completed')
}
Expand All @@ -64,28 +77,22 @@ AbstractIterator.prototype.seek = function (target) {
AbstractIterator.prototype._seek = function (target) {}

AbstractIterator.prototype.end = function (callback) {
let promise
callback = fromCallback(callback, kPromise)

if (callback === undefined) {
promise = new Promise(function (resolve, reject) {
callback = function (err) {
if (err) reject(err)
else resolve()
}
})
} else if (typeof callback !== 'function') {
throw new Error('end() requires a callback argument')
if (!this.db.isOperational()) {
this._nextTick(callback, new Error('Database is not open'))
return callback[kPromise]
}

if (this._ended) {
this._nextTick(callback, new Error('end() already called on iterator'))
return promise
return callback[kPromise]
}

this._ended = true
this._end(callback)

return promise
return callback[kPromise]
}

AbstractIterator.prototype._end = function (callback) {
Expand Down
Loading