import { QLDBSession } from "aws-sdk" ; import { AbortTransactionResult , CommitDigest , CommitTransactionResult , EndSessionResult , ExecuteStatementResult , FetchPageResult , PageToken , SendCommandRequest , SendCommandResult , StartTransactionResult , ValueHolder } from "aws-sdk/clients/qldbsession" ; import { inspect } from "util" ; import { debug , warn } from "./LogUtil" ; internal export class Communicator { private _qldbClient : QLDBSession ; private _sessionToken : string ; param qldbClient param sessionToken private constructor ( qldbClient , sessionToken ) { this . _qldbClient = qldbClient ; this . _sessionToken = sessionToken ; } param qldbClient param ledgerName returns static async create ( qldbClient , ledgerName ) { const request = { StartSession : { LedgerName : ledgerName } } ; const result = await qldbClient . sendCommand ( request ) . promise ( ) ; return new Communicator ( qldbClient , result . StartSession . SessionToken ) ; } returns async abortTransaction ( ) { const request = { SessionToken : this . _sessionToken , AbortTransaction : { } } ; const result = await this . _sendCommand ( request ) ; return result . AbortTransaction ; } param txnId param commitDigest returns async commit ( txnId , commitDigest ) { const request = { SessionToken : this . _sessionToken , CommitTransaction : { TransactionId : txnId , CommitDigest : commitDigest } } ; const result = await this . _sendCommand ( request ) ; return result . CommitTransaction ; } param txnId param statement param parameters returns async executeStatement ( txnId , statement , parameters ) { const request = { SessionToken : this . _sessionToken , ExecuteStatement : { Statement : statement , TransactionId : txnId , Parameters : parameters } } ; const result = await this . _sendCommand ( request ) ; return result . ExecuteStatement ; } returns async endSession ( ) { const request = { SessionToken : this . _sessionToken , EndSession : { } } ; const result = await this . _sendCommand ( request ) ; return result . EndSession ; } param txnId param pageToken returns async fetchPage ( txnId , pageToken ) { const request = { SessionToken : this . _sessionToken , FetchPage : { TransactionId : txnId , NextPageToken : pageToken } } ; const result = await this . _sendCommand ( request ) ; return result . FetchPage ; } returns getQldbClient ( ) { return this . _qldbClient ; } returns getSessionToken ( ) { return this . _sessionToken ; } returns async startTransaction ( ) { const request = { SessionToken : this . _sessionToken , StartTransaction : { } } ; const result = await this . _sendCommand ( request ) ; return result . StartTransaction ; } param request returns private async _sendCommand ( request ) { try { const result = await this . _qldbClient . sendCommand ( request ) . promise ( ) ; debug ( `Received response: ${ inspect ( result , { depth : 2 } ) }` ) ; return result ; } catch ( e ) { warn ( `Error sending a command: ${ e }.` ) ; throw e ; } } }