@ibm-cloud/cloudant
Version:
IBM Cloudant Node.js SDK
149 lines • 6.62 kB
JavaScript
;
/**
* © Copyright IBM Corporation 2025. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PagerType = exports.Pagination = void 0;
const node_stream_1 = require("node:stream");
const iteratorPager_1 = require("./iteratorPager");
const allDocsPageIterator_1 = require("./allDocsPageIterator");
const stream_1 = require("../stream");
const designDocsPageIterator_1 = require("./designDocsPageIterator");
const findPageIterator_1 = require("./findPageIterator");
const allDocsPartitionPageIterator_1 = require("./allDocsPartitionPageIterator");
const findPartitionPageIterator_1 = require("./findPartitionPageIterator");
const searchPartitionPageIterator_1 = require("./searchPartitionPageIterator");
const viewPageIterator_1 = require("./viewPageIterator");
const searchPageIterator_1 = require("./searchPageIterator");
const viewPartitionPageIterator_1 = require("./viewPartitionPageIterator");
/**
* {@link Pagination} is the entry point for pagination features.
*
* Use the static methods to create new {@link Pagination} instances. The instances in turn can be
* used to create:
* * {@link Stream} of result rows via {@link rowStream()}
* * {@link Stream} of pages via {@link pageStream()}
* * {@link AsyncIterableIterator} of result rows via {@link rows()}
* * {@link AsyncIterableIterator}s of pages via {@link pages()}
* * IBM Cloud SDK style {@link Pager}s via {@link pager()}
*
* @param <I> the result row type of the operation.
*/
class Pagination {
pageIterableIterator;
constructor(pageIterableIterator) {
this.pageIterableIterator = pageIterableIterator;
}
/**
* Get a new IBM Cloud SDK style Pager for the operation.
*
* This type is useful for retrieving one page at a time through a function call.
*
* @return a new IBM Cloud SDK style Pager
*/
pager() {
return new iteratorPager_1.IteratorPager(this.pageIterableIterator);
}
/**
* Get an AsyncIterableIterator for all the pages.
*
* This function is useful for handling pages in an enhanced for loop e.g.
* ```ts
* for await (const row: ReadonlyArray<DocsResultRow> of Pagination.newPagination(client, PagerType.POST_ALL_DOCS, allDocsParams).pages()){
* ...
* }
* ```
*
* @return an {@link Iterable} over all the pages
*/
pages() {
return this.pageIterableIterator;
}
/**
* Get a page by page stream of all the pages.
*
* @return a {@link Stream} of all the pages
*/
pageStream() {
return (0, node_stream_1.pipeline)(node_stream_1.Readable.from(this.pages()), new stream_1.Stream(), () => { });
}
/**
* Get an AsyncIterableIterator for all the rows from all the pages.
*
* This function is useful for handling rows in an enhanced for loop e.g.
* ```ts
* for await (const row: DocsResultRow of Pagination.newPagination(client, PagerType.POST_ALL_DOCS, allDocsParams).rows()) {
* ...
* }
* ```
*
* @return an {@link AsyncIterableIterator} over all the result rows
*/
async *rows() {
// eslint-disable-next-line no-restricted-syntax
for await (const row of this.rowStream()) {
yield row;
}
}
/**
* Get a row by row stream of all the rows from all the pages.
*
* @return a {@link Stream} of all the result rows
*/
rowStream() {
return (0, node_stream_1.pipeline)(node_stream_1.Readable.from(this.pages()).flatMap((item) => item), new stream_1.Stream(), () => { });
}
static newPagination(client, type, params) {
switch (type) {
case PagerType.POST_ALL_DOCS:
return new Pagination(new allDocsPageIterator_1.AllDocsPageIterator(client, params));
case PagerType.POST_DESIGN_DOCS:
return new Pagination(new designDocsPageIterator_1.DesignDocsPageIterator(client, params));
case PagerType.POST_FIND:
return new Pagination(new findPageIterator_1.FindPageIterator(client, params));
case PagerType.POST_PARTITION_ALL_DOCS:
return new Pagination(new allDocsPartitionPageIterator_1.AllDocsPartitionPageIterator(client, params));
case PagerType.POST_PARTITION_FIND:
return new Pagination(new findPartitionPageIterator_1.FindPartitionPageIterator(client, params));
case PagerType.POST_PARTITION_SEARCH:
return new Pagination(new searchPartitionPageIterator_1.SearchPartitionPageIterator(client, params));
case PagerType.POST_PARTITION_VIEW:
return new Pagination(new viewPartitionPageIterator_1.ViewPartitionPageIterator(client, params));
case PagerType.POST_SEARCH:
return new Pagination(new searchPageIterator_1.SearchPageIterator(client, params));
case PagerType.POST_VIEW:
return new Pagination(new viewPageIterator_1.ViewPageIterator(client, params));
default:
throw new Error(`No implementation available for PagerType ${type}.`);
}
}
}
exports.Pagination = Pagination;
/**
* Enumeration of the available Pager types
*/
var PagerType;
(function (PagerType) {
PagerType[PagerType["POST_ALL_DOCS"] = 0] = "POST_ALL_DOCS";
PagerType[PagerType["POST_DESIGN_DOCS"] = 1] = "POST_DESIGN_DOCS";
PagerType[PagerType["POST_FIND"] = 2] = "POST_FIND";
PagerType[PagerType["POST_PARTITION_ALL_DOCS"] = 3] = "POST_PARTITION_ALL_DOCS";
PagerType[PagerType["POST_PARTITION_FIND"] = 4] = "POST_PARTITION_FIND";
PagerType[PagerType["POST_PARTITION_SEARCH"] = 5] = "POST_PARTITION_SEARCH";
PagerType[PagerType["POST_PARTITION_VIEW"] = 6] = "POST_PARTITION_VIEW";
PagerType[PagerType["POST_SEARCH"] = 7] = "POST_SEARCH";
PagerType[PagerType["POST_VIEW"] = 8] = "POST_VIEW";
})(PagerType || (exports.PagerType = PagerType = {}));
//# sourceMappingURL=pagination.js.map