Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow extending store method options #193

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 9 additions & 7 deletions packages/interface-blockstore/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import type {
Options as StoreOptions,
AbortOptions,
AwaitIterable,
Store
} from 'interface-store'
import type { CID } from 'multiformats/cid'

export interface Options extends StoreOptions {

}

export interface Pair {
cid: CID
block: Uint8Array
}

export interface Blockstore extends Store<CID, Uint8Array, Pair> {
export interface Blockstore <HasOptionsExtension = {},
PutOptionsExtension = {}, PutManyOptionsExtension = {},
GetOptionsExtension = {}, GetManyOptionsExtension = {}, GetAllOptionsExtension = {},
DeleteOptionsExtension = {}, DeleteManyOptionsExtension = {}> extends Store<CID, Uint8Array, Pair, HasOptionsExtension,
PutOptionsExtension, PutManyOptionsExtension,
GetOptionsExtension, GetManyOptionsExtension,
DeleteOptionsExtension, DeleteManyOptionsExtension> {
/**
* Retrieve all cid/block pairs from the blockstore as an unordered iterable
*
Expand All @@ -26,5 +28,5 @@ export interface Blockstore extends Store<CID, Uint8Array, Pair> {
* }
* ```
*/
getAll: (options?: Options) => AwaitIterable<Pair>
getAll: (options?: AbortOptions & GetAllOptionsExtension) => AwaitIterable<Pair>
}
30 changes: 18 additions & 12 deletions packages/interface-datastore/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import type {
Options as StoreOptions,
Await,
Store
AwaitIterable,
Store,
AbortOptions
} from 'interface-store'
import { Key } from './key.js'

export interface Options extends StoreOptions {

}

export interface Pair {
key: Key
value: Uint8Array
}

export interface Batch {
export interface Batch<BatchOptionsExtension> {
put: (key: Key, value: Uint8Array) => void
delete: (key: Key) => void
commit: (options?: Options) => Await<void>
commit: (options?: AbortOptions & BatchOptionsExtension) => Await<void>
}

export interface Datastore extends Store<Key, Uint8Array, Pair> {
export interface Datastore <HasOptionsExtension = {},
PutOptionsExtension = {}, PutManyOptionsExtension = {},
GetOptionsExtension = {}, GetManyOptionsExtension = {},
DeleteOptionsExtension = {}, DeleteManyOptionsExtension = {},
QueryOptionsExtension = {}, QueryKeysOptionsExtension = {},
BatchOptionsExtension = {}
> extends Store<Key, Uint8Array, Pair, HasOptionsExtension,
PutOptionsExtension, PutManyOptionsExtension,
GetOptionsExtension, GetManyOptionsExtension,
DeleteOptionsExtension, DeleteManyOptionsExtension> {
/**
* This will return an object with which you can chain multiple operations together, with them only being executed on calling `commit`.
*
Expand All @@ -36,7 +42,7 @@ export interface Datastore extends Store<Key, Uint8Array, Pair> {
* console.log('put 100 values')
* ```
*/
batch: () => Batch
batch: () => Batch<BatchOptionsExtension>

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

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

export interface QueryFilter { (item: Pair): boolean }
Expand Down
28 changes: 19 additions & 9 deletions packages/interface-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@

/**
* An iterable or async iterable of values
*/
export type AwaitIterable<T> = Iterable<T> | AsyncIterable<T>

/**
* A value or a promise of a value
*/
export type Await<T> = Promise<T> | T

/**
* Options for async operations.
*/
export interface Options {
export interface AbortOptions {
signal?: AbortSignal
}

export interface Store<Key, Value, Pair> {
export interface Store<Key, Value, Pair, HasOptionsExtension = {},
PutOptionsExtension = {}, PutManyOptionsExtension = {},
GetOptionsExtension = {}, GetManyOptionsExtension = {},
DeleteOptionsExtension = {}, DeleteManyOptionsExtension = {}> {
/**
* Check for the existence of a value for the passed key
*
Expand All @@ -24,7 +34,7 @@ export interface Store<Key, Value, Pair> {
*}
*```
*/
has: (key: Key, options?: Options) => Await<boolean>
has: (key: Key, options?: AbortOptions & HasOptionsExtension) => Await<boolean>

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

/**
* Store the given key/value pairs
Expand All @@ -51,7 +61,7 @@ export interface Store<Key, Value, Pair> {
*/
putMany: (
source: AwaitIterable<Pair>,
options?: Options
options?: AbortOptions & PutManyOptionsExtension
) => AwaitIterable<Pair>

/**
Expand All @@ -64,7 +74,7 @@ export interface Store<Key, Value, Pair> {
* // => got content: datastore
* ```
*/
get: (key: Key, options?: Options) => Await<Value>
get: (key: Key, options?: AbortOptions & GetOptionsExtension) => Await<Value>

/**
* Retrieve values for the passed keys
Expand All @@ -79,7 +89,7 @@ export interface Store<Key, Value, Pair> {
*/
getMany: (
source: AwaitIterable<Key>,
options?: Options
options?: AbortOptions & GetManyOptionsExtension
) => AwaitIterable<Value>

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

/**
* Remove values for the passed keys
Expand All @@ -109,6 +119,6 @@ export interface Store<Key, Value, Pair> {
*/
deleteMany: (
source: AwaitIterable<Key>,
options?: Options
options?: AbortOptions & DeleteManyOptionsExtension
) => AwaitIterable<Key>
}