MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / makeFumaClient

Function makeFumaClient

packages/core/sdk/src/fuma-runtime.ts:293–341  ·  view source on GitHub ↗
(db: FumaDb, options: MakeFumaClientOptions = {})

Source from the content-addressed store, hash-verified

291};
292
293export const makeFumaClient = (db: FumaDb, options: MakeFumaClientOptions = {}): IFumaClient => {
294 const use: IFumaClient["use"] = (label, fn) =>
295 Effect.flatMap(Effect.service(activeFumaDbRef), (active) =>
296 fumaEffect(label, () => fn(makeSafeFumaQuery(active ?? db, options))),
297 ).pipe(Effect.withSpan(`fumadb.${label}`));
298
299 const transaction = <A, E>(effect: Effect.Effect<A, E>): Effect.Effect<A, E | StorageFailure> =>
300 Effect.flatMap(Effect.service(activeFumaDbRef), (active) => {
301 if (active) return effect as Effect.Effect<unknown, unknown>;
302
303 // The outermost transaction owns the post-commit hook queue; hooks
304 // queued anywhere inside (including nested pass-through transactions)
305 // run only after THIS commit, and are discarded on rollback.
306 const commitHooks: Array<Effect.Effect<void>> = [];
307 return Effect.tryPromise({
308 try: () =>
309 db.transaction(async (transactionDb) => {
310 const exit = await Effect.runPromiseExit(
311 effect.pipe(
312 Effect.provideService(activeFumaDbRef, transactionDb),
313 Effect.provideService(pendingCommitHooksRef, commitHooks),
314 ),
315 );
316 if (Exit.isSuccess(exit)) return exit.value;
317
318 const failure = exit.cause.reasons.find(Cause.isFailReason);
319 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: FumaDB transactions roll back when the callback rejects
320 if (failure) throw new TransactionEffectFailure(failure.error);
321 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: FumaDB transactions roll back when the callback rejects
322 throw new TransactionEffectDefect(exit.cause);
323 }),
324 catch: (cause): E | StorageFailure => {
325 if (cause instanceof TransactionEffectFailure) return cause.error as E;
326 if (cause instanceof TransactionEffectDefect) {
327 return fumaFailureFromCause("transaction", cause.cause);
328 }
329 return fumaFailureFromCause("transaction", cause);
330 },
331 }).pipe(
332 Effect.tap(() =>
333 Effect.forEach(commitHooks, (hook) => hook.pipe(Effect.ignoreCause({ log: false })), {
334 discard: true,
335 }),
336 ),
337 );
338 }).pipe(Effect.withSpan("fumadb.transaction")) as Effect.Effect<A, E | StorageFailure>;
339
340 return { use, transaction };
341};
342
343export class FumaClient extends Context.Service<FumaClient, IFumaClient>()("executor/FumaClient") {
344 static layer = (db: FumaDb) => Layer.succeed(this)(makeFumaClient(db));

Callers 4

touchSubjectFunction · 0.90
createExecutorFunction · 0.90
makeAdminFunction · 0.90
FumaClientClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected