Files
speckle-server/packages/server/modules/shared/command.ts
T
Kristaps Fabians Geikins e3d3c1446b feat: enable domain discoverability on workspace creation (#4235)
* frontend changes implemented

* WIP BE

* backend seems to work

* CR fixes
2025-04-07 12:34:45 +03:00

36 lines
1.0 KiB
TypeScript

import { EmitArg, EventBus, EventBusEmit } from '@/modules/shared/services/eventBus'
import { Knex } from 'knex'
export const commandFactory =
<TOperation extends (...args: Parameters<TOperation>) => ReturnType<TOperation>>({
db,
eventBus,
operationFactory
}: {
db: Knex
eventBus?: EventBus
operationFactory: (arg: { db: Knex; trx: Knex; emit: EventBusEmit }) => TOperation
}) =>
async (...args: Parameters<TOperation>): Promise<Awaited<ReturnType<TOperation>>> => {
const events: EmitArg[] = []
const emit: EventBusEmit = async ({ eventName, payload }) => {
events.push({ eventName, payload })
}
const trx = await db.transaction()
try {
const result = await operationFactory({ db, trx, emit })(...args)
await trx.commit()
if (eventBus) {
for (const event of events) {
await eventBus.emit(event)
}
}
return result as Awaited<ReturnType<TOperation>>
} catch (err) {
trx.rollback()
throw err
}
}