@@ -5,6 +5,7 @@ import Prelude
55import Data.Array (intercalate, mapWithIndex)
66import Data.Maybe (Maybe)
77import Data.Symbol (class IsSymbol, reflectSymbol)
8+import Effect.Aff (Aff)
89import Prim.Boolean (True, False)
910import Prim.Row (class Cons, class Lacks, class Union) as Row
1011import Prim.RowList as RL
@@ -13,7 +14,6 @@ import Prim.TypeError (class Fail, Text)
1314import Type.Proxy (Proxy(..))
1415import Type.RowList (class ListToRow)
1516import Yoga.JSON (class ReadForeign)
16−import Yoga.Om (Om, ask)
1717import Yoga.Postgres as PG
1818import Yoga.Postgres.Schema
1919 ( class ColumnCountRL, class ColumnNamesRL, class ExtractType
@@ -26,12 +26,6 @@ import Yoga.Postgres.Schema
2626 , unsafeDecodeRow, unsafeDecodeRows
2727 )
2828
29−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
30−-- Context type for Om
31−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
32−
33−type PgCtx r = (pgConnection :: PG.Connection | r)
34−
3529-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3630-- Type extraction from tables
3731-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -162,20 +156,21 @@ instance
162156-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
163157
164158findAll
165− :: forall name cols row ctx errs
159+ :: forall name cols row
166160 . IsSymbol name
167161 => TableRow (Table name cols) row
168162 => ReadForeign { | row }
169163 => Proxy (Table name cols)
170− -> Om { | PgCtx ctx } errs (Array { | row })
171−findAll _ = do
172− { pgConnection } <- ask
173− PG.query (PG.SQL sql) [] pgConnection # liftAff <#> \r -> unsafeDecodeRows r.rows
164+ -> PG.Connection
165+ -> Aff (Array { | row })
166+findAll _ conn = do
167+ result <- PG.query (PG.SQL sql) [] conn
168+ pure (unsafeDecodeRows result.rows)
174169 where
175170 sql = "SELECT * FROM " <> reflectSymbol (Proxy :: Proxy name)
176171
177172findById
178− :: forall name cols row pkCol pkType ctx errs
173+ :: forall name cols row pkCol pkType
179174 . IsSymbol name
180175 => IsSymbol pkCol
181176 => TableRow (Table name cols) row
@@ -183,18 +178,19 @@ findById
183178 => FieldToPGValue pkType
184179 => ReadForeign { | row }
185180 => Proxy (Table name cols)
181+ -> PG.Connection
186182 -> pkType
187− -> Om { | PgCtx ctx } errs (Maybe { | row })
188−findById _ pk = do
189− { pgConnection } <- ask
190− PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] pgConnection # liftAff <#> \r -> r <#> unsafeDecodeRow
183+ -> Aff (Maybe { | row })
184+findById _ conn pk = do
185+ result <- PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] conn
186+ pure (result <#> unsafeDecodeRow)
191187 where
192188 sql = "SELECT * FROM " <> reflectSymbol (Proxy :: Proxy name)
193189 <> " WHERE " <> reflectSymbol (Proxy :: Proxy pkCol) <> " = $1"
194190
195191create
196192 :: forall name cols colsRL insertableRL insertable requiredRL required
197− optionalProvided missing userRow userRowRL row ctx errs
193+ optionalProvided missing userRow userRowRL row
198194 . IsSymbol name
199195 => RowToList cols colsRL
200196 => InsertableColumnsRL colsRL insertableRL
@@ -209,11 +205,12 @@ create
209205 => TableRow (Table name cols) row
210206 => ReadForeign { | row }
211207 => Proxy (Table name cols)
208+ -> PG.Connection
212209 -> { | userRow }
213− -> Om { | PgCtx ctx } errs (Maybe { | row })
214−create _ rec = do
215− { pgConnection } <- ask
216− PG.queryOne (PG.SQL sql) values pgConnection # liftAff <#> \r -> r <#> unsafeDecodeRow
210+ -> Aff (Maybe { | row })
211+create _ conn rec = do
212+ result <- PG.queryOne (PG.SQL sql) values conn
213+ pure (result <#> unsafeDecodeRow)
217214 where
218215 colNames = columnNamesRL (Proxy :: Proxy userRowRL)
219216 placeholders = colNames # mapWithIndex \i _ -> "$" <> show (i + 1)
@@ -224,7 +221,7 @@ create _ rec = do
224221 values = recordValuesRL (Proxy :: Proxy userRowRL) rec
225222
226223updateById
227− :: forall name cols row pkCol pkType setRow setRL ctx errs
224+ :: forall name cols row pkCol pkType setRow setRL
228225 . IsSymbol name
229226 => IsSymbol pkCol
230227 => TableRow (Table name cols) row
@@ -237,12 +234,13 @@ updateById
237234 => FieldToPGValue pkType
238235 => ReadForeign { | row }
239236 => Proxy (Table name cols)
237+ -> PG.Connection
240238 -> pkType
241239 -> { | setRow }
242− -> Om { | PgCtx ctx } errs (Maybe { | row })
243−updateById _ pk rec = do
244− { pgConnection } <- ask
245− PG.queryOne (PG.SQL sql) values pgConnection # liftAff <#> \r -> r <#> unsafeDecodeRow
240+ -> Aff (Maybe { | row })
241+updateById _ conn pk rec = do
242+ result <- PG.queryOne (PG.SQL sql) values conn
243+ pure (result <#> unsafeDecodeRow)
246244 where
247245 setClauses = setClauseRL (Proxy :: Proxy setRL) 1
248246 setCount = columnCountRL (Proxy :: Proxy setRL)
@@ -253,17 +251,17 @@ updateById _ pk rec = do
253251 values = recordValuesRL (Proxy :: Proxy setRL) rec <> [ fieldToPGValue pk ]
254252
255253deleteById
256− :: forall name cols pkCol pkType ctx errs
254+ :: forall name cols pkCol pkType
257255 . IsSymbol name
258256 => IsSymbol pkCol
259257 => FindPrimaryKey (Table name cols) pkCol pkType
260258 => FieldToPGValue pkType
261259 => Proxy (Table name cols)
260+ -> PG.Connection
262261 -> pkType
263− -> Om { | PgCtx ctx } errs Int
264−deleteById _ pk = do
265− { pgConnection } <- ask
266− PG.execute (PG.SQL sql) [ fieldToPGValue pk ] pgConnection # liftAff
262+ -> Aff Int
263+deleteById _ conn pk =
264+ PG.execute (PG.SQL sql) [ fieldToPGValue pk ] conn
267265 where
268266 sql = "DELETE FROM " <> reflectSymbol (Proxy :: Proxy name)
269267 <> " WHERE " <> reflectSymbol (Proxy :: Proxy pkCol) <> " = $1"
@@ -274,7 +272,7 @@ deleteById _ pk = do
274272
275273findWith
276274 :: forall childName childCols parentName parentCols
277− childRow parentRow localCol parentCol rest result ctx errs
275+ childRow parentRow localCol parentCol rest result
278276 . IsSymbol childName
279277 => IsSymbol parentName
280278 => IsSymbol localCol
@@ -287,10 +285,11 @@ findWith
287285 => ReadForeign { | result }
288286 => Proxy (Table childName childCols)
289287 -> Proxy (Table parentName parentCols)
290− -> Om { | PgCtx ctx } errs (Array { | result })
291−findWith _ _ = do
292− { pgConnection } <- ask
293− PG.query (PG.SQL sql) [] pgConnection # liftAff <#> \r -> unsafeDecodeRows r.rows
288+ -> PG.Connection
289+ -> Aff (Array { | result })
290+findWith _ _ conn = do
291+ result <- PG.query (PG.SQL sql) [] conn
292+ pure (unsafeDecodeRows result.rows)
294293 where
295294 childTbl = reflectSymbol (Proxy :: Proxy childName)
296295 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
@@ -303,7 +302,7 @@ findWith _ _ = do
303302
304303findByIdWith
305304 :: forall childName childCols parentName parentCols
306− childRow parentRow localCol parentCol pkCol pkType rest result ctx errs
305+ childRow parentRow localCol parentCol pkCol pkType rest result
307306 . IsSymbol childName
308307 => IsSymbol parentName
309308 => IsSymbol localCol
@@ -319,11 +318,12 @@ findByIdWith
319318 => ReadForeign { | result }
320319 => Proxy (Table childName childCols)
321320 -> Proxy (Table parentName parentCols)
321+ -> PG.Connection
322322 -> pkType
323− -> Om { | PgCtx ctx } errs (Maybe { | result })
324−findByIdWith _ _ pk = do
325− { pgConnection } <- ask
326− PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] pgConnection # liftAff <#> \r -> r <#> unsafeDecodeRow
323+ -> Aff (Maybe { | result })
324+findByIdWith _ _ conn pk = do
325+ result <- PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] conn
326+ pure (result <#> unsafeDecodeRow)
327327 where
328328 childTbl = reflectSymbol (Proxy :: Proxy childName)
329329 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
@@ -342,7 +342,7 @@ findByIdWith _ _ pk = do
342342
343343findIncluding
344344 :: forall @label parentName parentCols childName childCols
345− parentRow childRow localCol parentCol result ctx errs
345+ parentRow childRow localCol parentCol result
346346 . IsSymbol label
347347 => IsSymbol parentName
348348 => IsSymbol childName
@@ -356,10 +356,11 @@ findIncluding
356356 => ReadForeign { | result }
357357 => Proxy (Table parentName parentCols)
358358 -> Proxy (Table childName childCols)
359− -> Om { | PgCtx ctx } errs (Array { | result })
360−findIncluding _ _ = do
361− { pgConnection } <- ask
362− PG.query (PG.SQL sql) [] pgConnection # liftAff <#> \r -> unsafeDecodeRows r.rows
359+ -> PG.Connection
360+ -> Aff (Array { | result })
361+findIncluding _ _ conn = do
362+ result <- PG.query (PG.SQL sql) [] conn
363+ pure (unsafeDecodeRows result.rows)
363364 where
364365 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
365366 childTbl = reflectSymbol (Proxy :: Proxy childName)
@@ -373,7 +374,7 @@ findIncluding _ _ = do
373374
374375findByIdIncluding
375376 :: forall @label parentName parentCols childName childCols
376− parentRow childRow localCol parentCol pkCol pkType result ctx errs
377+ parentRow childRow localCol parentCol pkCol pkType result
377378 . IsSymbol label
378379 => IsSymbol parentName
379380 => IsSymbol childName
@@ -390,11 +391,12 @@ findByIdIncluding
390391 => ReadForeign { | result }
391392 => Proxy (Table parentName parentCols)
392393 -> Proxy (Table childName childCols)
394+ -> PG.Connection
393395 -> pkType
394− -> Om { | PgCtx ctx } errs (Maybe { | result })
395−findByIdIncluding _ _ pk = do
396− { pgConnection } <- ask
397− PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] pgConnection # liftAff <#> \r -> r <#> unsafeDecodeRow
396+ -> Aff (Maybe { | result })
397+findByIdIncluding _ _ conn pk = do
398+ result <- PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] conn
399+ pure (result <#> unsafeDecodeRow)
398400 where
399401 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
400402 childTbl = reflectSymbol (Proxy :: Proxy childName)