Switching getMultiple to default to failure if not all entries found, then updating usages.

This commit is contained in:
mike12345567 2023-11-09 17:08:14 +00:00
parent 37e34c8ed2
commit dde446286d
6 changed files with 11 additions and 7 deletions

View File

@ -119,7 +119,7 @@ export class DatabaseImpl implements Database {
async getMultiple<T extends Document>(
ids: string[],
opts?: { failIfMissing?: boolean }
opts?: { allowMissing?: boolean }
): Promise<T[]> {
// get unique
ids = [...new Set(ids)]
@ -129,8 +129,9 @@ export class DatabaseImpl implements Database {
})
const NOT_FOUND = "not_found"
const rows = response.rows.filter(row => row.error !== NOT_FOUND)
const someMissing = rows.length !== response.rows.length
// some were filtered out - means some missing
if (opts?.failIfMissing && rows.length !== response.rows.length) {
if (!opts?.allowMissing && someMissing) {
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}`)

View File

@ -237,7 +237,8 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
// look up the actual rows based on the ids
let linkedRows = await db.getMultiple<Row>(
linkVals.map(linkVal => linkVal.id)
linkVals.map(linkVal => linkVal.id),
{ allowMissing: true }
)
// get the linked tables

View File

@ -79,7 +79,7 @@ async function getFullLinkedDocs(links: LinkDocumentValue[]) {
const db = context.getAppDB()
const linkedRowIds = links.map(link => link.id)
const uniqueRowIds = [...new Set(linkedRowIds)]
let dbRows = await db.getMultiple<Row>(uniqueRowIds)
let dbRows = await db.getMultiple<Row>(uniqueRowIds, { allowMissing: true })
// convert the unique db rows back to a full list of linked rows
const linked = linkedRowIds
.map(id => dbRows.find(row => row && row._id === id))

View File

@ -130,7 +130,9 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
}
if (internalTableIds.length) {
const db = context.getAppDB()
const internalTables = await db.getMultiple<Table>(internalTableIds)
const internalTables = await db.getMultiple<Table>(internalTableIds, {
allowMissing: true,
})
tables = tables.concat(internalTables)
}
return processTables(tables)

View File

@ -96,7 +96,7 @@ export async function getRawGlobalUsers(userIds?: string[]): Promise<User[]> {
const db = tenancy.getGlobalDB()
let globalUsers: User[]
if (userIds) {
globalUsers = await db.getMultiple<User>(userIds)
globalUsers = await db.getMultiple<User>(userIds, { allowMissing: true })
} else {
globalUsers = (
await db.allDocs<User>(

View File

@ -125,7 +125,7 @@ export interface Database {
get<T extends Document>(id?: string): Promise<T>
getMultiple<T extends Document>(
ids: string[],
opts?: { failIfMissing?: boolean }
opts?: { allowMissing?: boolean }
): Promise<T[]>
remove(
id: string | Document,