packageDocumentation module textile import { grpc } from "@improbable-eng/grpc-web" ; import { ContextInterface } from "@textile/context" ; import { CreateRequest , DeleteRequest , DiscardRequest , FindByIDRequest , FindRequest , HasRequest , SaveRequest , StartTransactionRequest , VerifyRequest , WriteTransactionReply , WriteTransactionRequest , } 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 example export class WriteTransaction extends Transaction < WriteTransactionRequest , WriteTransactionReply > { 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 WriteTransactionRequest ( ) ; req . setStarttransactionrequest ( startReq ) ; const metadata = JSON . parse ( JSON . stringify ( this . context ) ) ; this . client . start ( metadata ) ; this . client . send ( req ) ; } param values public async create < T = unknown > ( values ) { return new Promise < Array < string > > ( ( resolve , reject ) => { const createReq = new CreateRequest ( ) ; const list = [ ] ; values . forEach ( ( v ) => { list . push ( encoder . encode ( JSON . stringify ( v ) ) ) ; } ) ; createReq . setInstancesList ( list ) ; const req = new WriteTransactionRequest ( ) ; req . setCreaterequest ( createReq ) ; this . client . onMessage ( ( message ) => { const reply = message . getCreatereply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } if ( reply === undefined ) { resolve ( [ ] ) ; } else { resolve ( reply . toObject ( ) . instanceidsList ) ; } } ) ; super . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } param values public async verify < T = unknown > ( values ) { return new Promise < void > ( ( resolve , reject ) => { const innerRequest = new VerifyRequest ( ) ; const list = values . map ( ( v ) => encoder . encode ( JSON . stringify ( v ) ) ) ; innerRequest . setInstancesList ( list ) ; const req = new WriteTransactionRequest ( ) ; req . setVerifyrequest ( innerRequest ) ; this . client . onMessage ( ( message ) => { const reply = message . getVerifyreply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } resolve ( ) ; } ) ; super . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } param values public async save < T = unknown > ( values ) { return new Promise < void > ( ( resolve , reject ) => { const saveReq = new SaveRequest ( ) ; const list = [ ] ; values . forEach ( ( v ) => { if ( ! ( "_id" in v ) ) { ; ( v as any ) . _id = "" ; } list . push ( encoder . encode ( JSON . stringify ( v ) ) ) ; } ) ; saveReq . setInstancesList ( list ) ; const req = new WriteTransactionRequest ( ) ; req . setSaverequest ( saveReq ) ; this . client . onMessage ( ( message ) => { const reply = message . getSavereply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } resolve ( ) ; } ) ; super . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } param IDs public async delete ( IDs ) { return new Promise < void > ( ( resolve , reject ) => { const deleteReq = new DeleteRequest ( ) ; deleteReq . setInstanceidsList ( IDs ) ; const req = new WriteTransactionRequest ( ) ; req . setDeleterequest ( deleteReq ) ; this . client . onMessage ( ( message ) => { const reply = message . getDeletereply ( ) ; const err = reply ?. getTransactionerror ( ) ; if ( err ) { reject ( new Error ( err ) ) ; } resolve ( ) ; } ) ; super . setReject ( reject ) ; 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 WriteTransactionRequest ( ) ; 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 ) ; } ) ; super . 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 WriteTransactionRequest ( ) ; 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 WriteTransactionRequest ( ) ; 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 ) ; } ) ; } example public async discard ( ) { return new Promise < void > ( ( resolve , reject ) => { const req = new WriteTransactionRequest ( ) ; req . setDiscardrequest ( new DiscardRequest ( ) ) ; this . client . onMessage ( ( message ) => { const reply = message . getDiscardreply ( ) ; if ( reply ) { resolve ( ) ; } else { reject ( new Error ( "unexpected response type" ) ) ; } } ) ; super . setReject ( reject ) ; this . client . send ( req ) ; } ) ; } }