{-# OPTIONS_GHC -Wno-unused-top-binds #-}
{- You can't export a data family constructor, so there's an "unused" warning -}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}

module Database.Persist.Compatible.Types
    ( Compatible(..)
    ) where

import Data.Aeson
import Database.Persist.Class
import Database.Persist.Sql.Class
import Control.Monad.Trans.Reader (withReaderT)


-- | A newtype wrapper for compatible backends, mainly useful for @DerivingVia@.
--
-- When writing a new backend that is 'BackendCompatible' with an existing backend,
-- instances for the new backend can be naturally defined in terms of the
-- instances for the existing backend.
--
-- For example, if you decide to augment the 'SqlBackend' with some additional
-- features:
--
-- @
-- data BetterSqlBackend = BetterSqlBackend { sqlBackend :: SqlBackend, ... }
--
-- instance BackendCompatible SqlBackend BetterSqlBackend where
--   projectBackend = sqlBackend
-- @
--
-- Then you can use @DerivingVia@ to automatically get instances like:
--
-- @
-- deriving via (Compatible SqlBackend BetterSqlBackend) instance PersistStoreRead BetterSqlBackend
-- deriving via (Compatible SqlBackend BetterSqlBackend) instance PersistStoreWrite BetterSqlBackend
-- ...
-- @
--
-- These instances will go through the compatible backend (in this case, 'SqlBackend')
-- for all their queries.
--
-- These instances require that both backends have the same 'BaseBackend', but
-- deriving 'HasPersistBackend' will enforce that for you.
--
-- @
-- deriving via (Compatible SqlBackend BetterSqlBackend) instance HasPersistBackend BetterSqlBackend
-- @
--
-- @since 2.12
newtype Compatible b s = Compatible { Compatible b s -> s
unCompatible :: s }

instance (BackendCompatible b s, HasPersistBackend b) => HasPersistBackend (Compatible b s) where
    type BaseBackend (Compatible b s) = BaseBackend b
    persistBackend :: Compatible b s -> BaseBackend (Compatible b s)
persistBackend = b -> BaseBackend b
forall backend.
HasPersistBackend backend =>
backend -> BaseBackend backend
persistBackend (b -> BaseBackend b)
-> (Compatible b s -> b) -> Compatible b s -> BaseBackend b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible

instance (BackendCompatible b s, PersistCore b) => PersistCore (Compatible b s) where
    -- | A newtype wrapper around @'BackendKey' b@, mainly useful for @DerivingVia@.
    --
    -- Similarly to @'Compatible' b s@, this data family instance is handy for deriving
    -- instances for @'BackendKey' s@ by defining them in terms of @'BackendKey' b@.
    --
    --
    -- For example, if you decide to augment the 'SqlBackend' with some additional
    -- features:
    --
    -- @
    -- data BetterSqlBackend = BetterSqlBackend { sqlBackend :: SqlBackend, ... }
    --
    -- instance PersistCore BetterSqlBackend where
    --   newtype BackendKey BetterSqlBackend = BSQLKey { unBSQLKey :: BackendKey (Compatible SqlBackend BetterSqlBackend) }
    -- @
    --
    -- Then you can use @DerivingVia@ to automatically get instances like:
    --
    -- @
    -- deriving via BackendKey (Compatible SqlBackend BetterSqlBackend) instance Show (BackendKey BetterSqlBackend)
    -- ...
    -- @
    --
    -- These instances will go through the compatible backend's key (in this case,
    -- @'BackendKey' 'SqlBackend'@) for all their logic.
    newtype BackendKey (Compatible b s) = CompatibleKey { BackendKey (Compatible b s) -> BackendKey b
unCompatibleKey :: BackendKey b }

instance (HasPersistBackend b, BackendCompatible b s, PersistStoreRead b) => PersistStoreRead (Compatible b s) where
    get :: Key record -> ReaderT (Compatible b s) m (Maybe record)
get = (Compatible b s -> b)
-> ReaderT b m (Maybe record)
-> ReaderT (Compatible b s) m (Maybe record)
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Maybe record)
 -> ReaderT (Compatible b s) m (Maybe record))
-> (Key record -> ReaderT b m (Maybe record))
-> Key record
-> ReaderT (Compatible b s) m (Maybe record)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> ReaderT b m (Maybe record)
forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get
    getMany :: [Key record]
-> ReaderT (Compatible b s) m (Map (Key record) record)
getMany = (Compatible b s -> b)
-> ReaderT b m (Map (Key record) record)
-> ReaderT (Compatible b s) m (Map (Key record) record)
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Map (Key record) record)
 -> ReaderT (Compatible b s) m (Map (Key record) record))
-> ([Key record] -> ReaderT b m (Map (Key record) record))
-> [Key record]
-> ReaderT (Compatible b s) m (Map (Key record) record)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Key record] -> ReaderT b m (Map (Key record) record)
forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Key record] -> ReaderT backend m (Map (Key record) record)
getMany

