packageDocumentation module textile import { grpc } from '@improbable-eng/grpc-web' ; import { ContextInterface } from '@textile/context' ; import { FindByIDRequest , FindRequest , HasRequest , ReadTransactionReply , ReadTransactionRequest , StartTransactionRequest , } from '@textile/threads-client-grpc/threads_pb' ; import { ThreadID } from '@textile/threads-id' ; import { QueryJSON } from './query' ; import { Transaction } from './Transaction' ; const decoder = new TextDecoder ( ) ; const encoder = new TextEncoder ( ) ; example export class ReadTransaction extends Transaction < ReadTransactionRequest , ReadTransactionReply > { constructor ( protected readonly context , protected readonly client , protected readonly threadID , protected readonly modelName ) { super ( client , threadID , modelName ) ; } /**
     * start begins the transaction. All operations between start and end will be applied as a single transaction upon a call to end.
     */ public async start ( ) { const startReq = new StartTransactionRequest ( ) ; startReq . setDbid ( this . threadID . toBytes ( ) ) ; startReq . setCollectionname ( this . modelName ) ; const req = new ReadTransactionRequest ( ) ; req . setStarttransactionrequest ( startReq ) ; const metadata = JSON . parse ( JSON . stringify ( this . context ) ) ; this . client . start ( metadata ) ; this . client . send ( req ) ; } param IDs public async has ( IDs ) { return new Promise < boolean > ( ( resolve , reject ) => { const hasReq = new HasRequest ( ) ; hasReq . setInstanceidsList ( IDs ) ; const req = new ReadTransactionRequest ( ) ; req . setHasrequest ( hasReq ) ; this . client . onMessage ( ( message ) => { const reply = message . getHasreply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } resolve ( reply ? reply . getExists ( ) : false ) ; } ) ; this . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } param query public async find < T = unknown > ( query ) { return new Promise < Array < T > > ( ( resolve , reject ) => { const findReq = new FindRequest ( ) ; findReq . setQueryjson ( encoder . encode ( JSON . stringify ( query ) ) ) ; const req = new ReadTransactionRequest ( ) ; req . setFindrequest ( findReq ) ; this . client . onMessage ( ( message ) => { const reply = message . getFindreply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } if ( reply === undefined ) { resolve ( [ ] ) ; } else { const ret = reply . getInstancesList_asU8 ( ) . map ( ( instance ) => JSON . parse ( decoder . decode ( instance ) ) ) ; resolve ( ret ) ; } } ) ; this . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } param ID public async findByID < T = unknown > ( ID ) { return new Promise < T | undefined > ( ( resolve , reject ) => { const findReq = new FindByIDRequest ( ) ; findReq . setInstanceid ( ID ) ; const req = new ReadTransactionRequest ( ) ; req . setFindbyidrequest ( findReq ) ; this . client . onMessage ( ( message ) => { const reply = message . getFindbyidreply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } if ( reply === undefined ) { resolve ( undefined ) ; } else { resolve ( JSON . parse ( decoder . decode ( reply . getInstance_asU8 ( ) ) ) ) ; } } ) ; this . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } }