Skip to content

Commit 252bced

Browse files
authored
fix!: return stream of pairs from getmany (#195)
To make the return value of getMany consistent with putMany and getAll, return a stream of pairs from the return type of getMany - this also allows decoupling of processing the output from the input since the output processor doesn't need any extra context since they get both the key and the value being returned. BREAKING CHANGE: the output of store.getMany is now a stream of pairs
1 parent 40f500b commit 252bced

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

packages/interface-blockstore-tests/src/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function interfaceBlockstoreTests <B extends Blockstore = Blockstore> (te
6767
await Promise.all(data.map(async d => { await store.put(d.cid, d.block) }))
6868

6969
const res = await all(store.getMany(data.map(d => d.cid)))
70-
expect(res).to.deep.equal(data.map(d => d.block))
70+
expect(res).to.deep.equal(data)
7171
})
7272
})
7373

@@ -95,7 +95,7 @@ export function interfaceBlockstoreTests <B extends Blockstore = Blockstore> (te
9595
expect(index).to.equal(data.length)
9696

9797
const res = await all(store.getMany(data.map(d => d.cid)))
98-
expect(res).to.deep.equal(data.map(d => d.block))
98+
expect(res).to.deep.equal(data)
9999
})
100100
})
101101

@@ -158,7 +158,8 @@ export function interfaceBlockstoreTests <B extends Blockstore = Blockstore> (te
158158

159159
const res = await all(store.getMany(source))
160160
expect(res).to.have.lengthOf(1)
161-
expect(res[0]).to.equalBytes(block)
161+
expect(res[0].cid).to.deep.equal(cid)
162+
expect(res[0].block).to.equalBytes(block)
162163
})
163164

164165
it('should throw error for missing key', async () => {

packages/interface-datastore-tests/src/index.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { expect } from 'aegir/chai'
55
import all from 'it-all'
66
import drain from 'it-drain'
77
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8-
import { Datastore, Key, KeyQueryFilter, KeyQueryOrder, QueryFilter, QueryOrder } from 'interface-datastore'
8+
import { Datastore, Key, KeyQueryFilter, KeyQueryOrder, Pair, QueryFilter, QueryOrder } from 'interface-datastore'
99
import length from 'it-length'
1010

1111
export interface InterfacDatastoreTest<D extends Datastore = Datastore> {
@@ -37,15 +37,15 @@ export function interfaceDatastoreTests <D extends Datastore = Datastore> (test:
3737
})
3838

3939
it('parallel', async () => {
40-
const data = []
40+
const data: Pair[] = []
4141
for (let i = 0; i < 100; i++) {
4242
data.push({ key: new Key(`/z/key${i}`), value: uint8ArrayFromString(`data${i}`) })
4343
}
4444

4545
await Promise.all(data.map(async d => { await store.put(d.key, d.value) }))
4646

4747
const res = await all(store.getMany(data.map(d => d.key)))
48-
expect(res).to.deep.equal(data.map(d => d.value))
48+
expect(res).to.deep.equal(data)
4949
})
5050
})
5151

@@ -59,7 +59,7 @@ export function interfaceDatastoreTests <D extends Datastore = Datastore> (test:
5959
afterEach(async () => { await cleanup(store) })
6060

6161
it('streaming', async () => {
62-
const data = []
62+
const data: Pair[] = []
6363
for (let i = 0; i < 100; i++) {
6464
data.push({ key: new Key(`/z/key${i}`), value: uint8ArrayFromString(`data${i}`) })
6565
}
@@ -74,7 +74,7 @@ export function interfaceDatastoreTests <D extends Datastore = Datastore> (test:
7474
expect(index).to.equal(data.length)
7575

7676
const res = await all(store.getMany(data.map(d => d.key)))
77-
expect(res).to.deep.equal(data.map(d => d.value))
77+
expect(res).to.deep.equal(data)
7878
})
7979
})
8080

@@ -124,7 +124,8 @@ export function interfaceDatastoreTests <D extends Datastore = Datastore> (test:
124124

125125
const res = await all(store.getMany(source))
126126
expect(res).to.have.lengthOf(1)
127-
expect(res[0]).to.be.eql(uint8ArrayFromString('hello'))
127+
expect(res[0].key).to.be.eql(k)
128+
expect(res[0].value).to.be.eql(uint8ArrayFromString('hello'))
128129
})
129130

130131
it('should throw error for missing key', async () => {

packages/interface-store/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ export interface Store<Key, Value, Pair, HasOptionsExtension = {},
8181
*
8282
* @example
8383
* ```js
84-
* for await (const value of store.getMany([new Key('awesome')])) {
85-
* console.log('got content:', new TextDecoder('utf8').decode(value))
86-
* // => got content: datastore
84+
* for await (const { key, value } of store.getMany([new Key('awesome')])) {
85+
* console.log(`got "${key}" = "${new TextDecoder('utf8').decode(value)}"`')
86+
* // => got "/awesome" = "datastore"
8787
* }
8888
* ```
8989
*/
9090
getMany: (
9191
source: AwaitIterable<Key>,
9292
options?: AbortOptions & GetManyOptionsExtension
93-
) => AwaitIterable<Value>
93+
) => AwaitIterable<Pair>
9494

9595
/**
9696
* Remove the record for the passed key

0 commit comments

Comments
 (0)