Skip to content

Commit 725e313

Browse files
committed
Improve snapshot documentation
- Add `snapshot` to TypeScript option types - Remove redundant documentation from README - Don't mark explicit snapshots as experimental. I'm confident in this feature now (having finished the implementation in `classic-level`) and I'm happy with the API. Ref: Level/community#118 Follow-Up-To: #93
1 parent 926978a commit 725e313

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

README.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Get a value from the database by `key`. The optional `options` object may contai
154154

155155
- `keyEncoding`: custom key encoding for this operation, used to encode the `key`.
156156
- `valueEncoding`: custom value encoding for this operation, used to decode the value.
157-
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
157+
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from.
158158

159159
Returns a promise for the value. If the `key` was not found then the value will be `undefined`.
160160

@@ -164,7 +164,7 @@ Get multiple values from the database by an array of `keys`. The optional `optio
164164

165165
- `keyEncoding`: custom key encoding for this operation, used to encode the `keys`.
166166
- `valueEncoding`: custom value encoding for this operation, used to decode values.
167-
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
167+
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from.
168168

169169
Returns a promise for an array of values with the same order as `keys`. If a key was not found, the relevant value will be `undefined`.
170170

@@ -173,7 +173,7 @@ Returns a promise for an array of values with the same order as `keys`. If a key
173173
Check if the database has an entry with the given `key`. The optional `options` object may contain:
174174

175175
- `keyEncoding`: custom key encoding for this operation, used to encode the `key`.
176-
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
176+
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from.
177177

178178
Returns a promise for a boolean. For example:
179179

@@ -198,7 +198,7 @@ if (value !== undefined) {
198198
Check if the database has entries with the given keys. The `keys` argument must be an array. The optional `options` object may contain:
199199

200200
- `keyEncoding`: custom key encoding for this operation, used to encode the `keys`.
201-
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
201+
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from.
202202

203203
Returns a promise for an array of booleans with the same order as `keys`. For example:
204204

@@ -299,7 +299,7 @@ The `gte` and `lte` range options take precedence over `gt` and `lt` respectivel
299299
- `keyEncoding`: custom key encoding for this iterator, used to encode range options, to encode `seek()` targets and to decode keys.
300300
- `valueEncoding`: custom value encoding for this iterator, used to decode values.
301301
- `signal`: an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to [abort read operations on the iterator](#aborting-iterators).
302-
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) for the iterator to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot before returning an iterator.
302+
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from.
303303

304304
Lastly, an implementation is free to add its own options.
305305

@@ -342,7 +342,7 @@ Delete all entries or a range. Not guaranteed to be atomic. Returns a promise. A
342342
- `reverse` (boolean, default: `false`): delete entries in reverse order. Only effective in combination with `limit`, to delete the last N entries.
343343
- `limit` (number, default: `Infinity`): limit the number of entries to be deleted. This number represents a _maximum_ number of entries and will not be reached if the end of the range is reached first. A value of `Infinity` or `-1` means there is no limit. When `reverse` is true the entries with the highest keys will be deleted instead of the lowest keys.
344344
- `keyEncoding`: custom key encoding for this operation, used to encode range options.
345-
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from, such that entries not present in the snapshot will not be deleted. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database may create its own internal snapshot but (unlike on other methods) this is currently not a hard requirement for implementations.
345+
- `snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from, such that entries not present in the snapshot will not be deleted. If no `snapshot` is provided, the database may create its own internal snapshot but (unlike on other methods) this is currently not a hard requirement for implementations.
346346

347347
The `gte` and `lte` range options take precedence over `gt` and `lt` respectively. If no options are provided, all entries will be deleted.
348348

@@ -451,14 +451,10 @@ console.log(nested.prefixKey('a', 'utf8', true)) // '!nested!a'
451451

452452
### `snapshot = db.snapshot(options)`
453453

