refactor: assemble main transaction session in MainTx

This change makes the API surface between MainTx and App smaller.

Currently, App reconstructs a database transaction by unpacking the isolation
level, transaction mode, DbHandler, and transaction runner returned by MainTx.
That exposes MainTx internals at the call site even though MainTx already owns
query setup, execution, decoding, and rollback behavior.

The goal is to keep transaction assembly in MainTx while App remains responsible
for pool execution, database error mapping, and response orchestration. DbTx now
carries the assembled SQL session, and App passes that session directly to the
connection pool.
This commit is contained in:
Michał Kłeczek
2026-05-31 15:52:56 -05:00
committed by Steve Chavez
parent 56ad5a9324
commit 0df56f9ea8
2 changed files with 4 additions and 9 deletions
+2 -2
View File
@@ -196,8 +196,8 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache jwtTime authResul
(txTime, txResult) <- withTiming conf $ do
case tx of
MainTx.NoDbTx r -> pure r
MainTx.DbTx{..} -> do
dbRes <- lift $ AppState.usePool appState (dqTransaction dqIsoLevel dqTxMode $ runExceptT dqDbHandler)
MainTx.DbTx dbSession -> do
dbRes <- lift $ AppState.usePool appState dbSession
let eitherResp = join $ mapLeft (Error.PgErr . Error.PgError (Just authRole /= configDbAnonRole)) dbRes
-- TODO: we use obsQuery twice, one here and one below because in case of an error with the usePool above, the request will finish here and return an error message.
+2 -7
View File
@@ -59,12 +59,7 @@ import Protolude hiding (Handler)
type DbHandler = ExceptT Error SQL.Transaction
data MainTx
= DbTx {
dqIsoLevel :: SQL.IsolationLevel
, dqTxMode :: SQL.Mode
, dqDbHandler :: DbHandler DbResult
, dqTransaction :: SQL.IsolationLevel -> SQL.Mode -> SQL.Transaction (Either Error DbResult) -> SQL.Session (Either Error DbResult)
}
= DbTx (SQL.Session (Either Error DbResult))
| NoDbTx DbResult
data DbResult
@@ -96,7 +91,7 @@ data ResultSet
mainTx :: MainQuery -> AppConfig -> AuthResult -> ApiRequest -> ActionPlan -> SchemaCache -> MainTx
mainTx _ _ _ _ (NoDb x) _ = NoDbTx $ NoDbResult x
mainTx genQ@MainQuery{..} conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache =
DbTx isoLvl txMode dbHandler SQL.transactionNoRetry
DbTx $ SQL.transactionNoRetry isoLvl txMode $ runExceptT dbHandler
where
isoLvl = planIsoLvl conf authRole plan
txMode = planTxMode plan