Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit cd5c704

Browse files
committed
implement node.getByOrigin
1 parent c407211 commit cd5c704

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

archaeologist/src/storage_api_local.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { v4 as uuidv4 } from 'uuid'
4040
import base32Encode from 'base32-encode'
4141

4242
import browser from 'webextension-polyfill'
43-
import { MimeType, unixtime } from 'armoury'
43+
import { genOriginId, MimeType, unixtime } from 'armoury'
4444
import lodash from 'lodash'
4545

4646
// TODO[snikitin@outlook.com] Describe that "yek" is "key" in reverse,
@@ -338,6 +338,26 @@ async function getNode({
338338
return NodeUtil.fromJson(value)
339339
}
340340

341+
async function getNodesByOrigin({
342+
store,
343+
origin,
344+
}: {
345+
store: YekLavStore
346+
origin: OriginId
347+
}): Promise<TNode[]> {
348+
const yek: OriginToNidYek = { yek: { kind: 'origin->nid', key: origin } }
349+
const lav: OriginToNidLav | undefined = await store.get(yek)
350+
if (lav == null) {
351+
return []
352+
}
353+
const value: Nid[] = lav.lav.value
354+
const nidYeks: NidYek[] = value.map((nid: Nid): NidYek => {
355+
return { yek: { kind: 'nid', key: nid } }
356+
})
357+
const nidLavs: NidLav[] = await store.get(nidYeks)
358+
return nidLavs.map((lav: NidLav) => NodeUtil.fromJson(lav.lav.value))
359+
}
360+
341361
async function getNodeBatch(
342362
store: YekLavStore,
343363
req: NodeBatchRequestBody
@@ -539,7 +559,8 @@ export function makeLocalStorageApi(
539559
node: {
540560
get: ({ nid }: { nid: string; signal?: AbortSignal }) =>
541561
getNode({ store, nid }),
542-
getByOrigin: throwUnimplementedError('node.getByOrigin'),
562+
getByOrigin: ({ origin }: { origin: OriginId; signal?: AbortSignal }) =>
563+
getNodesByOrigin({ store, origin }),
543564
update: (
544565
args: { nid: string } & NodePatchRequest,
545566
_signal?: AbortSignal

0 commit comments

Comments
 (0)