Building and using a service layer with Redis

Building a Layer that returns a service? I see things like Layer.effect, my idea is I have two libraries i've wrapped in Effect. I want to build a layer that does some of the service "setup". Then use this service inside of another Layer.

I see Layer.effect but it takes a tag? which i'm not entirely sure on. My psuedo code looks like so

class RedisConnectionError extends Error {
  readonly _tag = 'RedisConnectionError';
}
class ConnectRedis extends Context.Tag('ConnectRedis')<ConnectRedis, typeof RedisStore>() {
  static Live = Layer.succeed(ConnectRedis, RedisStore);
}
class RedisService extends Context.Tag('Redis')<RedisService, typeof redis>() {
  static Live = Effect.gen(function* () {
    const redis = yield* RedisService;
    const redisClient = redis.createClient();
    const RedisStore = yield* ConnectRedis;

    yield* Effect.tryPromise({
      try: () => redisClient.connect(),
      catch: () => new RedisConnectionError(),
    });

    const store = new RedisStore({ client: redisClient, prefix: 'RedisSessionManagement' });

    return store;
  });
}

const Redis = Layer.succeed(RedisService, RedisService.Live);

export { Redis, ConnectRedis };


So my idea is to use Redis.Live in say my Express service to setup middleware.

I'm just not sure what the method to use is. I can do succeed but initially that feels wrong as what if the redis connection fails? Or is layer creation above the implementation of the service? So suceed is correct, and the failure to connect isn't really a Layer issue, just an error in code to handle?
Was this page helpful?