move function and bump version

- moves associateJoin to Database.Esqueleto
- relaxes bounds on containers dep
-
This commit is contained in:
brady.ouren 2019-10-25 11:58:54 -07:00
parent e94808856f
commit 7b3cb37131
5 changed files with 36 additions and 29 deletions

View File

@ -1,3 +1,9 @@
3.1.2
========
- @tippenein
- [#149](https://github.com/bitemyapp/esqueleto/pull/157): Added `associateJoin` query helpers.
3.1.1
========

View File

@ -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,7 +47,7 @@ library
, aeson >=1.0
, blaze-html
, bytestring
, containers >=0.6
, containers
, conduit >=1.3
, monad-logger
, persistent >=2.10.0 && <2.11

View File

@ -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])

View File

@ -50,7 +50,6 @@ import qualified Data.ByteString as B
import qualified Data.Conduit as C
import qualified Data.Conduit.List as CL
import qualified Data.HashSet as HS
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TLB
@ -2885,27 +2884,3 @@ insertSelect = void . insertSelectCount
insertSelectCount :: (MonadIO m, PersistEntity a) =>
SqlQuery (SqlExpr (Insertion a)) -> SqlWriteT m Int64
insertSelectCount = rawEsqueleto INSERT_INTO . fmap EInsertFinal
-- | Avoid N+1 queries and join entities into a map structure
-- @
-- getFoosAndNestedBarsFromParent :: ParentId -> (Map (Key Foo) (Foo, [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)
-- @
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])

View File

@ -55,7 +55,7 @@ module Database.Esqueleto.Internal.Language
, subList_select, valList, justList
, in_, notIn, exists, notExists
, set, (=.), (+=.), (-=.), (*=.), (/=.)
, case_, toBaseId, (<#), (<&>), associateJoin
, case_, toBaseId, (<#), (<&>)
) where
import Database.Esqueleto.Internal.PersistentImport