instance (HasPersistBackend b, BackendCompatible b s, PersistQueryRead b) => PersistQueryRead (Compatible b s) where
    selectSourceRes :: [Filter record]
-> [SelectOpt record]
-> ReaderT
     (Compatible b s) m1 (Acquire (ConduitM () (Entity record) m2 ()))
selectSourceRes [Filter record]
filts [SelectOpt record]
opts = (Compatible b s -> b)
-> ReaderT b m1 (Acquire (ConduitM () (Entity record) m2 ()))
-> ReaderT
     (Compatible b s) m1 (Acquire (ConduitM () (Entity record) m2 ()))
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m1 (Acquire (ConduitM () (Entity record) m2 ()))
 -> ReaderT
      (Compatible b s) m1 (Acquire (ConduitM () (Entity record) m2 ())))
-> ReaderT b m1 (Acquire (ConduitM () (Entity record) m2 ()))
-> ReaderT
     (Compatible b s) m1 (Acquire (ConduitM () (Entity record) m2 ()))
forall a b. (a -> b) -> a -> b
$ [Filter record]
-> [SelectOpt record]
-> ReaderT b m1 (Acquire (ConduitM () (Entity record) m2 ()))
forall backend record (m1 :: * -> *) (m2 :: * -> *).
(PersistQueryRead backend, PersistRecordBackend record backend,
 MonadIO m1, MonadIO m2) =>
[Filter record]
-> [SelectOpt record]
-> ReaderT backend m1 (Acquire (ConduitM () (Entity record) m2 ()))
selectSourceRes [Filter record]
filts [SelectOpt record]
opts
    selectFirst :: [Filter record]
-> [SelectOpt record]
-> ReaderT (Compatible b s) m (Maybe (Entity record))
selectFirst [Filter record]
filts [SelectOpt record]
opts = (Compatible b s -> b)
-> ReaderT b m (Maybe (Entity record))
-> ReaderT (Compatible b s) m (Maybe (Entity record))
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Maybe (Entity record))
 -> ReaderT (Compatible b s) m (Maybe (Entity record)))
-> ReaderT b m (Maybe (Entity record))
-> ReaderT (Compatible b s) m (Maybe (Entity record))
forall a b. (a -> b) -> a -> b
$ [Filter record]
-> [SelectOpt record] -> ReaderT b m (Maybe (Entity record))
forall backend (m :: * -> *) record.
(PersistQueryRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record]
-> [SelectOpt record] -> ReaderT backend m (Maybe (Entity record))
selectFirst [Filter record]
filts [SelectOpt record]
opts
    selectKeysRes :: [Filter record]
-> [SelectOpt record]
-> ReaderT
     (Compatible b s) m1 (Acquire (ConduitM () (Key record) m2 ()))
selectKeysRes [Filter record]
filts [SelectOpt record]
opts = (Compatible b s -> b)
-> ReaderT b m1 (Acquire (ConduitM () (Key record) m2 ()))
-> ReaderT
     (Compatible b s) m1 (Acquire (ConduitM () (Key record) m2 ()))
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m1 (Acquire (ConduitM () (Key record) m2 ()))
 -> ReaderT
      (Compatible b s) m1 (Acquire (ConduitM () (Key record) m2 ())))
-> ReaderT b m1 (Acquire (ConduitM () (Key record) m2 ()))
-> ReaderT
     (Compatible b s) m1 (Acquire (ConduitM () (Key record) m2 ()))
forall a b. (a -> b) -> a -> b
$ [Filter record]
-> [SelectOpt record]
-> ReaderT b m1 (Acquire (ConduitM () (Key record) m2 ()))
forall backend (m1 :: * -> *) (m2 :: * -> *) record.
(PersistQueryRead backend, MonadIO m1, MonadIO m2,
 PersistRecordBackend record backend) =>
[Filter record]
-> [SelectOpt record]
-> ReaderT backend m1 (Acquire (ConduitM () (Key record) m2 ()))
selectKeysRes [Filter record]
filts [SelectOpt record]
opts
    count :: [Filter record] -> ReaderT (Compatible b s) m Int
