1
0
Fork 0
mirror of synced 2024-08-03 20:31:50 +12:00

Handle skip on execution

This commit is contained in:
adrinr 2023-03-16 09:59:10 +01:00
parent 3b07f0e1a2
commit bf32801917

View file

@ -56,6 +56,7 @@ export class QueryBuilder<T> {
#version?: string
#indexBuilder?: () => Promise<any>
#noEscaping = false
#skip?: number
constructor(dbName: string, index: string, base?: SearchFilters) {
this.#dbName = dbName
@ -138,6 +139,11 @@ export class QueryBuilder<T> {
return this
}
setSkip(skip: number | undefined) {
this.#skip = skip
return this
}
excludeDocs() {
this.#includeDocs = false
return this
@ -468,6 +474,34 @@ export class QueryBuilder<T> {
}
async run() {
if (this.#skip) {
await this.#skipPages(this.#skip)
}
return await this.#execute()
}
async #skipPages(skip: number) {
// Lucene does not support pagination.
// Handle pagination by finding the right bookmark
const prevIncludeDocs = this.#includeDocs
const prevLimit = this.#limit
this.excludeDocs()
const maxPageSize = 1000
let skipRemaining = skip
do {
const toSkip = Math.min(maxPageSize, skipRemaining)
this.setLimit(toSkip)
const { bookmark } = await this.#execute()
this.setBookmark(bookmark)
skipRemaining -= toSkip
} while (skipRemaining > 0)
this.#includeDocs = prevIncludeDocs
this.#limit = prevLimit
}
async #execute() {
const { url, cookie } = getCouchInfo()
const fullPath = `${url}/${this.#dbName}/_design/database/_search/${
this.#index