Adding the ability to fail on getMultiple if needed.

This commit is contained in:
mike12345567 2023-11-09 14:53:14 +00:00
parent a26f2e83e4
commit 37e34c8ed2
2 changed files with 16 additions and 3 deletions

View File

@ -117,14 +117,24 @@ export class DatabaseImpl implements Database {
return this.updateOutput(() => db.get(id))
}
async getMultiple<T extends Document>(ids: string[]): Promise<T[]> {
async getMultiple<T extends Document>(
ids: string[],
opts?: { failIfMissing?: boolean }
): Promise<T[]> {
// get unique
ids = [...new Set(ids)]
const response = await this.allDocs<T>({
keys: ids,
include_docs: true,
})
const rows = response.rows.filter(row => row.error !== "not_found")
const NOT_FOUND = "not_found"
const rows = response.rows.filter(row => row.error !== NOT_FOUND)
// some were filtered out - means some missing
if (opts?.failIfMissing && rows.length !== response.rows.length) {
const missing = response.rows.filter(row => row.error === NOT_FOUND)
const missingIds = missing.map(row => row.key).join(", ")
throw new Error(`Unable to get documents: ${missingIds}`)
}
return rows.map(row => row.doc!)
}

View File

@ -123,7 +123,10 @@ export interface Database {
exists(): Promise<boolean>
checkSetup(): Promise<Nano.DocumentScope<any>>
get<T extends Document>(id?: string): Promise<T>
getMultiple<T extends Document>(ids: string[]): Promise<T[]>
getMultiple<T extends Document>(
ids: string[],
opts?: { failIfMissing?: boolean }
): Promise<T[]>
remove(
id: string | Document,
rev?: string