@@ -5,7 +5,6 @@ import Prelude
55import Data.Array (intercalate, mapWithIndex)
66import Data.Maybe (Maybe)
77import Data.Symbol (class IsSymbol, reflectSymbol)
8−import Effect.Aff (Aff)
98import Prim.Boolean (True, False)
109import Prim.Row (class Cons, class Lacks, class Union) as Row
1110import Prim.RowList as RL
@@ -14,6 +13,7 @@ import Prim.TypeError (class Fail, Text)
1413import Type.Proxy (Proxy(..))
1514import Type.RowList (class ListToRow)
1615import 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,6 +26,12 @@ 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+
2935-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3036-- Type extraction from tables
3137-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -156,21 +162,20 @@ instance
156162-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
157163
158164findAll
159− :: forall name cols row
165+ :: forall name cols row ctx errs
160166 . IsSymbol name
161167 => TableRow (Table name cols) row
162168 => ReadForeign { | row }
163169 => Proxy (Table name cols)
164− -> PG.Connection
165− -> Aff (Array { | row })
166−findAll _ conn = do
167− result <- PG.query (PG.SQL sql) [] conn
168− pure (unsafeDecodeRows result.rows)
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
169174 where
170175 sql = "SELECT * FROM " <> reflectSymbol (Proxy :: Proxy name)
171176
172177findById
173− :: forall name cols row pkCol pkType
178+ :: forall name cols row pkCol pkType ctx errs
174179 . IsSymbol name
175180 => IsSymbol pkCol
176181 => TableRow (Table name cols) row
@@ -178,19 +183,18 @@ findById
178183 => FieldToPGValue pkType
179184 => ReadForeign { | row }
180185 => Proxy (Table name cols)
181− -> PG.Connection
182186 -> pkType
183− -> Aff (Maybe { | row })
184−findById _ conn pk = do
185− result <- PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] conn
186− pure (result <#> unsafeDecodeRow)
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
187191 where
188192 sql = "SELECT * FROM " <> reflectSymbol (Proxy :: Proxy name)
189193 <> " WHERE " <> reflectSymbol (Proxy :: Proxy pkCol) <> " = $1"
190194
191195create
192196 :: forall name cols colsRL insertableRL insertable requiredRL required
193− optionalProvided missing userRow userRowRL row
197+ optionalProvided missing userRow userRowRL row ctx errs
194198 . IsSymbol name
195199 => RowToList cols colsRL
196200 => InsertableColumnsRL colsRL insertableRL
@@ -205,12 +209,11 @@ create
205209 => TableRow (Table name cols) row
206210 => ReadForeign { | row }
207211 => Proxy (Table name cols)
208− -> PG.Connection
209212 -> { | userRow }
210− -> Aff (Maybe { | row })
211−create _ conn rec = do
212− result <- PG.queryOne (PG.SQL sql) values conn
213− pure (result <#> unsafeDecodeRow)
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
214217 where
215218 colNames = columnNamesRL (Proxy :: Proxy userRowRL)
216219 placeholders = colNames # mapWithIndex \i _ -> "$" <> show (i + 1)
@@ -221,7 +224,7 @@ create _ conn rec = do
221224 values = recordValuesRL (Proxy :: Proxy userRowRL) rec
222225
223226updateById
224− :: forall name cols row pkCol pkType setRow setRL
227+ :: forall name cols row pkCol pkType setRow setRL ctx errs
225228 . IsSymbol name
226229 => IsSymbol pkCol
227230 => TableRow (Table name cols) row
@@ -234,13 +237,12 @@ updateById
234237 => FieldToPGValue pkType
235238 => ReadForeign { | row }
236239 => Proxy (Table name cols)
237− -> PG.Connection
238240 -> pkType
239241 -> { | setRow }
240− -> Aff (Maybe { | row })
241−updateById _ conn pk rec = do
242− result <- PG.queryOne (PG.SQL sql) values conn
243− pure (result <#> unsafeDecodeRow)
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
244246 where
245247 setClauses = setClauseRL (Proxy :: Proxy setRL) 1
246248 setCount = columnCountRL (Proxy :: Proxy setRL)
@@ -251,17 +253,17 @@ updateById _ conn pk rec = do
251253 values = recordValuesRL (Proxy :: Proxy setRL) rec <> [ fieldToPGValue pk ]
252254
253255deleteById
254− :: forall name cols pkCol pkType
256+ :: forall name cols pkCol pkType ctx errs
255257 . IsSymbol name
256258 => IsSymbol pkCol
257259 => FindPrimaryKey (Table name cols) pkCol pkType
258260 => FieldToPGValue pkType
259261 => Proxy (Table name cols)
260− -> PG.Connection
261262 -> pkType
262− -> Aff Int
263−deleteById _ conn pk =
264− PG.execute (PG.SQL sql) [ fieldToPGValue pk ] conn
263+ -> Om { | PgCtx ctx } errs Int
264+deleteById _ pk = do
265+ { pgConnection } <- ask
266+ PG.execute (PG.SQL sql) [ fieldToPGValue pk ] pgConnection # liftAff
265267 where
266268 sql = "DELETE FROM " <> reflectSymbol (Proxy :: Proxy name)
267269 <> " WHERE " <> reflectSymbol (Proxy :: Proxy pkCol) <> " = $1"
@@ -272,7 +274,7 @@ deleteById _ conn pk =
272274
273275findWith
274276 :: forall childName childCols parentName parentCols
275− childRow parentRow localCol parentCol rest result
277+ childRow parentRow localCol parentCol rest result ctx errs
276278 . IsSymbol childName
277279 => IsSymbol parentName
278280 => IsSymbol localCol
@@ -285,11 +287,10 @@ findWith
285287 => ReadForeign { | result }
286288 => Proxy (Table childName childCols)
287289 -> Proxy (Table parentName parentCols)
288− -> PG.Connection
289− -> Aff (Array { | result })
290−findWith _ _ conn = do
291− result <- PG.query (PG.SQL sql) [] conn
292− pure (unsafeDecodeRows result.rows)
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
293294 where
294295 childTbl = reflectSymbol (Proxy :: Proxy childName)
295296 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
@@ -302,7 +303,7 @@ findWith _ _ conn = do
302303
303304findByIdWith
304305 :: forall childName childCols parentName parentCols
305− childRow parentRow localCol parentCol pkCol pkType rest result
306+ childRow parentRow localCol parentCol pkCol pkType rest result ctx errs
306307 . IsSymbol childName
307308 => IsSymbol parentName
308309 => IsSymbol localCol
@@ -318,12 +319,11 @@ findByIdWith
318319 => ReadForeign { | result }
319320 => Proxy (Table childName childCols)
320321 -> Proxy (Table parentName parentCols)
321− -> PG.Connection
322322 -> pkType
323− -> Aff (Maybe { | result })
324−findByIdWith _ _ conn pk = do
325− result <- PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] conn
326− pure (result <#> unsafeDecodeRow)
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
327327 where
328328 childTbl = reflectSymbol (Proxy :: Proxy childName)
329329 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
@@ -342,7 +342,7 @@ findByIdWith _ _ conn pk = do
342342
343343findIncluding
344344 :: forall @label parentName parentCols childName childCols
345− parentRow childRow localCol parentCol result
345+ parentRow childRow localCol parentCol result ctx errs
346346 . IsSymbol label
347347 => IsSymbol parentName
348348 => IsSymbol childName
@@ -356,11 +356,10 @@ findIncluding
356356 => ReadForeign { | result }
357357 => Proxy (Table parentName parentCols)
358358 -> Proxy (Table childName childCols)
359− -> PG.Connection
360− -> Aff (Array { | result })
361−findIncluding _ _ conn = do
362− result <- PG.query (PG.SQL sql) [] conn
363− pure (unsafeDecodeRows result.rows)
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
364363 where
365364 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
366365 childTbl = reflectSymbol (Proxy :: Proxy childName)
@@ -374,7 +373,7 @@ findIncluding _ _ conn = do
374373
375374findByIdIncluding
376375 :: forall @label parentName parentCols childName childCols
377− parentRow childRow localCol parentCol pkCol pkType result
376+ parentRow childRow localCol parentCol pkCol pkType result ctx errs
378377 . IsSymbol label
379378 => IsSymbol parentName
380379 => IsSymbol childName
@@ -391,12 +390,11 @@ findByIdIncluding
391390 => ReadForeign { | result }
392391 => Proxy (Table parentName parentCols)
393392 -> Proxy (Table childName childCols)
394− -> PG.Connection
395393 -> pkType
396− -> Aff (Maybe { | result })
397−findByIdIncluding _ _ conn pk = do
398− result <- PG.queryOne (PG.SQL sql) [ fieldToPGValue pk ] conn
399− pure (result <#> unsafeDecodeRow)
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
400398 where
401399 parentTbl = reflectSymbol (Proxy :: Proxy parentName)
402400 childTbl = reflectSymbol (Proxy :: Proxy childName)