mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-21 09:05:10 +00:00
[FAB-12026] pagination samples for node marbles02
This change synchronizes the node marbles02 sample chaincode with fabric/examples/marbles02 Change-Id: I6c11fee15ae39950fca7344fbf230c42f876274a Signed-off-by: zhaochy <zhaochy_2015@hotmail.com>
This commit is contained in:
parent
6dc5ce503b
commit
24c5e47539
1 changed files with 74 additions and 2 deletions
|
|
@ -18,10 +18,14 @@
|
||||||
// peer chaincode query -C myc1 -n marbles -c '{"Args":["readMarble","marble1"]}'
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["readMarble","marble1"]}'
|
||||||
// peer chaincode query -C myc1 -n marbles -c '{"Args":["getMarblesByRange","marble1","marble3"]}'
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["getMarblesByRange","marble1","marble3"]}'
|
||||||
// peer chaincode query -C myc1 -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}'
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}'
|
||||||
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["getMarblesByRangeWithPagination","marble1","marble3","3",""]}'
|
||||||
|
|
||||||
// Rich Query (Only supported if CouchDB is used as state database):
|
// Rich Query (Only supported if CouchDB is used as state database):
|
||||||
// peer chaincode query -C myc1 -n marbles -c '{"Args":["queryMarblesByOwner","tom"]}'
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["queryMarblesByOwner","tom"]}'
|
||||||
// peer chaincode query -C myc1 -n marbles -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"tom\"}}"]}'
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"tom\"}}"]}'
|
||||||
|
|
||||||
|
// Rich Query with Pagination (Only supported if CouchDB is used as state database):
|
||||||
|
// peer chaincode query -C myc1 -n marbles -c '{"Args":["queryMarblesWithPagination","{\"selector\":{\"owner\":\"tom\"}}","3",""]}'
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
const shim = require('fabric-shim');
|
const shim = require('fabric-shim');
|
||||||
|
|
@ -404,6 +408,74 @@ let Chaincode = class {
|
||||||
|
|
||||||
return Buffer.from(JSON.stringify(results));
|
return Buffer.from(JSON.stringify(results));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ====== Pagination =========================================================================
|
||||||
|
// Pagination provides a method to retrieve records with a defined pagesize and
|
||||||
|
// start point (bookmark). An empty string bookmark defines the first "page" of a query
|
||||||
|
// result. Paginated queries return a bookmark that can be used in
|
||||||
|
// the next query to retrieve the next page of results. Paginated queries extend
|
||||||
|
// rich queries and range queries to include a pagesize and bookmark.
|
||||||
|
//
|
||||||
|
// Two examples are provided in this example. The first is getMarblesByRangeWithPagination
|
||||||
|
// which executes a paginated range query.
|
||||||
|
// The second example is a paginated query for rich ad-hoc queries.
|
||||||
|
// =========================================================================================
|
||||||
|
|
||||||
|
// ====== Example: Pagination with Range Query ===============================================
|
||||||
|
// getMarblesByRangeWithPagination performs a range query based on the start & end key,
|
||||||
|
// page size and a bookmark.
|
||||||
|
//
|
||||||
|
// The number of fetched records will be equal to or lesser than the page size.
|
||||||
|
// Paginated range queries are only valid for read only transactions.
|
||||||
|
// ===========================================================================================
|
||||||
|
async getMarblesByRangeWithPagination(stub, args, thisClass) {
|
||||||
|
if (args.length < 2) {
|
||||||
|
throw new Error('Incorrect number of arguments. Expecting 2');
|
||||||
|
}
|
||||||
|
const startKey = args[0];
|
||||||
|
const endKey = args[1];
|
||||||
|
|
||||||
|
const pageSize = parseInt(args[2], 10);
|
||||||
|
const bookmark = args[3];
|
||||||
|
|
||||||
|
const { iterator, metadata } = await stub.getStateByRangeWithPagination(startKey, endKey, pageSize, bookmark);
|
||||||
|
const getAllResults = thisClass['getAllResults'];
|
||||||
|
const results = await getAllResults(iterator, false);
|
||||||
|
// use RecordsCount and Bookmark to keep consistency with the go sample
|
||||||
|
results.ResponseMetadata = {
|
||||||
|
RecordsCount: metadata.fetched_records_count,
|
||||||
|
Bookmark: metadata.bookmark,
|
||||||
|
};
|
||||||
|
return Buffer.from(JSON.stringify(results));
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================================
|
||||||
|
// getQueryResultForQueryStringWithPagination executes the passed in query string with
|
||||||
|
// pagination info. Result set is built and returned as a byte array containing the JSON results.
|
||||||
|
// =========================================================================================
|
||||||
|
async queryMarblesWithPagination(stub, args, thisClass) {
|
||||||
|
|
||||||
|
// 0
|
||||||
|
// "queryString"
|
||||||
|
if (args.length < 3) {
|
||||||
|
return shim.Error("Incorrect number of arguments. Expecting 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
const queryString = args[0];
|
||||||
|
const pageSize = parseInt(args[2], 10);
|
||||||
|
const bookmark = args[3];
|
||||||
|
|
||||||
|
const { iterator, metadata } = await stub.GetQueryResultWithPagination(queryString, pageSize, bookmark);
|
||||||
|
const getAllResults = thisClass['getAllResults'];
|
||||||
|
const results = await getAllResults(iterator, false);
|
||||||
|
// use RecordsCount and Bookmark to keep consistency with the go sample
|
||||||
|
results.ResponseMetadata = {
|
||||||
|
RecordsCount: metadata.fetched_records_count,
|
||||||
|
Bookmark: metadata.bookmark,
|
||||||
|
};
|
||||||
|
|
||||||
|
return Buffer.from(JSON.stringify(results));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
shim.start(new Chaincode());
|
shim.start(new Chaincode());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue