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

Function makeFumaClient

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

Source from the content-addressed store, hash-verified

358};
359
360export const makeFumaClient = (db: FumaDb, options: MakeFumaClientOptions = {}): IFumaClient => {
361 const rebind = (handle: FumaDb): FumaDb =>
362 options.context === undefined ? handle : withQueryContext(handle, options.context);
363 const use: IFumaClient["use"] = (label, fn) =>
364 Effect.flatMap(Effect.service(activeFumaDbRef), (active) =>
365 fumaEffect(label, () => fn(makeSafeFumaQuery(rebind(active ?? db), options))),
366 ).pipe(Effect.withSpan(`fumadb.${label}`));
367
368 const transaction = <A, E>(effect: Effect.Effect<A, E>): Effect.Effect<A, E | StorageFailure> =>
369 Effect.flatMap(Effect.service(activeFumaDbRef), (active) => {
370 if (active) return effect as Effect.Effect<unknown, unknown>;
371
372 // The outermost transaction owns the post-commit hook queue; hooks
373 // queued anywhere inside (including nested pass-through transactions)
374 // run only after THIS commit, and are discarded on rollback.
375 const commitHooks: PendingCommitHook[] = [];
376 return Effect.contextWith((context) =>
377 Effect.tryPromise({
378 try: () =>
379 db.transaction(async (transactionDb) => {
380 const exit = await Effect.runPromiseExitWith(context)(
381 effect.pipe(
382 Effect.provideService(activeFumaDbRef, transactionDb),
383 Effect.provideService(pendingCommitHooksRef, commitHooks),
384 ),
385 );
386 if (Exit.isSuccess(exit)) return exit.value;
387
388 const failure = exit.cause.reasons.find(Cause.isFailReason);
389 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: FumaDB transactions roll back when the callback rejects
390 if (failure) throw new TransactionEffectFailure(failure.error);
391 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: FumaDB transactions roll back when the callback rejects
392 throw new TransactionEffectDefect(exit.cause);
393 }),
394 catch: (cause): E | StorageFailure => {
395 if (cause instanceof TransactionEffectFailure) return cause.error as E;
396 if (cause instanceof TransactionEffectDefect) {
397 return fumaFailureFromCause("transaction", cause.cause);
398 }
399 return fumaFailureFromCause("transaction", cause);
400 },
401 }).pipe(Effect.tap(() => runCommitHooks(commitHooks))),
402 );
403 }).pipe(Effect.withSpan("fumadb.transaction")) as Effect.Effect<A, E | StorageFailure>;
404
405 return { use, transaction };
406};
407
408export class FumaClient extends Context.Service<FumaClient, IFumaClient>()("executor/FumaClient") {
409 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