purescript-selda icon indicating copy to clipboard operation
purescript-selda copied to clipboard

Problematic writing generic queries with open rows

Open Kamirus opened this issue 4 years ago • 0 comments

Writing such queries often requires providing type signatures including type class constraints.

The query below uses a combination of selectFrom and query1 that require complicated constraints. The table row is open thus these constraints need to persist - it is advised to write the 'top-level constraints' (FromTable in selectFrom and GenericQuery in query1) attached to those functions (in their signatures) and don't let the compiler unfold them.

To type-check the function below we need to provide the type signature ourself - or satisfy ourselves with concrete and closed table-row t and let the compiler solve these constraints immediately.

Please note that the query (returned by selectFrom) is defined separately in the where clause. Somehow with the proper type signature, the function getUserByEmail does not type-check if we inline the query q.

This should be mentioned in the guide.

getUserByEmail
  ∷ ∀ t m i o
  . MonadSeldaPG m
  ⇒ FromTable Unit t ( email ∷ Col Unit String | i )
  ⇒ GenericQuery BackendPGClass m 
      ( email ∷ Col Unit String | i )
      ( email ∷ String | o )
  ⇒ Selda.Table t → String → m (Maybe { email ∷ String | o })
getUserByEmail table email = query1 q
  where
    q = selectFrom table \r → do
      restrict $ r.email .== lit email
      pure r

Kamirus avatar May 29 '20 09:05 Kamirus