I have more than 500 records in my blockchain network and i want to write a chaincode for the pagination on frontend. I have used getQueryResultWithPagination
and getStateByRangeWithPagination
but my concern is that i want to fetch records starting from 90th to 100th means 10 records but not starting from beginning neither i want to fetch the whole record. My chaincode is written in nodejs. I'm stuck on this and would really appreciate the community to give me their valuable suggestions.

- 635
- 1
- 5
- 15
-
Are you trying to page through all the results from your front end or are you specifically trying to start with record N? – Gari Singh Sep 02 '19 at 14:22
-
suppose i want to fetch transactions starting from 50 to 100, how to do that ? because i dont' want to fetch all the records and then manipulate on front-end/backend. I just want to fetch data between two ranges. – Brajesh Singh Sep 02 '19 at 14:29
-
How are you identifying these transactions? The state database stores key/value pairs ... can you give a real example of what asking for records 50-100 would mean? The pagination feature allows you to return "pages" of records ... the combination of page size and bookmark allows you to move through records ... the query simply needs to pass in the bookmark each time to get the next group of records. – Gari Singh Sep 02 '19 at 20:59
-
@GariSingh is there any way to introduce jump to ? like if i want to go to page 30 from page 1. how can i achieve that ? – Adarsha Jha Sep 06 '19 at 06:27
2 Answers
Are you looking for directly jumping to a page if so, then its not possible. One drawback of the linked list style pagination is that you can’t pre-compute the rows for a particular page from the page number and the rows per page. Jumping to a specific page doesn’t really work.
Just see here for the reference: https://docs.couchdb.org/en/stable/ddocs/views/pagination.html

- 867
- 6
- 15
-
2As I know, we may pass skip and limit in couch db query - { "selector": {}, "skip": 100, "limit": 5 } – Brajesh Singh Sep 03 '19 at 07:25
-
That won't work here, page size and bookmark are the options that are quite similar to skip and limit. But if you want to access some records that are suppose after 500th record of 100 page size i.e. between 500-600 records then the problem is to fetch 500 records first then get the bookmark upto that point and then fetch 100 records using 100 page size and the bookmark you got above. But this is not optimal solution so you have to use some hybrid logic based on indexes to query it more optimally. – Trinayan Sep 03 '19 at 12:04
getQueryResultWithPagination
of course only works with CouchDB ... so when using the CouchDB query language you should be able to specify the skip
parameter in your actual query. Note that per the documentation, the limit
parameter is not honored as the page size parameter is used:
If a pageSize is specified using the paginated query APIs (GetStateByRangeWithPagination(), GetStateByPartialCompositeKeyWithPagination(), and GetQueryResultWithPagination()), a set of results (bound by the pageSize) will be returned to the chaincode along with a bookmark. The bookmark can be returned from chaincode to invoking clients, which can use the bookmark in a follow on query to receive the next “page” of results.
Of course to build a front-end client which pages through results, you need to pass both the pageSize
and bookmark
parameters to getQueryResultWithPagination
and your chain code function will need to return the bookmark
to the caller so that it can be passed in to fetch the next page of results.

- 11,418
- 2
- 18
- 41
-
Explain to me why this is being down voted? This answer is correct ... you use skip and pageSize (and bookmark if you want to page) – Gari Singh Sep 03 '19 at 11:06