Created
January 15, 2025 04:44
-
-
Save 0x009922/6c2a83d737ec4aca3f7cad825820af15 to your computer and use it in GitHub Desktop.
Iroha JS v7.0.0 - missing feature of Query pagination and batches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ToriiRequirementsForApiHttp } from '@iroha2/client' | |
import { datamodel } from '@iroha2/data-model' | |
interface QueryParams { | |
start?: number | |
limit?: number | |
sortByMetadataKey?: string | |
} | |
async function* queryBatches( | |
pre: ToriiRequirementsForApiHttp, | |
query: datamodel.SignedQuery, | |
params?: QueryParams, | |
): AsyncGenerator<datamodel.Value[]> { | |
const baseParams: Record<string, string> = {} | |
if (params?.start) { | |
baseParams.start = String(params.start) | |
} | |
if (params?.limit) { | |
baseParams.limit = String(params.limit) | |
} | |
if (params?.sortByMetadataKey) { | |
baseParams.sort_by_metadata_key = params.sortByMetadataKey | |
} | |
type Cursor = { cursor: bigint; query_id: string } | |
let next: null | Cursor = null | |
do { | |
const urlParams = new URLSearchParams( | |
next ? { cursor: String(next.cursor), query_id: next.query_id } : baseParams, | |
) | |
const response = await pre.fetch(`${pre.apiURL}/query?${urlParams}`, { | |
method: 'POST', | |
body: datamodel.SignedQuery.toBuffer(query), | |
}) | |
const { | |
batch, | |
cursor: { cursor, query_id }, | |
} = datamodel.BatchedResponseValue.fromBuffer(new Uint8Array(await response.arrayBuffer())).enum.as('V1') | |
yield batch.enum.as('Vec') | |
if (cursor.enum.tag === 'Some') { | |
next = { cursor: cursor.enum.content, query_id: query_id.enum.as('Some') } | |
} else next = null | |
} while (next) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment