Fix tests.

This commit is contained in:
Sam Rose 2024-10-10 15:49:00 +01:00
parent a00a64bb6e
commit 4c4429b88a
No known key found for this signature in database
6 changed files with 39 additions and 8 deletions

View File

@ -211,6 +211,17 @@ export class DatabaseImpl implements Database {
}) })
} }
async tryGet<T extends Document>(id?: string): Promise<T | undefined> {
try {
return await this.get<T>(id)
} catch (err: any) {
if (err.statusCode === 404) {
return undefined
}
throw err
}
}
async getMultiple<T extends Document>( async getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean; excludeDocs?: boolean } opts?: { allowMissing?: boolean; excludeDocs?: boolean }

View File

@ -42,6 +42,13 @@ export class DDInstrumentedDatabase implements Database {
}) })
} }
tryGet<T extends Document>(id?: string | undefined): Promise<T | undefined> {
return tracer.trace("db.tryGet", span => {
span?.addTags({ db_name: this.name, doc_id: id })
return this.db.tryGet(id)
})
}
getMultiple<T extends Document>( getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean | undefined } | undefined opts?: { allowMissing?: boolean | undefined } | undefined

View File

@ -21,14 +21,15 @@ export async function find(ctx: Ctx<void, RowActionsResponse>) {
const table = await getTable(ctx) const table = await getTable(ctx)
const tableId = table._id! const tableId = table._id!
if (!(await sdk.rowActions.docExists(tableId))) { const rowActions = await sdk.rowActions.getAll(tableId)
if (!rowActions) {
ctx.body = { ctx.body = {
actions: {}, actions: {},
} }
return return
} }
const { actions } = await sdk.rowActions.getAll(tableId) const { actions } = rowActions
const result: RowActionsResponse = { const result: RowActionsResponse = {
actions: Object.entries(actions).reduce<Record<string, RowActionResponse>>( actions: Object.entries(actions).reduce<Record<string, RowActionResponse>>(
(acc, [key, action]) => ({ (acc, [key, action]) => ({

View File

@ -26,13 +26,13 @@ export async function getBuilderData(
return tableNameCache[tableId] return tableNameCache[tableId]
} }
const rowActionNameCache: Record<string, TableRowActions> = {} const rowActionNameCache: Record<string, TableRowActions | undefined> = {}
async function getRowActionName(tableId: string, rowActionId: string) { async function getRowActionName(tableId: string, rowActionId: string) {
if (!rowActionNameCache[tableId]) { if (!rowActionNameCache[tableId]) {
rowActionNameCache[tableId] = await sdk.rowActions.getAll(tableId) rowActionNameCache[tableId] = await sdk.rowActions.getAll(tableId)
} }
return rowActionNameCache[tableId].actions[rowActionId]?.name return rowActionNameCache[tableId]?.actions[rowActionId]?.name
} }
const result: Record<string, AutomationBuilderData> = {} const result: Record<string, AutomationBuilderData> = {}
@ -51,6 +51,10 @@ export async function getBuilderData(
const tableName = await getTableName(tableId) const tableName = await getTableName(tableId)
const rowActionName = await getRowActionName(tableId, rowActionId) const rowActionName = await getRowActionName(tableId, rowActionId)
if (!rowActionName) {
throw new Error(`Row action not found: ${rowActionId}`)
}
result[automation._id!] = { result[automation._id!] = {
displayName: `${tableName}: ${automation.name}`, displayName: `${tableName}: ${automation.name}`,
triggerInfo: { triggerInfo: {

View File

@ -103,13 +103,17 @@ export async function get(tableId: string, rowActionId: string) {
export async function getAll(tableId: string) { export async function getAll(tableId: string) {
const db = context.getAppDB() const db = context.getAppDB()
const rowActionsId = generateRowActionsID(tableId) const rowActionsId = generateRowActionsID(tableId)
return await db.get<TableRowActions>(rowActionsId) return await db.tryGet<TableRowActions>(rowActionsId)
} }
export async function deleteAll(tableId: string) { export async function deleteAll(tableId: string) {
const db = context.getAppDB() const db = context.getAppDB()
const doc = await getAll(tableId) const doc = await getAll(tableId)
if (!doc) {
return
}
const automationIds = Object.values(doc.actions).map(a => a.automationId) const automationIds = Object.values(doc.actions).map(a => a.automationId)
const automations = await db.getMultiple<Automation>(automationIds) const automations = await db.getMultiple<Automation>(automationIds)
@ -238,9 +242,8 @@ export async function run(tableId: any, rowActionId: any, rowId: string) {
throw new HTTPError("Table not found", 404) throw new HTTPError("Table not found", 404)
} }
const { actions } = await getAll(tableId) const rowActions = await getAll(tableId)
const rowAction = rowActions?.actions[rowActionId]
const rowAction = actions[rowActionId]
if (!rowAction) { if (!rowAction) {
throw new HTTPError("Row action not found", 404) throw new HTTPError("Row action not found", 404)
} }

View File

@ -129,7 +129,12 @@ export interface Database {
name: string name: string
exists(): Promise<boolean> exists(): Promise<boolean>
/**
* @deprecated the plan is to get everything using `tryGet` instead, then rename
* `tryGet` to `get`.
*/
get<T extends Document>(id?: string): Promise<T> get<T extends Document>(id?: string): Promise<T>
tryGet<T extends Document>(id?: string): Promise<T | undefined>
exists(docId: string): Promise<boolean> exists(docId: string): Promise<boolean>
getMultiple<T extends Document>( getMultiple<T extends Document>(
ids: string[], ids: string[],