Throughout the BitBadges API, we use a bookmark-based pagination system for efficient data retrieval. This system is particularly useful when dealing with large datasets that need to be fetched in smaller chunks.
Some endpoints like the get accounts or get collections will use a generic viewsToFetch and views object which can maintain multiple paginated views. Some endpoints will request the bookmark directly. Refer to the documentation for each endpoint to see how it handles pagination.
How Bookmark Pagination Works
First Request: For your initial request, use an empty bookmark string ("").
constfirstRequest={viewType:'owners',viewId:'owners',bookmark:'',// Empty for first page};
Response Structure: Each paginated response includes:
The requested data
A bookmark string for the next page
A hasMore boolean indicating if more data exists
{data: [...],views:{'owners': {ids: [...],pagination:{bookmark:'abc123...',// Use this for next requesthasMore:true}}}}
Subsequent Requests: To fetch the next page, use the bookmark from the previous response:
Completion: Continue this process until hasMore is false.
Understanding the Views Object
The views object is a central concept in the BitBadges API, used to manage paginated data across different interfaces (BitBadgesCollection, BitBadgesUserInfo, etc). It follows this structure:
Key Components
viewId: A unique identifier for the view
ids: Array of document IDs that correspond to full documents in the response
pagination: Contains the bookmark and hasMore flag
type: Indicates the type of view (e.g., 'owners', 'activity', etc.)
Document ID Mapping
Documents in the response are referenced by their _docId field. To access the full document, you map the IDs from the view to the corresponding array in the response. For example, the activity view documents are stored in the activity array, and the view IDs are stored in the views.activity.ids array.
Common View Types
Different interfaces support different view types. See the corresponding documentation for each interface to see what views are supported.
Note: This is planning to be deprecated in favor of more confined view routes.
Please use those instead. They are much less confusing.
views: {
[viewId: string]: {
ids: string[]; // Array of document IDs
pagination: {
bookmark: string; // Bookmark for next page
hasMore: boolean; // Whether more data exists
};
type: string; // The view type
} | undefined;
}
// Example of accessing activity documents from a view
getActivityView(viewId: string) {
return (this.views[viewId]?.ids.map((x) => {
return this.activity.find((y) => y._docId === x);
}) ?? []);
}
// Check if more pages exist
const hasMore = collection.viewHasMore('owners');
// Get bookmark for next page
const nextBookmark = collection.getViewBookmark('owners');
// Fetch next page of data
await collection.fetchNextForView(BitBadgesApi, 'owners', 'owners');
// Get all documents for a view
const ownersView = collection.getOwnersView('owners');