Historian for a particular participant

后端 未结 1 1385
鱼传尺愫
鱼传尺愫 2020-12-18 17:30

Is there any way in which I can get Historian for a particular participant in hyperledger-composer using node API?

I am developing an application based on hyperledge

相关标签:
1条回答
  • 2020-12-18 17:41

    you can return results from REST API calls since v0.20 to the calling client application, so something like the following would work (not tested, but you get the idea). NOTE: You could just call the REST API end (/GET Trader) direct via REST with your parameter (or whatever endpoints you create for your own business network - the example below is trade-network), rather than the example of using 'READ-ONLY' Transaction processor Endpoint described below, for returning larger result sets to your client application. See more on this in the docs

    NODE JS Client using APIs:


        const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
    
        const rp = require('request-promise');
    
        this.bizNetworkConnection = new BusinessNetworkConnection();
        this.cardName ='admin@mynet';
        this.businessNetworkIdentifier = 'mynet';
    
        this.bizNetworkConnection.connect(this.cardName)
        .then((result) => { 
    
        //You can do ANYTHING HERE eg.
    
        })
        .catch((error) => {
        throw error;
        });
    
        // set up my read only transaction object - find the history of a particular Participant - note it could equally be an Asset instead !
    
        var obj = {
            "$class": "org.example.trading.MyPartHistory",
            "tradeId": "P1"
        };
    
    
        async function callPartHistory() {
    
        var options = {
            method: 'POST',
            uri: 'http://localhost:3000/api/MyPartHistory',
            body: obj,
            json: true 
        };
    
        let results = await rp(options);
        //    console.log("Return value from REST API is " + results);
        console.log(" ");
        console.log(`PARTICIPANT HISTORY for Asset ID:  ${results[0].tradeId} is: `); 
        console.log("=============================================");
    
        for (const part of results) {
             console.log(`${part.tradeId}             ${part.name}` );
        }
       }
    
       // Main
    
       callPartHistory();
    

    // MODEL FILE


    @commit(false)
    @returns(Trader[])
    transaction MyPartHistory {
    o String tradeId
    }
    

    READ-ONLY TRANSACTION PROCESSOR CODE (in 'logic.js') :


    /**
     * Sample read-only transaction
     * @param {org.example.trading.MyPartHistory} tx
     * @returns {org.example.trading.Trader[]} All trxns  
     * @transaction
     */
    
    
    async function participantHistory(tx) {
    
        const partId = tx.tradeid;
        const nativeSupport = tx.nativeSupport;
        // const partRegistry = await getParticipantRegistry('org.example.trading.Trader')
    
        const nativeKey = getNativeAPI().createCompositeKey('Asset:org.example.trading.Trader', [partId]);
        const iterator = await getNativeAPI().getHistoryForKey(nativeKey);
        let results = [];
        let res = {done : false};
        while (!res.done) {
            res = await iterator.next();
    
            if (res && res.value && res.value.value) {
                let val = res.value.value.toString('utf8');
                if (val.length > 0) {
                   console.log("@debug val is  " + val );
                   results.push(JSON.parse(val));
                }
            }
            if (res && res.done) {
                try {
                    iterator.close();
                }
                catch (err) {
                }
            }
        }
        var newArray = [];
        for (const item of results) {
                newArray.push(getSerializer().fromJSON(item));
        }
        console.log("@debug the results to be returned are as follows: ");
    
        return newArray; // returns something to my NodeJS client (called via REST API)
    }
    
    0 讨论(0)
提交回复
热议问题