Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit c257b98

Browse files
authored
chore: implement interface-store (#91)
We have datastores and blockstores and blockstores are like datastores just with a different key/value types. Instead of retyping them, make datastores generic so blockstores can and datastores can be a specialisation of stores.
1 parent 9245723 commit c257b98

File tree

6 files changed

+47
-188
lines changed

6 files changed

+47
-188
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"scripts": {
1313
"prepare": "aegir build --no-bundle",
14-
"lint": "aegir lint",
14+
"lint": "aegir ts -p check && aegir lint",
1515
"test": "aegir test",
1616
"test:node": "aegir test --target node",
1717
"test:browser": "aegir test --target browser",
@@ -42,6 +42,7 @@
4242
},
4343
"dependencies": {
4444
"err-code": "^3.0.1",
45+
"interface-store": "^0.0.2",
4546
"ipfs-utils": "^7.0.0",
4647
"iso-random-stream": "^2.0.0",
4748
"it-all": "^1.0.2",

src/adapter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ const filter = require('it-filter')
66
const take = require('it-take')
77

88
/**
9+
* @typedef {import('interface-store').Options} Options
910
* @typedef {import('./key')} Key
1011
* @typedef {import('./types').Pair} Pair
1112
* @typedef {import('./types').Datastore} Datastore
12-
* @typedef {import('./types').Options} Options
1313
* @typedef {import('./types').Query} Query
1414
* @typedef {import('./types').KeyQuery} KeyQuery
1515
* @typedef {import('./types').Batch} Batch
1616
*/
1717

1818
/**
1919
* @template O
20-
* @typedef {import('./types').AwaitIterable<O>} AwaitIterable
20+
* @typedef {import('interface-store').AwaitIterable<O>} AwaitIterable
2121
*/
2222

2323
/**

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* @typedef {import('./types').Datastore} Datastore
55
* @typedef {import('./types').Batch} Batch
6-
* @typedef {import('./types').Options} Options
6+
* @typedef {import('interface-store').Options} Options
77
* @typedef {import('./types').Query} Query
88
* @typedef {import('./types').QueryFilter} QueryFilter
99
* @typedef {import('./types').QueryOrder} QueryOrder

src/memory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Errors = require('./errors')
77
/**
88
* @typedef {import('./types').Pair} Pair
99
* @typedef {import('./types').Datastore} Datastore
10-
* @typedef {import('./types').Options} Options
10+
* @typedef {import('interface-store').Options} Options
1111
*/
1212

1313
/**

src/types.d.ts

+40-172
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,53 @@
1+
import {
2+
Pair as StorePair,
3+
Batch as StoreBatch,
4+
QueryFilter as StoreQueryFilter,
5+
QueryOrder as StoreQueryOrder,
6+
Query as StoreQuery,
7+
KeyQueryFilter as StoreKeyQueryFilter,
8+
KeyQueryOrder as StoreKeyQueryOrder,
9+
KeyQuery as StoreKeyQuery,
10+
Options as StoreOptions,
11+
Store
12+
} from 'interface-store'
113
import type Key from './key'
214

3-
export type AwaitIterable<T> = Iterable<T> | AsyncIterable<T>
4-
export type Await<T> = Promise<T> | T
5-
export interface Pair {
6-
key: Key
7-
value: Uint8Array
15+
export interface Options extends StoreOptions{
16+
17+
}
18+
19+
export interface Pair extends StorePair<Key, Uint8Array> {
20+
21+
}
22+
23+
export interface Batch extends StoreBatch<Key, Uint8Array> {
24+
25+
}
26+
27+
export interface Datastore extends Store<Key, Uint8Array> {
28+
829
}
9-
/**
10-
* Options for async operations.
11-
*/
12-
export interface Options {
13-
signal?: AbortSignal
30+
31+
export interface QueryFilter extends StoreQueryFilter<Key, Uint8Array> {
32+
1433
}
1534

16-
export interface Batch {
17-
put: (key: Key, value: Uint8Array) => void
18-
delete: (key: Key) => void
19-
commit: (options?: Options) => Promise<void>
35+
export interface QueryOrder extends StoreQueryOrder<Key, Uint8Array> {
36+
2037
}
21-
export interface Datastore {
22-
open: () => Promise<void>
23-
close: () => Promise<void>
24-
/**
25-
* Store the passed value under the passed key
26-
*
27-
* @example
28-
*
29-
* ```js
30-
* await store.put([{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }])
31-
* ```
32-
*/
33-
put: (key: Key, val: Uint8Array, options?: Options) => Promise<void>
34-
/**
35-
* Retrieve the value stored under the given key
36-
*
37-
* @example
38-
* ```js
39-
* const value = await store.get(new Key('awesome'))
40-
* console.log('got content: %s', value.toString('utf8'))
41-
* // => got content: datastore
42-
* ```
43-
*/
44-
get: (key: Key, options?: Options) => Promise<Uint8Array>
45-
/**
46-
* Check for the existence of a value for the passed key
47-
*
48-
* @example
49-
* ```js
50-
*const exists = await store.has(new Key('awesome'))
51-
*
52-
*if (exists) {
53-
* console.log('it is there')
54-
*} else {
55-
* console.log('it is not there')
56-
*}
57-
*```
58-
*/
59-
has: (key: Key, options?: Options) => Promise<boolean>
60-
/**
61-
* Remove the record for the passed key
62-
*
63-
* @example
64-
*
65-
* ```js
66-
* await store.delete(new Key('awesome'))
67-
* console.log('deleted awesome content :(')
68-
* ```
69-
*/
70-
delete: (key: Key, options?: Options) => Promise<void>
71-
/**
72-
* Store the given key/value pairs
73-
*
74-
* @example
75-
* ```js
76-
* const source = [{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }]
77-
*
78-
* for await (const { key, value } of store.putMany(source)) {
79-
* console.info(`put content for key ${key}`)
80-
* }
81-
* ```
82-
*/
83-
putMany: (
84-
source: AwaitIterable<Pair>,
85-
options?: Options
86-
) => AsyncIterable<Pair>
87-
/**
88-
* Retrieve values for the passed keys
89-
*
90-
* @example
91-
* ```js
92-
* for await (const value of store.getMany([new Key('awesome')])) {
93-
* console.log('got content:', new TextDecoder('utf8').decode(value))
94-
* // => got content: datastore
95-
* }
96-
* ```
97-
*/
98-
getMany: (
99-
source: AwaitIterable<Key>,
100-
options?: Options
101-
) => AsyncIterable<Uint8Array>
102-
/**
103-
* Remove values for the passed keys
104-
*
105-
* @example
106-
*
107-
* ```js
108-
* const source = [new Key('awesome')]
109-
*
110-
* for await (const key of store.deleteMany(source)) {
111-
* console.log(`deleted content with key ${key}`)
112-
* }
113-
* ```
114-
*/
115-
deleteMany: (
116-
source: AwaitIterable<Key>,
117-
options?: Options
118-
) => AsyncIterable<Key>
119-
/**
120-
* This will return an object with which you can chain multiple operations together, with them only being executed on calling `commit`.
121-
*
122-
* @example
123-
* ```js
124-
* const b = store.batch()
125-
*
126-
* for (let i = 0; i < 100; i++) {
127-
* b.put(new Key(`hello${i}`), new TextEncoder('utf8').encode(`hello world ${i}`))
128-
* }
129-
*
130-
* await b.commit()
131-
* console.log('put 100 values')
132-
* ```
133-
*/
134-
batch: () => Batch
135-
/**
136-
* Query the store.
137-
*
138-
* @example
139-
* ```js
140-
* // retrieve __all__ key/value pairs from the store
141-
* let list = []
142-
* for await (const { key, value } of store.query({})) {
143-
* list.push(value)
144-
* }
145-
* console.log('ALL THE VALUES', list)
146-
* ```
147-
*/
148-
query: (query: Query, options?: Options) => AsyncIterable<Pair>
149-
/**
150-
* Query the store.
151-
*
152-
* @example
153-
* ```js
154-
* // retrieve __all__ keys from the store
155-
* let list = []
156-
* for await (const key of store.queryKeys({})) {
157-
* list.push(key)
158-
* }
159-
* console.log('ALL THE KEYS', key)
160-
* ```
161-
*/
162-
queryKeys: (query: KeyQuery, options?: Options) => AsyncIterable<Key>
38+
39+
export interface Query extends StoreQuery<Key, Uint8Array> {
40+
41+
}
42+
43+
export interface KeyQueryFilter extends StoreKeyQueryFilter<Key> {
44+
16345
}
16446

165-
export type QueryFilter = (item: Pair) => boolean
166-
export type QueryOrder = (a: Pair, b: Pair) => -1 | 0 | 1
47+
export interface KeyQueryOrder extends StoreKeyQueryOrder<Key> {
16748

168-
export interface Query {
169-
prefix?: string
170-
filters?: QueryFilter[]
171-
orders?: QueryOrder[]
172-
limit?: number
173-
offset?: number
17449
}
17550

176-
export type KeyQueryFilter = (item: Key) => boolean
177-
export type KeyQueryOrder = (a: Key, b: Key) => -1 | 0 | 1
51+
export interface KeyQuery extends StoreKeyQuery<Key> {
17852

179-
export interface KeyQuery {
180-
prefix?: string
181-
filters?: KeyQueryFilter[]
182-
orders?: KeyQueryOrder[]
183-
limit?: number
184-
offset?: number
18553
}

src/utils.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@
33
const tempdir = require('ipfs-utils/src/temp-dir')
44
const all = require('it-all')
55

6-
/**
7-
* @template T
8-
* @typedef {import("./types").Await<T>} PromiseOrValue
9-
*/
10-
11-
/**
12-
* @template T
13-
* @typedef {import("./types").AwaitIterable<T>} AnyIterable
14-
*/
15-
166
/**
177
* Collect all values from the iterable and sort them using
188
* the passed sorter function
199
*
2010
* @template T
21-
* @param {AnyIterable<T>} iterable
11+
* @param {AsyncIterable<T> | Iterable<T>} iterable
2212
* @param {(a: T, b: T) => -1 | 0 | 1} sorter
2313
* @returns {AsyncIterable<T>}
2414
*/

0 commit comments

Comments
 (0)