Switching getMultiple to default to failure if not all entries found, then updating usages.
This commit is contained in:
parent
37e34c8ed2
commit
dde446286d
|
@ -119,7 +119,7 @@ export class DatabaseImpl implements Database {
|
||||||
|
|
||||||
async getMultiple<T extends Document>(
|
async getMultiple<T extends Document>(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
opts?: { failIfMissing?: boolean }
|
opts?: { allowMissing?: boolean }
|
||||||
): Promise<T[]> {
|
): Promise<T[]> {
|
||||||
// get unique
|
// get unique
|
||||||
ids = [...new Set(ids)]
|
ids = [...new Set(ids)]
|
||||||
|
@ -129,8 +129,9 @@ export class DatabaseImpl implements Database {
|
||||||
})
|
})
|
||||||
const NOT_FOUND = "not_found"
|
const NOT_FOUND = "not_found"
|
||||||
const rows = response.rows.filter(row => row.error !== 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
|
// 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 missing = response.rows.filter(row => row.error === NOT_FOUND)
|
||||||
const missingIds = missing.map(row => row.key).join(", ")
|
const missingIds = missing.map(row => row.key).join(", ")
|
||||||
throw new Error(`Unable to get documents: ${missingIds}`)
|
throw new Error(`Unable to get documents: ${missingIds}`)
|
||||||
|
|
|
@ -237,7 +237,8 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
|
||||||
|
|
||||||
// look up the actual rows based on the ids
|
// look up the actual rows based on the ids
|
||||||
let linkedRows = await db.getMultiple<Row>(
|
let linkedRows = await db.getMultiple<Row>(
|
||||||
linkVals.map(linkVal => linkVal.id)
|
linkVals.map(linkVal => linkVal.id),
|
||||||
|
{ allowMissing: true }
|
||||||
)
|
)
|
||||||
|
|
||||||
// get the linked tables
|
// get the linked tables
|
||||||
|
|
|
@ -79,7 +79,7 @@ async function getFullLinkedDocs(links: LinkDocumentValue[]) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const linkedRowIds = links.map(link => link.id)
|
const linkedRowIds = links.map(link => link.id)
|
||||||
const uniqueRowIds = [...new Set(linkedRowIds)]
|
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
|
// convert the unique db rows back to a full list of linked rows
|
||||||
const linked = linkedRowIds
|
const linked = linkedRowIds
|
||||||
.map(id => dbRows.find(row => row && row._id === id))
|
.map(id => dbRows.find(row => row && row._id === id))
|
||||||
|
|
|
@ -130,7 +130,9 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
|
||||||
}
|
}
|
||||||
if (internalTableIds.length) {
|
if (internalTableIds.length) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const internalTables = await db.getMultiple<Table>(internalTableIds)
|
const internalTables = await db.getMultiple<Table>(internalTableIds, {
|
||||||
|
allowMissing: true,
|
||||||
|
})
|
||||||
tables = tables.concat(internalTables)
|
tables = tables.concat(internalTables)
|
||||||
}
|
}
|
||||||
return processTables(tables)
|
return processTables(tables)
|
||||||
|
|
|
@ -96,7 +96,7 @@ export async function getRawGlobalUsers(userIds?: string[]): Promise<User[]> {
|
||||||
const db = tenancy.getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
let globalUsers: User[]
|
let globalUsers: User[]
|
||||||
if (userIds) {
|
if (userIds) {
|
||||||
globalUsers = await db.getMultiple<User>(userIds)
|
globalUsers = await db.getMultiple<User>(userIds, { allowMissing: true })
|
||||||
} else {
|
} else {
|
||||||
globalUsers = (
|
globalUsers = (
|
||||||
await db.allDocs<User>(
|
await db.allDocs<User>(
|
||||||
|
|
|
@ -125,7 +125,7 @@ export interface Database {
|
||||||
get<T extends Document>(id?: string): Promise<T>
|
get<T extends Document>(id?: string): Promise<T>
|
||||||
getMultiple<T extends Document>(
|
getMultiple<T extends Document>(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
opts?: { failIfMissing?: boolean }
|
opts?: { allowMissing?: boolean }
|
||||||
): Promise<T[]>
|
): Promise<T[]>
|
||||||
remove(
|
remove(
|
||||||
id: string | Document,
|
id: string | Document,
|
||||||
|
|
Loading…
Reference in New Issue