Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho committed Oct 10, 2024
1 parent a00a64b commit 4c4429b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/backend-core/src/db/couch/DatabaseImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
ids: string[],
opts?: { allowMissing?: boolean; excludeDocs?: boolean }
Expand Down
7 changes: 7 additions & 0 deletions packages/backend-core/src/db/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
ids: string[],
opts?: { allowMissing?: boolean | undefined } | undefined
Expand Down
5 changes: 3 additions & 2 deletions packages/server/src/api/controllers/rowAction/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export async function find(ctx: Ctx<void, RowActionsResponse>) {
const table = await getTable(ctx)
const tableId = table._id!

if (!(await sdk.rowActions.docExists(tableId))) {
const rowActions = await sdk.rowActions.getAll(tableId)
if (!rowActions) {
ctx.body = {
actions: {},
}
return
}

const { actions } = await sdk.rowActions.getAll(tableId)
const { actions } = rowActions
const result: RowActionsResponse = {
actions: Object.entries(actions).reduce<Record<string, RowActionResponse>>(
(acc, [key, action]) => ({
Expand Down
8 changes: 6 additions & 2 deletions packages/server/src/sdk/app/automations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export async function getBuilderData(
return tableNameCache[tableId]
}

const rowActionNameCache: Record<string, TableRowActions> = {}
const rowActionNameCache: Record<string, TableRowActions | undefined> = {}
async function getRowActionName(tableId: string, rowActionId: string) {
if (!rowActionNameCache[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> = {}
Expand All @@ -51,6 +51,10 @@ export async function getBuilderData(
const tableName = await getTableName(tableId)
const rowActionName = await getRowActionName(tableId, rowActionId)

if (!rowActionName) {
throw new Error(`Row action not found: ${rowActionId}`)
}

result[automation._id!] = {
displayName: `${tableName}: ${automation.name}`,
triggerInfo: {
Expand Down
11 changes: 7 additions & 4 deletions packages/server/src/sdk/app/rowActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ export async function get(tableId: string, rowActionId: string) {
export async function getAll(tableId: string) {
const db = context.getAppDB()
const rowActionsId = generateRowActionsID(tableId)
return await db.get<TableRowActions>(rowActionsId)
return await db.tryGet<TableRowActions>(rowActionsId)
}

export async function deleteAll(tableId: string) {
const db = context.getAppDB()

const doc = await getAll(tableId)
if (!doc) {
return
}

const automationIds = Object.values(doc.actions).map(a => a.automationId)
const automations = await db.getMultiple<Automation>(automationIds)

Expand Down Expand Up @@ -238,9 +242,8 @@ export async function run(tableId: any, rowActionId: any, rowId: string) {
throw new HTTPError("Table not found", 404)
}

const { actions } = await getAll(tableId)

const rowAction = actions[rowActionId]
const rowActions = await getAll(tableId)
const rowAction = rowActions?.actions[rowActionId]
if (!rowAction) {
throw new HTTPError("Row action not found", 404)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/sdk/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ export interface Database {
name: string

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>
tryGet<T extends Document>(id?: string): Promise<T | undefined>
exists(docId: string): Promise<boolean>
getMultiple<T extends Document>(
ids: string[],
Expand Down

0 comments on commit 4c4429b

Please sign in to comment.