Effect CommunityEC
Effect Community•2y ago•
8 replies
wasd171

Combining TypeScript Effects with Slonik Transactions

I am trying to organize a piece of code that heavily depends on https://github.com/gajus/slonik with effect. So far so good but I cannot find the way to ergonomically consume such APIs as
await connection.transaction(async transaction => {
  await transaction.query(...)
  await transaction.query(...)
})

^ it opens a db transaction when the callback starts executing and closes it when it resolves

I have functions like doStuff = (transaction) => Effect and doOtherStuff = (transaction) => Effect and I cannot figure out how to combine them with Effect.tryPromise(...) 😦 Within the .tryPromise callback I cannot yield* and have to resort to Effect.runPromise for these doStuff / doOtherStuff helpers. But then I also need to manually re-bind all dependencies for these helpers from the parent effect. Alternatively I could convert my helpers to doStuff = (transaction) => Promise but then I will lose all the Effect's benefits. I have a feeling that there should be a better way of doing it but cannot figure it out

Any help would be appreciated 🙂
Was this page helpful?