Handle skip on execution
This commit is contained in:
parent
3b07f0e1a2
commit
bf32801917
|
@ -56,6 +56,7 @@ export class QueryBuilder<T> {
|
||||||
#version?: string
|
#version?: string
|
||||||
#indexBuilder?: () => Promise<any>
|
#indexBuilder?: () => Promise<any>
|
||||||
#noEscaping = false
|
#noEscaping = false
|
||||||
|
#skip?: number
|
||||||
|
|
||||||
constructor(dbName: string, index: string, base?: SearchFilters) {
|
constructor(dbName: string, index: string, base?: SearchFilters) {
|
||||||
this.#dbName = dbName
|
this.#dbName = dbName
|
||||||
|
@ -138,6 +139,11 @@ export class QueryBuilder<T> {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setSkip(skip: number | undefined) {
|
||||||
|
this.#skip = skip
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
excludeDocs() {
|
excludeDocs() {
|
||||||
this.#includeDocs = false
|
this.#includeDocs = false
|
||||||
return this
|
return this
|
||||||
|
@ -468,6 +474,34 @@ export class QueryBuilder<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
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 { url, cookie } = getCouchInfo()
|
||||||
const fullPath = `${url}/${this.#dbName}/_design/database/_search/${
|
const fullPath = `${url}/${this.#dbName}/_design/database/_search/${
|
||||||
this.#index
|
this.#index
|
||||||
|
|
Loading…
Reference in New Issue