From 7fd6edb662897b4ae7bef8c1dded0a9459070f4c Mon Sep 17 00:00:00 2001 From: David Faulstich Diniz Reis Date: Thu, 25 Nov 2021 12:51:51 -0300 Subject: [PATCH] Query assets with pagination functions of marbles02 and asset_transfer_ledger_chaincode chaincodes without ResponseMetadata fetchedRecordsCount and bookmark (#547) Signed-off-by: David Faulstich Diniz Reis --- .../application-javascript/app.js | 9 ++++++++- .../application-javascript/package.json | 1 + .../lib/asset_transfer_ledger_chaincode.js | 17 +++++++++++------ .../marbles02/javascript/marbles_chaincode.js | 16 ++++++++++++---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/asset-transfer-ledger-queries/application-javascript/app.js b/asset-transfer-ledger-queries/application-javascript/app.js index bc33a89c..73fc83a1 100644 --- a/asset-transfer-ledger-queries/application-javascript/app.js +++ b/asset-transfer-ledger-queries/application-javascript/app.js @@ -191,7 +191,14 @@ async function main() { // Rich Query with Pagination (Only supported if CouchDB is used as state database) console.log('\n--> Evaluate Transaction: QueryAssetsWithPagination, function returns "Tom" assets'); - result = await contract.evaluateTransaction('QueryAssetsWithPagination', '{"selector":{"docType":"asset","owner":"Tom"}, "use_index":["_design/indexOwnerDoc", "indexOwner"]}', '3', ''); + result = await contract.evaluateTransaction('QueryAssetsWithPagination', '{"selector":{"docType":"asset","owner":"Tom"}, "use_index":["_design/indexOwnerDoc", "indexOwner"]}', '1', ''); + console.log(`*** Result: ${prettyJSONString(result.toString())}`); + + // Recover the bookmark from previous query. Normally it will be inside a variable. + const resultJson = JSON.parse(result.toString()); + + console.log('\n--> Evaluate Transaction: QueryAssetsWithPagination, function returns "Tom" assets next page'); + result = await contract.evaluateTransaction('QueryAssetsWithPagination', '{"selector":{"docType":"asset","owner":"Tom"}, "use_index":["_design/indexOwnerDoc", "indexOwner"]}', '1', resultJson.ResponseMetadata.Bookmark); console.log(`*** Result: ${prettyJSONString(result.toString())}`); console.log('\n--> Submit Transaction: TransferAssetByColor, transfer all yellow assets to new owner(Michel)'); diff --git a/asset-transfer-ledger-queries/application-javascript/package.json b/asset-transfer-ledger-queries/application-javascript/package.json index 8d92146a..a8a35073 100644 --- a/asset-transfer-ledger-queries/application-javascript/package.json +++ b/asset-transfer-ledger-queries/application-javascript/package.json @@ -10,6 +10,7 @@ "author": "Hyperledger", "license": "Apache-2.0", "scripts": { + "run": "node app.js", "lint": "eslint *.js" }, "dependencies": { diff --git a/asset-transfer-ledger-queries/chaincode-javascript/lib/asset_transfer_ledger_chaincode.js b/asset-transfer-ledger-queries/chaincode-javascript/lib/asset_transfer_ledger_chaincode.js index da83bb58..1fe1d29d 100644 --- a/asset-transfer-ledger-queries/chaincode-javascript/lib/asset_transfer_ledger_chaincode.js +++ b/asset-transfer-ledger-queries/chaincode-javascript/lib/asset_transfer_ledger_chaincode.js @@ -62,10 +62,10 @@ // curl -i -X POST -H "Content-Type: application/json" -d "{\"index\":{\"fields\":[{\"size\":\"desc\"},{\"docType\":\"desc\"},{\"owner\":\"desc\"}]},\"ddoc\":\"indexSizeSortDoc\", \"name\":\"indexSizeSortDesc\",\"type\":\"json\"}" http://hostname:port/myc1_assets/_index // Rich Query with index design doc and index name specified (Only supported if CouchDB is used as state database): -// peer chaincode query -C CHANNEL_NAME -n asset_transfer -c '{"Args":["QueryAssets","{\"selector\":{\"docType\":\"asset\",\"owner\":\"Tom\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}"]}' +// peer chaincode query -C CHANNEL_NAME -n ledger -c '{"Args":["QueryAssets","{\"selector\":{\"docType\":\"asset\",\"owner\":\"Tom\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}"]}' // Rich Query with index design doc specified only (Only supported if CouchDB is used as state database): -// peer chaincode query -C CHANNEL_NAME -n asset_transfer -c '{"Args":["QueryAssets","{\"selector\":{\"docType\":{\"$eq\":\"asset\"},\"owner\":{\"$eq\":\"Tom\"},\"size\":{\"$gt\":0}},\"fields\":[\"docType\",\"owner\",\"size\"],\"sort\":[{\"size\":\"desc\"}],\"use_index\":\"_design/indexSizeSortDoc\"}"]}' +// peer chaincode query -C CHANNEL_NAME -n ledger -c '{"Args":["QueryAssets","{\"selector\":{\"docType\":{\"$eq\":\"asset\"},\"owner\":{\"$eq\":\"Tom\"},\"size\":{\"$gt\":0}},\"fields\":[\"docType\",\"owner\",\"size\"],\"sort\":[{\"size\":\"desc\"}],\"use_index\":\"_design/indexSizeSortDoc\"}"]}' 'use strict'; @@ -262,12 +262,15 @@ class Chaincode extends Contract { async GetAssetsByRangeWithPagination(ctx, startKey, endKey, pageSize, bookmark) { const {iterator, metadata} = await ctx.stub.getStateByRangeWithPagination(startKey, endKey, pageSize, bookmark); - const results = await this._GetAllResults(iterator, false); + let results = {}; + + results.results = await this._GetAllResults(iterator, false); results.ResponseMetadata = { - RecordsCount: metadata.fetched_records_count, + RecordsCount: metadata.fetchedRecordsCount, Bookmark: metadata.bookmark, }; + return JSON.stringify(results); } @@ -282,10 +285,12 @@ class Chaincode extends Contract { async QueryAssetsWithPagination(ctx, queryString, pageSize, bookmark) { const {iterator, metadata} = await ctx.stub.getQueryResultWithPagination(queryString, pageSize, bookmark); - const results = await this._GetAllResults(iterator, false); + let results = {}; + + results.results = await this._GetAllResults(iterator, false); results.ResponseMetadata = { - RecordsCount: metadata.fetched_records_count, + RecordsCount: metadata.fetchedRecordsCount, Bookmark: metadata.bookmark, }; diff --git a/chaincode/marbles02/javascript/marbles_chaincode.js b/chaincode/marbles02/javascript/marbles_chaincode.js index 7536178c..a4b3547f 100644 --- a/chaincode/marbles02/javascript/marbles_chaincode.js +++ b/chaincode/marbles02/javascript/marbles_chaincode.js @@ -440,12 +440,17 @@ let Chaincode = class { const { iterator, metadata } = await stub.getStateByRangeWithPagination(startKey, endKey, pageSize, bookmark); const getAllResults = thisClass['getAllResults']; - const results = await getAllResults(iterator, false); + + let results = {}; + + results.results = await getAllResults(iterator, false); + // use RecordsCount and Bookmark to keep consistency with the go sample results.ResponseMetadata = { - RecordsCount: metadata.fetched_records_count, + RecordsCount: metadata.fetchedRecordsCount, Bookmark: metadata.bookmark, }; + return Buffer.from(JSON.stringify(results)); } @@ -467,10 +472,13 @@ let Chaincode = class { const { iterator, metadata } = await stub.getQueryResultWithPagination(queryString, pageSize, bookmark); const getAllResults = thisClass['getAllResults']; - const results = await getAllResults(iterator, false); + let results = {}; + + results.results = await getAllResults(iterator, false); + // use RecordsCount and Bookmark to keep consistency with the go sample results.ResponseMetadata = { - RecordsCount: metadata.fetched_records_count, + RecordsCount: metadata.fetchedRecordsCount, Bookmark: metadata.bookmark, };