Integrating an `Effect.Effect` into a `Layer` can be a bit tricky since `Layer` is designed to wo...

Hey all,

I created a new service implementation that I want to provide to one of my Lives:

const ServerLive = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
  Layer.provide(HttpApiScalar.layer()),
  Layer.provide(DatabaseLive),
  Layer.provide(BookingAPILive),
  // Add the Bookings.Live somewhere here...
  HttpServer.withLogAddress,
  Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 }))
)

Layer.launch(ServerLive).pipe(
  Logger.withMinimumLogLevel(LogLevel.All),
  NodeRuntime.runMain
)


However, Bookings.Live is of type Effect.Effect since it uses static Live = pipe(...) in the service itself, unlike the other Lives which use Layer.effect(...). How can I properly integrate this into ServerLive?

My service looks like this:

export class Bookings extends Context.Tag('Service/Bookings')<
    Bookings,
    {
        list: Effect.Effect<Booking.Booking[], DatabaseError>
    }
>() {
    static Live = pipe(
        Effect.all([Database, IdGenerator]),
        Effect.map(([db, idGen]) => Bookings.of({
            list: pipe(
                Ops.list,
                Effect.provideService(Database, db),
                Effect.map(Array.map(Booking.decodeStored))
            )
        }))
    )
}
Was this page helpful?