count = (Compatible b s -> b)
-> ReaderT b m Int -> ReaderT (Compatible b s) m Int
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m Int -> ReaderT (Compatible b s) m Int)
-> ([Filter record] -> ReaderT b m Int)
-> [Filter record]
-> ReaderT (Compatible b s) m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Filter record] -> ReaderT b m Int
forall backend (m :: * -> *) record.
(PersistQueryRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> ReaderT backend m Int
count
    exists :: [Filter record] -> ReaderT (Compatible b s) m Bool
exists = (Compatible b s -> b)
-> ReaderT b m Bool -> ReaderT (Compatible b s) m Bool
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m Bool -> ReaderT (Compatible b s) m Bool)
-> ([Filter record] -> ReaderT b m Bool)
-> [Filter record]
-> ReaderT (Compatible b s) m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Filter record] -> ReaderT b m Bool
forall backend (m :: * -> *) record.
(PersistQueryRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> ReaderT backend m Bool
exists

instance (HasPersistBackend b, BackendCompatible b s, PersistQueryWrite b) => PersistQueryWrite (Compatible b s) where
    updateWhere :: [Filter record] -> [Update record] -> ReaderT (Compatible b s) m ()
updateWhere [Filter record]
filts [Update record]
updates = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall a b. (a -> b) -> a -> b
$ [Filter record] -> [Update record] -> ReaderT b m ()
forall backend (m :: * -> *) record.
(PersistQueryWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> [Update record] -> ReaderT backend m ()
updateWhere [Filter record]
filts [Update record]
updates
    deleteWhere :: [Filter record] -> ReaderT (Compatible b s) m ()
deleteWhere = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ([Filter record] -> ReaderT b m ())
-> [Filter record]
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Filter record] -> ReaderT b m ()
forall backend (m :: * -> *) record.
(PersistQueryWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> ReaderT backend m ()
deleteWhere

instance (HasPersistBackend b, BackendCompatible b s, PersistUniqueRead b) => PersistUniqueRead (Compatible b s) where
    getBy :: Unique record -> ReaderT (Compatible b s) m (Maybe (Entity record))
getBy = (Compatible b s -> b)
-> ReaderT b m (Maybe (Entity record))
-> ReaderT (Compatible b s) m (Maybe (Entity record))
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Maybe (Entity record))
 -> ReaderT (Compatible b s) m (Maybe (Entity record)))
-> (Unique record -> ReaderT b m (Maybe (Entity record)))
-> Unique record
-> ReaderT (Compatible b s) m (Maybe (Entity record))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique record -> ReaderT b m (Maybe (Entity record))
forall backend record (m :: * -> *).
(PersistUniqueRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Unique record -> ReaderT backend m (Maybe (Entity record))
getBy

instance (HasPersistBackend b, BackendCompatible b s, PersistStoreWrite b) => PersistStoreWrite (Compatible b s) where
    insert :: record -> ReaderT (Compatible b s) m (Key record)
insert = (Compatible b s -> b)
-> ReaderT b m (Key record)
-> ReaderT (Compatible b s) m (Key record)
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Key record)
 -> ReaderT (Compatible b s) m (Key record))
-> (record -> ReaderT b m (Key record))
-> record
-> ReaderT (Compatible b s) m (Key record)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. record -> ReaderT b m (Key record)
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
record -> ReaderT backend m (Key record)
insert
    insert_ :: record -> ReaderT (Compatible b s) m ()
insert_ = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> (record -> ReaderT b m ())
-> record
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. record -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
record -> ReaderT backend m ()
insert_
    insertMany :: [record] -> ReaderT (Compatible b s) m [Key record]
insertMany = (Compatible b s -> b)
-> ReaderT b m [Key record]
-> ReaderT (Compatible b s) m [Key record]
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m [Key record]
 -> ReaderT (Compatible b s) m [Key record])
-> ([record] -> ReaderT b m [Key record])
-> [record]
-> ReaderT (Compatible b s) m [Key record]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [record] -> ReaderT b m [Key record]
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[record] -> ReaderT backend m [Key record]
insertMany
    insertMany_ :: [record] -> ReaderT (Compatible b s) m ()
insertMany_ = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ([record] -> ReaderT b m ())
-> [record]
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [record] -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[record] -> ReaderT backend m ()
insertMany_
    insertEntityMany :: [Entity record] -> ReaderT (Compatible b s) m ()
insertEntityMany = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ([Entity record] -> ReaderT b m ())
-> [Entity record]
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Entity record] -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Entity record] -> ReaderT backend m ()
insertEntityMany
    insertKey :: Key record -> record -> ReaderT (Compatible b s) m ()
insertKey Key record
k = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> (record -> ReaderT b m ())
-> record
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> record -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
insertKey Key record
k
    repsert :: Key record -> record -> ReaderT (Compatible b s) m ()
repsert Key record
k = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> (record -> ReaderT b m ())
-> record
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> record -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
repsert Key record
k
    repsertMany :: [(Key record, record)] -> ReaderT (Compatible b s) m ()
repsertMany = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ([(Key record, record)] -> ReaderT b m ())
-> [(Key record, record)]
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Key record, record)] -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[(Key record, record)] -> ReaderT backend m ()
repsertMany
    replace :: Key record -> record -> ReaderT (Compatible b s) m ()
replace Key record
k = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> (record -> ReaderT b m ())
-> record
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> record -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
replace Key record
k
    delete :: Key record -> ReaderT (Compatible b s) m ()
