Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
Created December 30, 2016 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsonsmatt/a19b6dfb73085bc1da7ec7345c10feb1 to your computer and use it in GitHub Desktop.
Save parsonsmatt/a19b6dfb73085bc1da7ec7345c10feb1 to your computer and use it in GitHub Desktop.
type Join a b = Map (Key a) (Entity a, Collection b)
type Collection a = Map (Key a) (Entity a)
innerJoin
:: ( PersistEntity val1
, PersistEntity val2
, PersistField typ
, PersistEntityBackend val1 ~ SqlBackend
, PersistEntityBackend val2 ~ SqlBackend
, Monad m, MonadResource m
)
=> EntityField val1 typ
-> EntityField val2 typ
-> SqlPersistT m (Join val1 val2)
innerJoin x y = selectSource q $$ foldlC k mempty
where
q = from $ \(m `InnerJoin` a) -> do
on (m ^. x ==. a ^. y)
pure (m, a)
k acc (m, a) =
Map.insertWith h (entityKey m) (m, Map.singleton (entityKey a) a) acc
h (m, a) (_, b) = (m, Map.union a b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment