Skip to content

Commit

Permalink
Merge pull request #155 from oliver-oloughlin/feature/handle-many-res…
Browse files Browse the repository at this point in the history
…ult-limit

Feature/handle many result limit
  • Loading branch information
oliver-oloughlin authored Dec 26, 2023
2 parents 1fd34d2 + 031b0ac commit bbf327f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
EnqueueOptions,
FindManyOptions,
FindOptions,
HandleManyOptions,
HistoryEntry,
IdempotentListener,
IdGenerator,
Expand Down Expand Up @@ -1949,7 +1950,7 @@ export class Collection<
protected async handleMany<const T>(
prefixKey: KvKey,
fn: (doc: Document<TOutput>) => T,
options: ListOptions<Document<TOutput>> | undefined,
options: HandleManyOptions<Document<TOutput>> | undefined,
) {
// Create list iterator with given options
const selector = createListSelector(prefixKey, options)
Expand All @@ -1960,6 +1961,7 @@ export class Collection<
const docs: Document<TOutput>[] = []
const result: Awaited<T>[] = []
const errors: unknown[] = []
const resultLimit = options?.resultLimit

// Loop over each document entry
let count = 0
Expand All @@ -1972,6 +1974,11 @@ export class Collection<
continue
}

// Check if result limit reached
if (resultLimit && result.length >= resultLimit) {
break
}

// Construct document from entry
const doc = await this.constructDocument(entry)

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ export type ListOptions<T> = Deno.KvListOptions & {
endId?: KvId
}

export type HandleManyOptions<T> = ListOptions<T> & {
resultLimit?: number
}

export type AtomicBatchOptions = {
/** Batch size of atomic operations where applicable */
atomicBatchSize?: number
Expand Down

0 comments on commit bbf327f

Please sign in to comment.