Skip to content

Commit 014148b

Browse files
committed
docs(core): extend docs
Signed-off-by: 0x009922 <[email protected]>
1 parent 42678d0 commit 014148b

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

packages/core/query.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export type QueryBuilderCtorArgs<Q extends QueryKind> = GetQueryPayload<Q> exten
2929

3030
export type DefaultQueryOutput<Q extends QueryKind> = SelectedTuple<prototypes.QuerySelectors[Q]>
3131

32+
/**
33+
* Utility to build queries in a safe and convenient way.
34+
*
35+
* It is a lower-level utility that only builds queries. For the higher-level
36+
* implementation which actually sends the queries, see `QueryBuilder` from `@iroha/client` package.
37+
*/
3238
export class QueryBuilder<Q extends QueryKind, Output = DefaultQueryOutput<Q>> {
3339
protected kind: Q
3440
protected payload: GetQueryPayload<Q>
@@ -37,7 +43,7 @@ export class QueryBuilder<Q extends QueryKind, Output = DefaultQueryOutput<Q>> {
3743
protected predicate: types.CompoundPredicate<unknown> = types.CompoundPredicate.PASS
3844
protected parseOutput: (resp: types.QueryOutputBatchBoxTuple) => Generator<Output> = generateOutputTuples<Output>
3945

40-
constructor(...args: QueryBuilderCtorArgs<Q>) {
46+
public constructor(...args: QueryBuilderCtorArgs<Q>) {
4147
this.kind = args[0]
4248

4349
if ((QUERIES_WITH_PAYLOAD as Set<QueryKind>).has(this.kind)) {
@@ -58,6 +64,29 @@ export class QueryBuilder<Q extends QueryKind, Output = DefaultQueryOutput<Q>> {
5864
}
5965
}
6066

67+
/**
68+
* Specify selected tuple with a _prototype_ of the querying object.
69+
*
70+
* @example Select a single value
71+
*
72+
* ```ts
73+
* import * as types from '@iroha/core/data-model'
74+
*
75+
* const builder: QueryBuilder<'FindAccounts', types.DomainId> =
76+
* new QueryBuilder('FindAccounts')
77+
* .selectWith((account) => account.id.domain)
78+
* ```
79+
*
80+
* @example Select multiple values
81+
*
82+
* ```ts
83+
* import * as types from '@iroha/core/data-model'
84+
*
85+
* const builder: QueryBuilder<'FindTransactions', [types.Hash, null | types.TransactionRejectionReason]> =
86+
* new QueryBuilder('FindTransactions')
87+
* .selectWith((tx) => [tx.value.hash, tx.error])
88+
* ```
89+
*/
6190
public selectWith<const ProtoTuple>(
6291
fn: (prototype: prototypes.QuerySelectors[Q]) => ProtoTuple,
6392
): QueryBuilder<Q, SelectedTuple<ProtoTuple>> {
@@ -67,6 +96,24 @@ export class QueryBuilder<Q extends QueryKind, Output = DefaultQueryOutput<Q>> {
6796
return this as QueryBuilder<Q, SelectedTuple<ProtoTuple>>
6897
}
6998

99+
/**
100+
* Specify predicate with a _prototype_ of the querying object.
101+
*
102+
* The returned type must be a variant {@linkcode [data-model].CompoundPredicate | CompoundPredicate} containing
103+
* the actual predicate.
104+
*
105+
* @example Specifying a compound logical predicate
106+
*
107+
* ```ts
108+
* import * as types from '@iroha/core/data-model'
109+
*
110+
* new QueryBuilder('FindAccounts')
111+
* .filterWith((account) => types.CompoundPredicate.Or(
112+
* types.CompoundPredicate.Atom(account.id.domain.name.startsWith('wonder')),
113+
* types.CompoundPredicate.Atom(account.id.domain.name.endsWith('land')),
114+
* ))
115+
* ```
116+
*/
70117
public filterWith(fn: (prototype: prototypes.QueryPredicates[Q]) => types.CompoundPredicate<PredicateOf<Q>>): this {
71118
const proto = predicateProto<Q>()
72119
this.predicate = fn(proto)

0 commit comments

Comments
 (0)