mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
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 <davidfdr@gmail.com>
This commit is contained in:
parent
ce66638035
commit
7fd6edb662
4 changed files with 32 additions and 11 deletions
|
|
@ -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)');
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
"author": "Hyperledger",
|
||||
"license": "Apache-2.0",
|
||||
"scripts": {
|
||||
"run": "node app.js",
|
||||
"lint": "eslint *.js"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue