You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Document `db.getMany(keys)`
- Add `for await...of` example
- Remove notice that `db.supports` isn't (wasn't) widely supported
- Remove notice that `db.clear()` is (was) experimental
- Simplify Promise Support section
- Document `status` in preference over `isXX()` utilities
- Remove redundant API table of contents
- exports a function that returns a [`levelup`](https://github.com/Level/levelup#ctor) instance when invoked
51
+
- exports a function that returns a [`levelup`](https://github.com/Level/levelup) instance when invoked
51
52
- bundles the current release of [`leveldown`][leveldown] and [`level-js`][level-js]
52
53
- leverages encodings using [`encoding-down`][encoding-down].
53
54
@@ -95,25 +96,6 @@ Binary keys and values are supported across the board.
95
96
96
97
For options specific to [`leveldown`][leveldown] and [`level-js`][level-js] ("underlying store" from here on out), please see their respective READMEs.
Note that `createIfMissing` is an option specific to [`leveldown`][leveldown].
163
145
164
-
<aname="supports"></a>
165
-
166
146
### `db.supports`
167
147
168
-
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:
148
+
A read-only [manifest](https://github.com/Level/supports). Might be used like so:
169
149
170
150
```js
171
151
if (!db.supports.permanence) {
@@ -177,42 +157,32 @@ if (db.supports.bufferKeys && db.supports.promises) {
177
157
}
178
158
```
179
159
180
-
<aname="open"></a>
181
-
182
160
### `db.open([callback])`
183
161
184
-
Opens the underlying store. In general you should never need to call this method directly as it's automatically called by <ahref="#ctor"><code>levelup()</code></a>.
185
-
186
-
However, it is possible to _reopen_ the store after it has been closed with <ahref="#close"><code>close()</code></a>, although this is not generally advised.
162
+
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).
187
163
188
164
If no callback is passed, a promise is returned.
189
165
190
-
<aname="close"></a>
191
-
192
166
### `db.close([callback])`
193
167
194
-
<code>close()</code> closes the underlying store. The callback will receive any error encountered during closing as the first argument.
168
+
`close()` closes the underlying store. The callback will receive any error encountered during closing as the first argument.
195
169
196
170
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.
197
171
198
172
If no callback is passed, a promise is returned.
199
173
200
-
<aname="put"></a>
201
-
202
174
### `db.put(key, value[, options][, callback])`
203
175
204
-
<code>put()</code> 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.
176
+
`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.
205
177
206
178
-`options` is passed on to the underlying store
207
179
-`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.
208
180
209
181
If no callback is passed, a promise is returned.
210
182
211
-
<aname="get"></a>
212
-
213
183
### `db.get(key[, options][, callback])`
214
184
215
-
<code>get()</code> 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`.
185
+
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`.
216
186
217
187
```js
218
188
db.get('foo', function (err, value) {
@@ -234,11 +204,17 @@ db.get('foo', function (err, value) {
234
204
235
205
If no callback is passed, a promise is returned.
236
206
237
-
<aname="del"></a>
207
+
### `db.getMany(keys[, options][, callback])`
208
+
209
+
Get multiple values from the store by an array of `keys`. The optional `options` object is the same as for [`get()`](#dbgetkey-options-callback).
210
+
211
+
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`.
212
+
213
+
If no callback is provided, a promise is returned.
238
214
239
215
### `db.del(key[, options][, callback])`
240
216
241
-
<code>del()</code> is the primary method for removing data from the store.
217
+
`del()` is the primary method for removing data from the store.
242
218
243
219
```js
244
220
db.del('foo', function (err) {
@@ -252,11 +228,9 @@ db.del('foo', function (err) {
<code>batch()</code> 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.
233
+
`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.
260
234
261
235
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.
262
236
@@ -280,11 +254,9 @@ db.batch(ops, function (err) {
280
254
281
255
If no callback is passed, a promise is returned.
282
256
283
-
<aname="batch_chained"></a>
284
-
285
257
### `db.batch()`_(chained form)_
286
258
287
-
<code>batch()</code>, 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.
259
+
`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.
288
260
289
261
```js
290
262
db.batch()
@@ -325,27 +297,19 @@ Commit the queued operations for this batch. All operations not _cleared_ will b
325
297
326
298
If no callback is passed, a promise is returned.
327
299
328
-
<aname="isOpen"></a>
329
-
330
-
### `db.isOpen()`
300
+
### `db.status`
331
301
332
-
A `levelup` instance can be in one of the following states:
302
+
A readonly string that is one of:
333
303
334
-
-_"new"_ - newly created, not opened or closed
335
-
-_"opening"_ - waiting for the underlying store to be opened
336
-
-_"open"_ - successfully opened the store, available for use
337
-
-_"closing"_ - waiting for the store to be closed
338
-
-_"closed"_ - store has been successfully closed, should not be used
304
+
-`new` - newly created, not opened or closed
305
+
-`opening` - waiting for the underlying store to be opened
306
+
-`open` - successfully opened the store, available for use
307
+
-`closing` - waiting for the store to be closed
308
+
-`closed` - store has been successfully closed.
339
309
340
-
`isOpen()` will return `true` only when the state is "open".
310
+
### `db.isOperational()`
341
311
342
-
<aname="isClosed"></a>
343
-
344
-
### `db.isClosed()`
345
-
346
-
`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.
347
-
348
-
<aname="createReadStream"></a>
312
+
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.
349
313
350
314
### `db.createReadStream([options])`
351
315
@@ -383,11 +347,9 @@ You can supply an options object as the first parameter to `createReadStream()`
383
347
384
348
Underlying stores may have additional options.
385
349
386
-
<aname="createKeyStream"></a>
387
-
388
350
### `db.createKeyStream([options])`
389
351
390
-
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 <ahref="#createReadStream"><code>createReadStream</code></a> to control the range and direction.
352
+
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.
391
353
392
354
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).
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 <ahref="#createReadStream"><code>createReadStream</code></a> to control the range and direction.
371
+
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.
412
372
413
373
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).
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 <ahref="#createReadStream"><code>createReadStream</code></a> and are passed to the underlying store.
390
+
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.
433
391
434
-
<aname="clear"></a>
392
+
These iterators support [`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of):
435
393
436
-
### `db.clear([options][, callback])`
394
+
```js
395
+
forawait (const [key, value] ofdb.iterator()) {
396
+
console.log(value)
397
+
}
398
+
```
437
399
438
-
**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()`.**
400
+
### `db.clear([options][, callback])`
439
401
440
402
Delete all entries or a range. Not guaranteed to be atomic. Accepts the following range options (with the same rules as on iterators):
441
403
@@ -450,39 +412,14 @@ If no callback is passed, a promise is returned.
450
412
451
413
## Promise Support
452
414
453
-
`level(up)` ships with native `Promise` support out of the box.
454
-
455
-
Each function taking a callback also can be used as a promise, if the callback is omitted. This applies for:
456
-
457
-
-`db.get(key[, options])`
458
-
-`db.put(key, value[, options])`
459
-
-`db.del(key[, options])`
460
-
-`db.batch(ops[, options])`
461
-
-`db.batch().write()`
462
-
-`db.clear(options)`
463
-
464
-
The only exception is the `level` constructor itself, which if no callback is passed will lazily open the underlying store in the background.
415
+
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.
0 commit comments