Skip to content

Commit 007e8ac

Browse files
authored
feat: allow extending store method options (#193)
To allow for extra options like progress handlers, add generic types for extending the options types passed to the various store methods. They are all defaulted to an empty interface (e.g. no extension) so this is a non-breaking change.
1 parent 6660830 commit 007e8ac

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import type {
2-
Options as StoreOptions,
2+
AbortOptions,
33
AwaitIterable,
44
Store
55
} from 'interface-store'
66
import type { CID } from 'multiformats/cid'
77

8-
export interface Options extends StoreOptions {
9-
10-
}
11-
128
export interface Pair {
139
cid: CID
1410
block: Uint8Array
1511
}
1612

17-
export interface Blockstore extends Store<CID, Uint8Array, Pair> {
13+
export interface Blockstore <HasOptionsExtension = {},
14+
PutOptionsExtension = {}, PutManyOptionsExtension = {},
15+
GetOptionsExtension = {}, GetManyOptionsExtension = {}, GetAllOptionsExtension = {},
16+
DeleteOptionsExtension = {}, DeleteManyOptionsExtension = {}> extends Store<CID, Uint8Array, Pair, HasOptionsExtension,
17+
PutOptionsExtension, PutManyOptionsExtension,
18+
GetOptionsExtension, GetManyOptionsExtension,
19+
DeleteOptionsExtension, DeleteManyOptionsExtension> {
1820
/**
1921
* Retrieve all cid/block pairs from the blockstore as an unordered iterable
2022
*
@@ -26,5 +28,5 @@ export interface Blockstore extends Store<CID, Uint8Array, Pair> {
2628
* }
2729
* ```
2830
*/
29-
getAll: (options?: Options) => AwaitIterable<Pair>
31+
getAll: (options?: AbortOptions & GetAllOptionsExtension) => AwaitIterable<Pair>
3032
}

packages/interface-datastore/src/index.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import type {
2-
Options as StoreOptions,
32
Await,
4-
Store
3+
AwaitIterable,
4+
Store,
5+
AbortOptions
56
} from 'interface-store'
67
import { Key } from './key.js'
78

8-
export interface Options extends StoreOptions {
9-
10-
}
11-
129
export interface Pair {
1310
key: Key
1411
value: Uint8Array
1512
}
1613

17-
export interface Batch {
14+
export interface Batch<BatchOptionsExtension> {
1815
put: (key: Key, value: Uint8Array) => void
1916
delete: (key: Key) => void
20-
commit: (options?: Options) => Await<void>
17+
commit: (options?: AbortOptions & BatchOptionsExtension) => Await<void>
2118
}
2219

23-
export interface Datastore extends Store<Key, Uint8Array, Pair> {
20+
export interface Datastore <HasOptionsExtension = {},
21+
PutOptionsExtension = {}, PutManyOptionsExtension = {},
22+
GetOptionsExtension = {}, GetManyOptionsExtension = {},
23+
DeleteOptionsExtension = {}, DeleteManyOptionsExtension = {},
24+
QueryOptionsExtension = {}, QueryKeysOptionsExtension = {},
25+
BatchOptionsExtension = {}
26+
> extends Store<Key, Uint8Array, Pair, HasOptionsExtension,
27+
PutOptionsExtension, PutManyOptionsExtension,
28+
GetOptionsExtension, GetManyOptionsExtension,
29+
DeleteOptionsExtension, DeleteManyOptionsExtension> {
2430
/**
2531
* This will return an object with which you can chain multiple operations together, with them only being executed on calling `commit`.
2632
*
@@ -36,7 +42,7 @@ export interface Datastore extends Store<Key, Uint8Array, Pair> {
3642
* console.log('put 100 values')
3743
* ```
3844
*/
39-
batch: () => Batch
45+
batch: () => Batch<BatchOptionsExtension>
4046

4147
/**
4248
* Query the datastore.
@@ -51,7 +57,7 @@ export interface Datastore extends Store<Key, Uint8Array, Pair> {
5157
* console.log('ALL THE VALUES', list)
5258
* ```
5359
*/
54-
query: (query: Query, options?: Options) => AsyncIterable<Pair>
60+
query: (query: Query, options?: AbortOptions & QueryOptionsExtension) => AwaitIterable<Pair>
5561

5662
/**
5763
* Query the datastore.
@@ -66,7 +72,7 @@ export interface Datastore extends Store<Key, Uint8Array, Pair> {
6672
* console.log('ALL THE KEYS', key)
6773
* ```
6874
*/
69-
queryKeys: (query: KeyQuery, options?: Options) => AsyncIterable<Key>
75+
queryKeys: (query: KeyQuery, options?: AbortOptions & QueryKeysOptionsExtension) => AwaitIterable<Key>
7076
}
7177

7278
export interface QueryFilter { (item: Pair): boolean }

packages/interface-store/src/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11

2+
/**
3+
* An iterable or async iterable of values
4+
*/
25
export type AwaitIterable<T> = Iterable<T> | AsyncIterable<T>
6+
7+
/**
8+
* A value or a promise of a value
9+
*/
310
export type Await<T> = Promise<T> | T
411

512
/**
613
* Options for async operations.
714
*/
8-
export interface Options {
15+
export interface AbortOptions {
916
signal?: AbortSignal
1017
}
1118

12-
export interface Store<Key, Value, Pair> {
19+
export interface Store<Key, Value, Pair, HasOptionsExtension = {},
20+
PutOptionsExtension = {}, PutManyOptionsExtension = {},
21+
GetOptionsExtension = {}, GetManyOptionsExtension = {},
22+
DeleteOptionsExtension = {}, DeleteManyOptionsExtension = {}> {
1323
/**
1424
* Check for the existence of a value for the passed key
1525
*
@@ -24,7 +34,7 @@ export interface Store<Key, Value, Pair> {
2434
*}
2535
*```
2636
*/
27-
has: (key: Key, options?: Options) => Await<boolean>
37+
has: (key: Key, options?: AbortOptions & HasOptionsExtension) => Await<boolean>
2838

2939
/**
3040
* Store the passed value under the passed key
@@ -35,7 +45,7 @@ export interface Store<Key, Value, Pair> {
3545
* await store.put([{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }])
3646
* ```
3747
*/
38-
put: (key: Key, val: Value, options?: Options) => Await<void>
48+
put: (key: Key, val: Value, options?: AbortOptions & PutOptionsExtension) => Await<void>
3949

4050
/**
4151
* Store the given key/value pairs
@@ -51,7 +61,7 @@ export interface Store<Key, Value, Pair> {
5161
*/
5262
putMany: (
5363
source: AwaitIterable<Pair>,
54-
options?: Options
64+
options?: AbortOptions & PutManyOptionsExtension
5565
) => AwaitIterable<Pair>
5666

5767
/**
@@ -64,7 +74,7 @@ export interface Store<Key, Value, Pair> {
6474
* // => got content: datastore
6575
* ```
6676
*/
67-
get: (key: Key, options?: Options) => Await<Value>
77+
get: (key: Key, options?: AbortOptions & GetOptionsExtension) => Await<Value>
6878

6979
/**
7080
* Retrieve values for the passed keys
@@ -79,7 +89,7 @@ export interface Store<Key, Value, Pair> {
7989
*/
8090
getMany: (
8191
source: AwaitIterable<Key>,
82-
options?: Options
92+
options?: AbortOptions & GetManyOptionsExtension
8393
) => AwaitIterable<Value>
8494

8595
/**
@@ -92,7 +102,7 @@ export interface Store<Key, Value, Pair> {
92102
* console.log('deleted awesome content :(')
93103
* ```
94104
*/
95-
delete: (key: Key, options?: Options) => Await<void>
105+
delete: (key: Key, options?: AbortOptions & DeleteOptionsExtension) => Await<void>
96106

97107
/**
98108
* Remove values for the passed keys
@@ -109,6 +119,6 @@ export interface Store<Key, Value, Pair> {
109119
*/
110120
deleteMany: (
111121
source: AwaitIterable<Key>,
112-
options?: Options
122+
options?: AbortOptions & DeleteManyOptionsExtension
113123
) => AwaitIterable<Key>
114124
}

0 commit comments

Comments
 (0)