Adding the ability to fail on getMultiple if needed.
This commit is contained in:
parent
a26f2e83e4
commit
37e34c8ed2
|
@ -117,14 +117,24 @@ export class DatabaseImpl implements Database {
|
||||||
return this.updateOutput(() => db.get(id))
|
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
|
// get unique
|
||||||
ids = [...new Set(ids)]
|
ids = [...new Set(ids)]
|
||||||
const response = await this.allDocs<T>({
|
const response = await this.allDocs<T>({
|
||||||
keys: ids,
|
keys: ids,
|
||||||
include_docs: true,
|
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!)
|
return rows.map(row => row.doc!)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,10 @@ export interface Database {
|
||||||
exists(): Promise<boolean>
|
exists(): Promise<boolean>
|
||||||
checkSetup(): Promise<Nano.DocumentScope<any>>
|
checkSetup(): Promise<Nano.DocumentScope<any>>
|
||||||
get<T extends Document>(id?: string): Promise<T>
|
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(
|
remove(
|
||||||
id: string | Document,
|
id: string | Document,
|
||||||
rev?: string
|
rev?: string
|
||||||
|
|
Loading…
Reference in New Issue