Merge pull request #157 from tippenein/bmo/associated-join
add associateJoin function
This commit is contained in:
commit
56420e1c34
@ -1,3 +1,9 @@
|
||||
3.1.2
|
||||
========
|
||||
|
||||
- @tippenein
|
||||
- [#149](https://github.com/bitemyapp/esqueleto/pull/157): Added `associateJoin` query helpers.
|
||||
|
||||
3.1.1
|
||||
========
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
cabal-version: 1.12
|
||||
|
||||
name: esqueleto
|
||||
version: 3.1.1
|
||||
version: 3.1.2
|
||||
synopsis: Type-safe EDSL for SQL queries on persistent backends.
|
||||
description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime.
|
||||
.
|
||||
@ -47,6 +47,7 @@ library
|
||||
, aeson >=1.0
|
||||
, blaze-html
|
||||
, bytestring
|
||||
, containers
|
||||
, conduit >=1.3
|
||||
, monad-logger
|
||||
, persistent >=2.10.0 && <2.11
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs #-}
|
||||
{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs, RankNTypes #-}
|
||||
-- | The @esqueleto@ EDSL (embedded domain specific language).
|
||||
-- This module replaces @Database.Persist@, so instead of
|
||||
-- importing that module you should just import this one:
|
||||
@ -100,6 +100,7 @@ module Database.Esqueleto
|
||||
-- * Helpers
|
||||
, valkey
|
||||
, valJ
|
||||
, associateJoin
|
||||
|
||||
-- * Re-exports
|
||||
-- $reexports
|
||||
@ -110,6 +111,7 @@ module Database.Esqueleto
|
||||
import Control.Monad.IO.Class (MonadIO)
|
||||
import Control.Monad.Trans.Reader (ReaderT)
|
||||
import Data.Int (Int64)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Database.Esqueleto.Internal.Language
|
||||
import Database.Esqueleto.Internal.Sql
|
||||
import Database.Esqueleto.Internal.PersistentImport
|
||||
@ -439,3 +441,27 @@ deleteKey :: ( PersistStore backend
|
||||
, PersistEntity val )
|
||||
=> Key val -> ReaderT backend m ()
|
||||
deleteKey = Database.Persist.delete
|
||||
|
||||
-- | Avoid N+1 queries and join entities into a map structure
|
||||
-- @
|
||||
-- getFoosAndNestedBarsFromParent :: ParentId -> (Map (Key Foo) (Foo, [Maybe (Entity Bar)]))
|
||||
-- getFoosAndNestedBarsFromParent parentId = 'fmap' associateJoin $ 'select' $
|
||||
-- 'from' $ \\(foo `'LeftOuterJoin`` bar) -> do
|
||||
-- 'on' (bar '?.' BarFooId '==.' foo '^.' FooId)
|
||||
-- 'where_' (foo '^.' FooParentId '==.' 'val' parentId)
|
||||
-- 'pure' (foo, bar)
|
||||
-- @
|
||||
-- /Since: 3.1.2/
|
||||
associateJoin
|
||||
:: forall e1 e0
|
||||
. Ord (Key e0)
|
||||
=> [(Entity e0, e1)]
|
||||
-> Map.Map (Key e0) (e0, [e1])
|
||||
associateJoin = foldr f start
|
||||
where
|
||||
start = Map.empty
|
||||
f (one, many) =
|
||||
Map.insertWith
|
||||
(\(oneOld, manyOld) (_, manyNew) -> (oneOld, manyNew ++ manyOld ))
|
||||
(entityKey one)
|
||||
(entityVal one, [many])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user