Effect CommunityEC
Effect Community3y ago
35 replies
iamK

Transactional Wrapper for EntityManager Effects

I have this wrapper which takes any effect with <EntityManager, E, A> and runs it in a transaction:

export function transaction<E, A>(self: Effect.Effect<EntityManager, E, A>) {
  const acquire = pipe(
    EntityManagerTag,
    Effect.flatMap((manager) =>
      Effect.tryPromise(async () =>
        manager.transaction(async (transactionalManager) =>
          Effect.runPromise(pipe(self, Effect.provideService(EntityManagerTag, transactionalManager)))
        )
      )
    )
  );

  return Effect.acquireUseRelease(
    Scope.make(),
    () => acquire,
    (scope) => {
      // TODO: check how to handle errors here
      return Scope.close(scope, Exit.unit());
    }
  );
}


it works fine. Unless the self changes to <EntityManager | AnotherDependency, E, A>

any ideas ?
Was this page helpful?