Proper typing

This commit is contained in:
Adria Navarro 2024-10-24 13:34:47 +02:00
parent 47e16dd844
commit 342d70b326
7 changed files with 68 additions and 46 deletions

View File

@ -7,11 +7,11 @@ import * as internal from "./internal"
import * as external from "./external"
import { isExternalTableID } from "../../../integrations/utils"
import {
AutomationEventType,
Ctx,
DeleteRow,
DeleteRowRequest,
DeleteRows,
EventType,
ExportRowsRequest,
ExportRowsResponse,
FieldType,
@ -67,7 +67,7 @@ export async function patch(
ctx.status = 200
ctx.eventEmitter?.emitRow({
eventName: AutomationEventType.ROW_UPDATE,
eventName: EventType.ROW_UPDATE,
appId,
row,
table,
@ -104,14 +104,14 @@ export const save = async (ctx: UserCtx<Row, Row>) => {
sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
)
ctx.status = 200
ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:save`,
appId,
row,
table,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_SAVE,
appId,
row,
table,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.message = `${table.name} saved successfully`
// prefer squashed for response
ctx.body = row || squashed
@ -183,13 +183,12 @@ async function deleteRows(ctx: UserCtx<DeleteRowRequest>) {
}
for (let row of rows) {
ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:delete`,
appId,
row,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_DELETE,
appId,
row,
user: sdk.users.getUserContextBindings(ctx.user),
})
gridSocket?.emitRowDeletion(ctx, row)
}
return rows
@ -204,13 +203,12 @@ async function deleteRow(ctx: UserCtx<DeleteRowRequest>) {
await quotas.removeRow()
}
ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:delete`,
appId,
row: resp.row,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_DELETE,
appId,
row: resp.row,
user: sdk.users.getUserContextBindings(ctx.user),
})
gridSocket?.emitRowDeletion(ctx, resp.row)
return resp

View File

@ -17,6 +17,7 @@ import {
import { context, features } from "@budibase/backend-core"
import {
ContextUser,
EventType,
FeatureFlag,
FieldType,
LinkDocumentValue,
@ -44,15 +45,7 @@ const INVALID_DISPLAY_COLUMN_TYPE = [
* This functionality makes sure that when rows with links are created, updated or deleted they are processed
* correctly - making sure that no stale links are left around and that all links have been made successfully.
*/
export const EventType = {
ROW_SAVE: "row:save",
ROW_UPDATE: "row:update",
ROW_DELETE: "row:delete",
TABLE_SAVE: "table:save",
TABLE_UPDATED: "table:updated",
TABLE_DELETE: "table:delete",
}
export { EventType } from "@budibase/types"
function clearRelationshipFields(schema: TableSchema, rows: Row[]) {
for (let [key, field] of Object.entries(schema)) {

View File

@ -1,6 +1,12 @@
import { EventEmitter } from "events"
import { rowEmission, tableEmission } from "./utils"
import { Table, Row, User } from "@budibase/types"
import {
Table,
Row,
UserBindings,
EventType,
ContextEmitter,
} from "@budibase/types"
/**
* keeping event emitter in one central location as it might be used for things other than
@ -12,7 +18,7 @@ import { Table, Row, User } from "@budibase/types"
* Extending the standard emitter to some syntactic sugar and standardisation to the emitted event.
* This is specifically quite important for template strings used in automations.
*/
class BudibaseEmitter extends EventEmitter {
class BudibaseEmitter extends EventEmitter implements ContextEmitter {
emitRow({
eventName,
appId,
@ -21,17 +27,17 @@ class BudibaseEmitter extends EventEmitter {
oldRow,
user,
}: {
eventName: string
eventName: EventType.ROW_SAVE | EventType.ROW_DELETE | EventType.ROW_UPDATE
appId: string
row: Row
table?: Table
oldRow?: Row
user: User
user: UserBindings
}) {
rowEmission({ emitter: this, eventName, appId, row, table, oldRow, user })
}
emitTable(eventName: string, appId: string, table?: Table) {
emitTable(eventName: EventType, appId: string, table?: Table) {
tableEmission({ emitter: this, eventName, appId, table })
}

View File

@ -1,4 +1,4 @@
import { Table, Row, User } from "@budibase/types"
import { Table, Row, UserBindings } from "@budibase/types"
import BudibaseEmitter from "./BudibaseEmitter"
type BBEventOpts = {
@ -9,7 +9,7 @@ type BBEventOpts = {
row?: Row
oldRow?: Row
metadata?: any
user?: User
user?: UserBindings
}
interface BBEventTable extends Table {
@ -25,7 +25,7 @@ type BBEvent = {
id?: string
revision?: string
metadata?: any
user?: User
user?: UserBindings
}
export function rowEmission({

View File

@ -0,0 +1,8 @@
export const enum EventType {
ROW_SAVE = "row:save",
ROW_UPDATE = "row:update",
ROW_DELETE = "row:delete",
TABLE_SAVE = "table:save",
TABLE_UPDATED = "table:updated",
TABLE_DELETE = "table:delete",
}

View File

@ -1 +1,2 @@
export * from "./installation"
export * from "./events"

View File

@ -7,10 +7,11 @@ import {
ConfigType,
Row,
Table,
AutomationEventType,
UserBindings,
} from "../documents"
import { FeatureFlag, License } from "../sdk"
import { Files } from "formidable"
import { EventType } from "../core"
export interface ContextUser extends Omit<User, "roles"> {
globalId?: string
@ -61,10 +62,25 @@ export interface BBContext extends Ctx {
}
export interface ContextEmitter {
emitRow: (params: {
eventName: AutomationEventType
emitRow(values: {
eventName: EventType.ROW_SAVE
appId: string
row: Row
table?: Table
}) => Promise<void>
table: Table
user: UserBindings
}): void
emitRow(values: {
eventName: EventType.ROW_UPDATE
appId: string
row: Row
table: Table
oldRow: Row
user: UserBindings
}): void
emitRow(values: {
eventName: EventType.ROW_DELETE
appId: string
row: Row
user: UserBindings
}): void
}