454-
**This is an experimental API ([Level/community#118](https://github.com/Level/community/issues/118)).**
455-
456-
Create an explicit [snapshot](#snapshot). Throws a [`LEVEL_NOT_SUPPORTED`](#level_not_supported) error if `db.supports.explicitSnapshots` is false. For details, see [Reading From Snapshots](#reading-from-snapshots).
454+
Create an explicit [snapshot](#snapshot). Throws a [`LEVEL_NOT_SUPPORTED`](#level_not_supported) error if `db.supports.explicitSnapshots` is false ([Level/community#118](https://github.com/Level/community/issues/118)). For details, see [Reading From Snapshots](#reading-from-snapshots).
457455

458456
There are currently no options but specific implementations may add their own.
459457

460-
Don't forget to call `snapshot.close()` when done.
461-
462458
### `db.supports`
463459

464460
A [manifest](https://github.com/Level/supports) describing the features supported by this database. Might be used like so:

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export {
44
AbstractOpenOptions,
55
AbstractGetOptions,
66
AbstractGetManyOptions,
7+
AbstractHasOptions,
8+
AbstractHasManyOptions,
79
AbstractPutOptions,
810
AbstractDelOptions,
911
AbstractBatchOptions,
@@ -42,4 +44,8 @@ export {
4244
AbstractSnapshot
4345
} from './types/abstract-snapshot'
4446

47+
export {
48+
AbstractReadOptions
49+
} from './types/interfaces'
50+
4551
export * as Transcoder from 'level-transcoder'

types/abstract-iterator.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Transcoder from 'level-transcoder'
2-
import { RangeOptions } from './interfaces'
2+
import { AbstractReadOptions, RangeOptions } from './interfaces'
33

4-
declare interface CommonIteratorOptions {
4+
declare interface CommonIteratorOptions extends AbstractReadOptions {
55
/**
66
* An [`AbortSignal`][1] to abort read operations on the iterator.
77
*

types/abstract-level.d.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
AbstractValueIteratorOptions
1515
} from './abstract-iterator'
1616

17-
import { RangeOptions } from './interfaces'
17+
import { AbstractReadOptions, RangeOptions } from './interfaces'
1818

1919
/**
2020
* Abstract class for a lexicographically sorted key-value database.
@@ -272,12 +272,20 @@ declare class AbstractLevel<TFormat, KDefault = string, VDefault = string>
272272

273273
/**
274274
* Create an explicit snapshot. Throws a `LEVEL_NOT_SUPPORTED` error if
275-
* `db.supports.explicitSnapshots` is false.
276-
*
277-
* Don't forget to call `snapshot.close()` when done.
275+
* `db.supports.explicitSnapshots` is false ([Level/community#118][1]).
278276
*
279277
* @param options There are currently no options but specific implementations
280278
* may add their own.
279+
*
280+
* @example
281+
* ```ts
282+
* await db.put('example', 'before')
283+
* await using snapshot = db.snapshot()
284+
* await db.put('example', 'after')
285+
* await db.get('example', { snapshot })) // Returns 'before'
286+
* ```
287+
*
288+
* [1]: https://github.com/Level/community/issues/118
281289
*/
282290
snapshot (options?: any | undefined): AbstractSnapshot
283291

@@ -351,7 +359,7 @@ export interface AbstractOpenOptions {
351359
/**
352360
* Options for the {@link AbstractLevel.get} method.
353361
*/
354-
export interface AbstractGetOptions<K, V> {
362+
export interface AbstractGetOptions<K, V> extends AbstractReadOptions {
355363
/**
356364
* Custom key encoding for this operation, used to encode the `key`.
357365
*/
@@ -366,7 +374,7 @@ export interface AbstractGetOptions<K, V> {
366374
/**
367375
* Options for the {@link AbstractLevel.getMany} method.
368376
*/
369-
export interface AbstractGetManyOptions<K, V> {
377+
export interface AbstractGetManyOptions<K, V> extends AbstractReadOptions {
370378
/**
371379
* Custom key encoding for this operation, used to encode the `keys`.
372380
*/
@@ -381,7 +389,7 @@ export interface AbstractGetManyOptions<K, V> {
381389
/**
382390
* Options for the {@link AbstractLevel.has} method.
383391
*/
384-
export interface AbstractHasOptions<K> {
392+
export interface AbstractHasOptions<K> extends AbstractReadOptions {
385393
/**
386394
* Custom key encoding for this operation, used to encode the `key`.
387395
*/
@@ -391,7 +399,7 @@ export interface AbstractHasOptions<K> {
391399
/**
392400
* Options for the {@link AbstractLevel.hasMany} method.
393401
*/
394-
export interface AbstractHasManyOptions<K> {
402+
export interface AbstractHasManyOptions<K> extends AbstractReadOptions {
395403
/**
396404
* Custom key encoding for this operation, used to encode the `keys`.
397405
*/
@@ -515,6 +523,12 @@ export interface AbstractClearOptions<K> extends RangeOptions<K> {
515523
* Custom key encoding for this operation, used to encode range options.
516524
*/
517525
keyEncoding?: string | Transcoder.PartialEncoding<K> | undefined
526+
527+
/**
528+
* Explicit snapshot to read from, such that entries not present in the snapshot will
529+
* not be deleted.
530+
*/
531+
snapshot?: AbstractSnapshot | undefined
518532
}
519533

520534
/**

types/interfaces.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { AbstractLevel } from './abstract-level'
2+
import { AbstractSnapshot } from './abstract-snapshot'
3+
14
export interface RangeOptions<K> {
25
gt?: K
36
gte?: K
@@ -6,3 +9,14 @@ export interface RangeOptions<K> {
69
reverse?: boolean | undefined
710
limit?: number | undefined
811
}
12+
13+
/**
14+
* Common options for read methods like {@link AbstractLevel.get} and
15+
* {@link AbstractLevel.iterator}.
16+
*/
17+
export interface AbstractReadOptions {
18+
/**
19+
* Explicit snapshot to read from.
20+
*/
21+
snapshot?: AbstractSnapshot | undefined
22+
}

0 commit comments

Comments
 (0)