From 84fd57af3d57f354d07c062430f2f1fb485226de Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:23:54 +0200 Subject: [PATCH 1/7] Document `db.getMany(keys)` --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f9f7bdb..5a6d6c6 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ For options specific to [`leveldown`][leveldown] and [`level-js`][level-js] ("un - db.close() - db.put() - db.get() +- db.getMany() - db.del() - db.batch() _(array form)_ - db.batch() _(chained form)_ @@ -212,7 +213,7 @@ If no callback is passed, a promise is returned. ### `db.get(key[, options][, callback])` -get() is the primary method for fetching data from the store. The `key` can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type `'NotFoundError'` so you can `err.type == 'NotFoundError'` or you can perform a truthy test on the property `err.notFound`. +Get a value from the store by `key`. The `key` can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type `'NotFoundError'` so you can `err.type == 'NotFoundError'` or you can perform a truthy test on the property `err.notFound`. ```js db.get('foo', function (err, value) { @@ -234,6 +235,16 @@ db.get('foo', function (err, value) { If no callback is passed, a promise is returned. + + +### `db.getMany(keys[, options][, callback])` + +Get multiple values from the store by an array of `keys`. The optional `options` object is the same as for [`get()`](#dbgetkey-options-callback). + +The `callback` function will be called with an `Error` if the operation failed for any reason. If successful the first argument will be `null` and the second argument will be an array of values with the same order as `keys`. If a key was not found, the relevant value will be `undefined`. + +If no callback is provided, a promise is returned. + ### `db.del(key[, options][, callback])` From 8ec3d379f1536619940efa83919322eb8d6aa4cd Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:24:36 +0200 Subject: [PATCH 2/7] Add `for await...of` example --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 5a6d6c6..87446c9 100644 --- a/README.md +++ b/README.md @@ -442,6 +442,14 @@ db.createReadStream({ keys: false, values: true }) Returns an [`abstract-leveldown` iterator](https://github.com/Level/abstract-leveldown/#iterator), which is what powers the readable streams above. Options are the same as the range options of createReadStream and are passed to the underlying store. +These iterators support [`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of): + +```js +for await (const [key, value] of db.iterator()) { + console.log(value) +} +``` + ### `db.clear([options][, callback])` From 5fcb69a045c71b9a19151f59c8eb382fe20f4b2d Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:25:28 +0200 Subject: [PATCH 3/7] Remove notice that `db.supports` isn't (wasn't) widely supported --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 87446c9..ece646c 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ Note that `createIfMissing` is an option specific to [`leveldown`][leveldown]. ### `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: +A read-only [manifest](https://github.com/Level/supports). Might be used like so: ```js if (!db.supports.permanence) { From a2dd25e96f3666c371162d60b978f4d5765283cd Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:26:09 +0200 Subject: [PATCH 4/7] Remove notice that `db.clear()` is (was) experimental --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index ece646c..a05194c 100644 --- a/README.md +++ b/README.md @@ -454,8 +454,6 @@ for await (const [key, value] of db.iterator()) { ### `db.clear([options][, callback])` -**This method is experimental. Not all underlying stores support it yet. Consult [Level/community#79](https://github.com/Level/community/issues/79) to find out if your (combination of) dependencies support `db.clear()`.** - Delete all entries or a range. Not guaranteed to be atomic. Accepts the following range options (with the same rules as on iterators): - `gt` (greater than), `gte` (greater than or equal) define the lower bound of the range to be deleted. Only entries where the key is greater than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the entries deleted will be the same. From afd8d30439c929c305a6f393be5c4ce25b38f0be Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:27:36 +0200 Subject: [PATCH 5/7] Simplify Promise Support section --- README.md | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index a05194c..07916b5 100644 --- a/README.md +++ b/README.md @@ -467,39 +467,14 @@ If no callback is passed, a promise is returned. ## Promise Support -`level(up)` ships with native `Promise` support out of the box. - -Each function taking a callback also can be used as a promise, if the callback is omitted. This applies for: - -- `db.get(key[, options])` -- `db.put(key, value[, options])` -- `db.del(key[, options])` -- `db.batch(ops[, options])` -- `db.batch().write()` -- `db.clear(options)` - -The only exception is the `level` constructor itself, which if no callback is passed will lazily open the underlying store in the background. +Each function taking a callback can also be used as a promise, if the callback is omitted. The only exception is the `level` constructor itself, which if no callback is passed will lazily open the underlying store in the background. Example: ```js const db = level('my-db') - -db.put('foo', 'bar') - .then(function () { return db.get('foo') }) - .then(function (value) { console.log(value) }) - .catch(function (err) { console.error(err) }) -``` - -Or using `async/await`: - -```js -const main = async () => { - const db = level('my-db') - - await db.put('foo', 'bar') - console.log(await db.get('foo')) -} +await db.put('foo', 'bar') +console.log(await db.get('foo')) ``` ## Events From 89281a9bb1bab778df2ee19acba2433d56295d4b Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:36:17 +0200 Subject: [PATCH 6/7] Document `status` in preference of `isXX()` utilities --- README.md | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 07916b5..00c226f 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ - [`db.del(key[, options][, callback])`](#dbdelkey-options-callback) - [`db.batch(array[, options][, callback])` _(array form)_](#dbbatcharray-options-callback-array-form) - [`db.batch()` _(chained form)_](#dbbatch-chained-form) - - [`db.isOpen()`](#dbisopen) - - [`db.isClosed()`](#dbisclosed) + - [`db.status`](#dbstatus) + - [`db.isOperational()`](#dbisoperational) - [`db.createReadStream([options])`](#dbcreatereadstreamoptions) - [`db.createKeyStream([options])`](#dbcreatekeystreamoptions) - [`db.createValueStream([options])`](#dbcreatevaluestreamoptions) @@ -105,8 +105,8 @@ For options specific to [`leveldown`][leveldown] and [`level-js`][level-js] ("un - db.del() - db.batch() _(array form)_ - db.batch() _(chained form)_ -- db.isOpen() -- db.isClosed() +- db.status +- db.isOperational() - db.createReadStream() - db.createKeyStream() - db.createValueStream() @@ -336,25 +336,23 @@ Commit the queued operations for this batch. All operations not _cleared_ will b If no callback is passed, a promise is returned. - + -### `db.isOpen()` +### `db.status` -A `levelup` instance can be in one of the following states: +A readonly string that is one of: -- _"new"_ - newly created, not opened or closed -- _"opening"_ - waiting for the underlying store to be opened -- _"open"_ - successfully opened the store, available for use -- _"closing"_ - waiting for the store to be closed -- _"closed"_ - store has been successfully closed, should not be used +- `new` - newly created, not opened or closed +- `opening` - waiting for the underlying store to be opened +- `open` - successfully opened the store, available for use +- `closing` - waiting for the store to be closed +- `closed` - store has been successfully closed. -`isOpen()` will return `true` only when the state is "open". + - +### `db.isOperational()` -### `db.isClosed()` - -`isClosed()` will return `true` only when the state is "closing" _or_ "closed", it can be useful for determining if read and write operations are permissible. +Returns `true` if the store accepts operations, which in the case of `level(up)` means that `status` is either `opening` or `open`, because it opens itself and queues up operations until opened. From 831fd3ab007423878ef99ec510432bc774ad39d4 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 2 Oct 2021 10:53:46 +0200 Subject: [PATCH 7/7] Remove redundant API table of contents --- README.md | 75 ++++++++----------------------------------------------- 1 file changed, 11 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 00c226f..5fae78f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ - [`db.close([callback])`](#dbclosecallback) - [`db.put(key, value[, options][, callback])`](#dbputkey-value-options-callback) - [`db.get(key[, options][, callback])`](#dbgetkey-options-callback) + - [`db.getMany(keys[, options][, callback])`](#dbgetmanykeys-options-callback) - [`db.del(key[, options][, callback])`](#dbdelkey-options-callback) - [`db.batch(array[, options][, callback])` _(array form)_](#dbbatcharray-options-callback-array-form) - [`db.batch()` _(chained form)_](#dbbatch-chained-form) @@ -47,7 +48,7 @@ This is a convenience package that: -- exports a function that returns a [`levelup`](https://github.com/Level/levelup#ctor) instance when invoked +- exports a function that returns a [`levelup`](https://github.com/Level/levelup) instance when invoked - bundles the current release of [`leveldown`][leveldown] and [`level-js`][level-js] - leverages encodings using [`encoding-down`][encoding-down]. @@ -95,26 +96,6 @@ Binary keys and values are supported across the board. For options specific to [`leveldown`][leveldown] and [`level-js`][level-js] ("underlying store" from here on out), please see their respective READMEs. -- level() -- db.supports -- db.open() -- db.close() -- db.put() -- db.get() -- db.getMany() -- db.del() -- db.batch() _(array form)_ -- db.batch() _(chained form)_ -- db.status -- db.isOperational() -- db.createReadStream() -- db.createKeyStream() -- db.createValueStream() -- db.iterator() -- db.clear() - - - ### `db = level(location[, options[, callback]])` The main entry point for creating a new `levelup` instance. @@ -162,8 +143,6 @@ level('my-db', { createIfMissing: false }, function (err, db) { Note that `createIfMissing` is an option specific to [`leveldown`][leveldown]. - - ### `db.supports` A read-only [manifest](https://github.com/Level/supports). Might be used like so: @@ -178,39 +157,29 @@ if (db.supports.bufferKeys && db.supports.promises) { } ``` - - ### `db.open([callback])` -Opens the underlying store. In general you should never need to call this method directly as it's automatically called by levelup(). - -However, it is possible to _reopen_ the store after it has been closed with close(), although this is not generally advised. +Opens the underlying store. In general you shouldn't need to call this method directly as it's automatically called by [`level()`](#db--levellocation-options-callback). However, it is possible to reopen the store after it has been closed with [`close()`](#dbclosecallback). If no callback is passed, a promise is returned. - - ### `db.close([callback])` -close() closes the underlying store. The callback will receive any error encountered during closing as the first argument. +`close()` closes the underlying store. The callback will receive any error encountered during closing as the first argument. A `levelup` instance has associated resources like file handles and locks. When you no longer need your `levelup` instance (for the remainder of your program) call `close()` to free up resources. The underlying store cannot be opened by multiple instances of `levelup` simultaneously. If no callback is passed, a promise is returned. - - ### `db.put(key, value[, options][, callback])` -put() is the primary method for inserting data into the store. Both `key` and `value` can be of any type as far as `levelup` is concerned. +`put()` is the primary method for inserting data into the store. Both `key` and `value` can be of any type as far as `levelup` is concerned. - `options` is passed on to the underlying store - `options.keyEncoding` and `options.valueEncoding` are passed to [`encoding-down`][encoding-down], allowing you to override the key- and/or value encoding for this `put` operation. If no callback is passed, a promise is returned. - - ### `db.get(key[, options][, callback])` Get a value from the store by `key`. The `key` can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type `'NotFoundError'` so you can `err.type == 'NotFoundError'` or you can perform a truthy test on the property `err.notFound`. @@ -235,8 +204,6 @@ db.get('foo', function (err, value) { If no callback is passed, a promise is returned. - - ### `db.getMany(keys[, options][, callback])` Get multiple values from the store by an array of `keys`. The optional `options` object is the same as for [`get()`](#dbgetkey-options-callback). @@ -245,11 +212,9 @@ The `callback` function will be called with an `Error` if the operation failed f If no callback is provided, a promise is returned. - - ### `db.del(key[, options][, callback])` -del() is the primary method for removing data from the store. +`del()` is the primary method for removing data from the store. ```js db.del('foo', function (err) { @@ -263,11 +228,9 @@ db.del('foo', function (err) { If no callback is passed, a promise is returned. - - ### `db.batch(array[, options][, callback])` _(array form)_ -batch() can be used for very fast bulk-write operations (both _put_ and _delete_). The `array` argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside the underlying store. +`batch()` can be used for fast bulk-write operations (both _put_ and _delete_). The `array` argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside the underlying store. Each operation is contained in an object having the following properties: `type`, `key`, `value`, where the _type_ is either `'put'` or `'del'`. In the case of `'del'` the `value` property is ignored. Any entries with a `key` of `null` or `undefined` will cause an error to be returned on the `callback` and any `type: 'put'` entry with a `value` of `null` or `undefined` will return an error. @@ -291,11 +254,9 @@ db.batch(ops, function (err) { If no callback is passed, a promise is returned. - - ### `db.batch()` _(chained form)_ -batch(), when called with no arguments will return a `Batch` object which can be used to build, and eventually commit, an atomic batch operation. Depending on how it's used, it is possible to obtain greater performance when using the chained form of `batch()` over the array form. +`batch()`, when called with no arguments will return a `Batch` object which can be used to build, and eventually commit, an atomic batch operation. Depending on how it's used, it is possible to obtain greater performance when using the chained form of `batch()` over the array form. ```js db.batch() @@ -336,8 +297,6 @@ Commit the queued operations for this batch. All operations not _cleared_ will b If no callback is passed, a promise is returned. - - ### `db.status` A readonly string that is one of: @@ -348,14 +307,10 @@ A readonly string that is one of: - `closing` - waiting for the store to be closed - `closed` - store has been successfully closed. - - ### `db.isOperational()` Returns `true` if the store accepts operations, which in the case of `level(up)` means that `status` is either `opening` or `open`, because it opens itself and queues up operations until opened. - - ### `db.createReadStream([options])` Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of key-value pairs. A pair is an object with `key` and `value` properties. By default it will stream all entries in the underlying store from start to end. Use the options described below to control the range, direction and results. @@ -392,11 +347,9 @@ You can supply an options object as the first parameter to `createReadStream()` Underlying stores may have additional options. - - ### `db.createKeyStream([options])` -Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of keys rather than key-value pairs. Use the same options as described for createReadStream to control the range and direction. +Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of keys rather than key-value pairs. Use the same options as described for [`createReadStream()`](#dbcreatereadstreamoptions) to control the range and direction. You can also obtain this stream by passing an options object to `createReadStream()` with `keys` set to `true` and `values` set to `false`. The result is equivalent; both streams operate in [object mode](https://nodejs.org/docs/latest/api/stream.html#stream_object_mode). @@ -413,11 +366,9 @@ db.createReadStream({ keys: true, values: false }) }) ``` - - ### `db.createValueStream([options])` -Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of values rather than key-value pairs. Use the same options as described for createReadStream to control the range and direction. +Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of values rather than key-value pairs. Use the same options as described for [`createReadStream()`](#dbcreatereadstreamoptions) to control the range and direction. You can also obtain this stream by passing an options object to `createReadStream()` with `values` set to `true` and `keys` set to `false`. The result is equivalent; both streams operate in [object mode](https://nodejs.org/docs/latest/api/stream.html#stream_object_mode). @@ -434,11 +385,9 @@ db.createReadStream({ keys: false, values: true }) }) ``` - - ### `db.iterator([options])` -Returns an [`abstract-leveldown` iterator](https://github.com/Level/abstract-leveldown/#iterator), which is what powers the readable streams above. Options are the same as the range options of createReadStream and are passed to the underlying store. +Returns an [`abstract-leveldown` iterator](https://github.com/Level/abstract-leveldown/#iterator), which is what powers the readable streams above. Options are the same as the range options of [`createReadStream()`](#dbcreatereadstreamoptions) and are passed to the underlying store. These iterators support [`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of): @@ -448,8 +397,6 @@ for await (const [key, value] of db.iterator()) { } ``` - - ### `db.clear([options][, callback])` Delete all entries or a range. Not guaranteed to be atomic. Accepts the following range options (with the same rules as on iterators):