delete = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> (Key record -> ReaderT b m ())
-> Key record
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m ()
delete
    update :: Key record -> [Update record] -> ReaderT (Compatible b s) m ()
update Key record
k = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ([Update record] -> ReaderT b m ())
-> [Update record]
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> [Update record] -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> [Update record] -> ReaderT backend m ()
update Key record
k
    updateGet :: Key record -> [Update record] -> ReaderT (Compatible b s) m record
updateGet Key record
k = (Compatible b s -> b)
-> ReaderT b m record -> ReaderT (Compatible b s) m record
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m record -> ReaderT (Compatible b s) m record)
-> ([Update record] -> ReaderT b m record)
-> [Update record]
-> ReaderT (Compatible b s) m record
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key record -> [Update record] -> ReaderT b m record
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> [Update record] -> ReaderT backend m record
updateGet Key record
k

instance (HasPersistBackend b, BackendCompatible b s, PersistUniqueWrite b) => PersistUniqueWrite (Compatible b s) where
    deleteBy :: Unique record -> ReaderT (Compatible b s) m ()
deleteBy = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> (Unique record -> ReaderT b m ())
-> Unique record
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique record -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Unique record -> ReaderT backend m ()
deleteBy
    insertUnique :: record -> ReaderT (Compatible b s) m (Maybe (Key record))
insertUnique = (Compatible b s -> b)
-> ReaderT b m (Maybe (Key record))
-> ReaderT (Compatible b s) m (Maybe (Key record))
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Maybe (Key record))
 -> ReaderT (Compatible b s) m (Maybe (Key record)))
-> (record -> ReaderT b m (Maybe (Key record)))
-> record
-> ReaderT (Compatible b s) m (Maybe (Key record))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. record -> ReaderT b m (Maybe (Key record))
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
record -> ReaderT backend m (Maybe (Key record))
insertUnique
    upsert :: record
-> [Update record] -> ReaderT (Compatible b s) m (Entity record)
upsert record
rec = (Compatible b s -> b)
-> ReaderT b m (Entity record)
-> ReaderT (Compatible b s) m (Entity record)
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Entity record)
 -> ReaderT (Compatible b s) m (Entity record))
-> ([Update record] -> ReaderT b m (Entity record))
-> [Update record]
-> ReaderT (Compatible b s) m (Entity record)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. record -> [Update record] -> ReaderT b m (Entity record)
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend, OnlyOneUniqueKey record) =>
record -> [Update record] -> ReaderT backend m (Entity record)
upsert record
rec
    upsertBy :: Unique record
-> record
-> [Update record]
-> ReaderT (Compatible b s) m (Entity record)
upsertBy Unique record
uniq record
rec = (Compatible b s -> b)
-> ReaderT b m (Entity record)
-> ReaderT (Compatible b s) m (Entity record)
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m (Entity record)
 -> ReaderT (Compatible b s) m (Entity record))
-> ([Update record] -> ReaderT b m (Entity record))
-> [Update record]
-> ReaderT (Compatible b s) m (Entity record)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique record
-> record -> [Update record] -> ReaderT b m (Entity record)
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Unique record
-> record -> [Update record] -> ReaderT backend m (Entity record)
upsertBy Unique record
uniq record
rec
    putMany :: [record] -> ReaderT (Compatible b s) m ()
putMany = (Compatible b s -> b)
-> ReaderT b m () -> ReaderT (Compatible b s) m ()
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
withReaderT (BackendCompatible b s => s -> b
forall sup sub. BackendCompatible sup sub => sub -> sup
projectBackend @b @s (s -> b) -> (Compatible b s -> s) -> Compatible b s -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compatible b s -> s
forall b s. Compatible b s -> s
unCompatible) (ReaderT b m () -> ReaderT (Compatible b s) m ())
-> ([record] -> ReaderT b m ())
-> [record]
-> ReaderT (Compatible b s) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [record] -> ReaderT b m ()
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[record] -> ReaderT backend m ()
putMany

deriving via (BackendKey b) instance (BackendCompatible b s, Show (BackendKey b)) => Show (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Read (BackendKey b)) => Read (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Eq (BackendKey b)) => Eq (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Ord (BackendKey b)) => Ord (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Num (BackendKey b)) => Num (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Integral (BackendKey b)) => Integral (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, PersistField (BackendKey b)) => PersistField (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, PersistFieldSql (BackendKey b)) => PersistFieldSql (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Real (BackendKey b)) => Real (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Enum (BackendKey b)) => Enum (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, Bounded (BackendKey b)) => Bounded (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, ToJSON (BackendKey b)) => ToJSON (BackendKey (Compatible b s))
deriving via (BackendKey b) instance (BackendCompatible b s, FromJSON (BackendKey b)) => FromJSON (BackendKey (Compatible b s))