Troubleshooting Database Service Creation in TypeScript

I'm trying to create a database service and having trouble finding the right pattern. What am I doing wrong?

export class DbError extends Schema.TaggedError<DbError>()('DbError', {
    cause: Schema.Unknown,
}) {}

const makeDatabase = Effect.gen(function*() {
  const acquireDatabase = () => Effect.sync(() => createClient({
    })).pipe(Effect.tap(Effect.log('DB client acquired')))

  const baseClient = yield* Effect.acquireRelease(
    acquireDatabase(),
    (client) => Effect.promise(() => client.close()).pipe(
      Effect.zipLeft(Effect.log('DB client closed'))
    ),
  )
  return {
    client: Effect.succeed(baseClient),
    executeDBQuery: (query: string) => (theClient: Client) => Effect.tryPromise<any, DbError>({
      try: () => theClient.query(query),
      catch: (cause) => {
        console.error('DbError', cause)
        return new DbError({ cause: cause })
      },
    }).pipe(Effect.withSpan('Db.query', { attributes: { sql: query } })),
  }
})

export class Database extends Effect.Tag('@adapter/db')<
  Database,
  Effect.Effect.Success<typeof makeDatabase>
>() {
  // static Live = makeDatabase.pipe(Layer.scoped(this))
    static Live = Layer.scoped(this, makeDatabase)
}

const program = Database.client.pipe(
  Effect.flatMap(Database.executeDBQuery('SELECT 1')), 
)


The problem is that the flatMap in my program is expecting

(c: Client) => Effect<any, DbError, never>


and instead, the signature of Database.executeDBQuery('SELECT 1') is

Effect<c: Client) => Effect<any, DbError, never>, never, Database>


What am I doing wrong?
Was this page helpful?