Skip to content

Commit 1ebc561

Browse files
committed
Cleanup selda runner a bit
1 parent 67e07c0 commit 1ebc561

File tree

1 file changed

+74
-70
lines changed

1 file changed

+74
-70
lines changed

src/WebRow/Selda.purs

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Effect.Aff (Aff)
1313
import Run (Run)
1414
import Run as Run
1515
import Run.Except (EXCEPT, throwAt)
16-
import Run.State (STATE, getAt, modifyAt, putAt)
16+
import Run.State (STATE, evalStateAt, getAt, modifyAt, putAt)
1717
import Selda (Col, FullQuery, Table)
1818
import Selda.PG.Class (class InsertRecordIntoTableReturning, BackendPGClass)
1919
import Selda.PG.Class (deleteFrom, insert, insert1, insert1_, query, query1, update) as PG.Class
@@ -51,11 +51,6 @@ _selda = SProxy ∷ SProxy "selda"
5151
type PgError r
5252
= ( pgError EXCEPT PGError | r )
5353

54-
type PgConnection r
55-
= ( pg STATE { conn Maybe PG.ConnectResult, inTransaction Boolean, pool PG.Pool } | r )
56-
57-
_pg = SProxy SProxy "pg"
58-
5954
_pgError = SProxy SProxy "pgError"
6055

6156
query
@@ -128,71 +123,80 @@ pgQuery ∷
128123
Run (Selda + eff) (Array o)
129124
pgQuery q i = Run.lift _selda (PgQueryF (\conn → PG.query conn q i))
130125

126+
type Pg r
127+
= ( pg STATE { conn Maybe PG.ConnectResult, inTransaction Boolean } | r )
128+
129+
_pg = SProxy SProxy "pg"
130+
131131
run
132132
eff.
133-
Run (AffRow + EffRow + PgConnection + PgError + Selda + eff)
134-
~> Run (AffRow + EffRow + PgConnection + PgError + eff)
135-
run action = do
136-
a ← Run.run (Run.on _selda handleSelda Run.send) action
137-
getAt _pg >>= _.conn
138-
>>> case _ of
139-
Just { connection, done } → do
140-
inTransaction
141-
>>= flip when do
142-
execute "ROLLBACK TRANSACTION"
143-
modifyAt _pg _ { inTransaction = false }
144-
Run.liftEffect $ done
145-
modifyAt _pg _ { conn = Nothing }
146-
Nothing → pure unit
147-
pure a
148-
where
149-
inTransaction = getAt _pg <#> _.inTransaction
150-
151-
conn = do
152-
pg ← getAt _pg
153-
case pg.conn of
154-
Nothing
155-
(Run.liftAff (PG.connect pg.pool))
156-
>>= case _ of
157-
Right result → do
158-
putAt _pg (pg { conn = Just result })
159-
pure result.connection
160-
Left err → throwAt _pgError err
161-
Just { connection } → pure connection
162-
163-
execute q = do
164-
c ← conn
165-
(Run.liftAff $ PG.execute c (PG.Query q) Row0)
166-
>>= case _ of
167-
Just err → throwAt _pgError err
168-
Nothing → pure unit
169-
170-
handleSelda a. SeldaF a Run (AffRow + EffRow + PgConnection + PgError + eff) a
171-
handleSelda = case _ of
172-
SeldaF q → do
173-
c ← conn
174-
Run.liftAff (runReaderT (runExceptT q) c)
175-
>>= case _ of
176-
Right next → pure next
177-
Left err → throwAt _pgError err
178-
PgExecuteF q next → do
179-
execute q
180-
pure next
181-
PgQueryF q → do
133+
PG.Pool
134+
Run (AffRow + EffRow + Pg + PgError + Selda + eff)
135+
~> Run (AffRow + EffRow + PgError + eff)
136+
run pool =
137+
let
138+
initial = { conn: Nothing, inTransaction: false }
139+
140+
inTransaction = getAt _pg <#> _.inTransaction
141+
142+
conn = do
143+
pg ← getAt _pg
144+
case pg.conn of
145+
Nothing
146+
(Run.liftAff (PG.connect pool))
147+
>>= case _ of
148+
Right result → do
149+
putAt _pg (pg { conn = Just result })
150+
pure result.connection
151+
Left err → throwAt _pgError err
152+
Just { connection } → pure connection
153+
154+
execute q = do
182155
c ← conn
183-
(Run.liftAff (q c))
156+
(Run.liftAff $ PG.execute c (PG.Query q) Row0)
184157
>>= case _ of
185-
Left err → throwAt _pgError err
186-
Right next → pure next
187-
PgOpenTransactionF next → do
188-
inTransaction >>= not
189-
>>> flip when do
190-
execute "BEGIN TRANSACTION"
191-
modifyAt _pg _ { inTransaction = true }
192-
pure next
193-
PgCloseTransactionF next → do
194-
inTransaction
195-
>>= flip when do
196-
execute "END TRANSACTION"
197-
modifyAt _pg _ { inTransaction = false }
198-
pure next
158+
Just err → throwAt _pgError err
159+
Nothing → pure unit
160+
161+
handleSelda a. SeldaF a Run (AffRow + EffRow + Pg + PgError + eff) a
162+
handleSelda = case _ of
163+
SeldaF q → do
164+
c ← conn
165+
Run.liftAff (runReaderT (runExceptT q) c)
166+
>>= case _ of
167+
Right next → pure next
168+
Left err → throwAt _pgError err
169+
PgExecuteF q next → do
170+
execute q
171+
pure next
172+
PgQueryF q → do
173+
c ← conn
174+
(Run.liftAff (q c))
175+
>>= case _ of
176+
Left err → throwAt _pgError err
177+
Right next → pure next
178+
PgOpenTransactionF next → do
179+
inTransaction >>= not
180+
>>> flip when do
181+
execute "BEGIN TRANSACTION"
182+
modifyAt _pg _ { inTransaction = true }
183+
pure next
184+
PgCloseTransactionF next → do
185+
inTransaction
186+
>>= flip when do
187+
execute "END TRANSACTION"
188+
modifyAt _pg _ { inTransaction = false }
189+
pure next
190+
in
191+
\action →
192+
evalStateAt _pg initial do
193+
a ← Run.run (Run.on _selda handleSelda Run.send) action
194+
getAt _pg >>= _.conn
195+
>>> case _ of
196+
Just { connection, done } → do
197+
inTransaction
198+
>>= flip when do
199+
execute "ROLLBACK TRANSACTION"
200+
Run.liftEffect $ done
201+
Nothing → pure unit
202+
pure a

0 commit comments

Comments
 (0)