{-# 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 Control.Monad.Trans.Reader (withReaderT)
import Data.Aeson
import Database.Persist.Class
import Database.Persist.Sql.Class


-- | 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 { forall b s. 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
. 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 { forall b s. BackendKey (Compatible b s) -> BackendKey b
unCompatibleKey :: BackendKey b }

instance (HasPersistBackend b, BackendCompatible b s, PersistStoreRead b) => PersistStoreRead (Compatible b s) where
    get :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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)
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> ReaderT b m (Maybe record)
get
    getMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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)
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
[Key record] -> ReaderT b m (Map (Key record) record)
getMany

instance (HasPersistBackend b, BackendCompatible b s, PersistQueryRead b) => PersistQueryRead (Compatible b s) where
    selectSourceRes :: forall record (m1 :: * -> *) (m2 :: * -> *).
(PersistRecordBackend record (Compatible b s), MonadIO m1,
 MonadIO m2) =>
[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 (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 ()))
forall record (m1 :: * -> *) (m2 :: * -> *).
(PersistRecordBackend record b, MonadIO m1, MonadIO m2) =>
[Filter record]
-> [SelectOpt record]
-> ReaderT b m1 (Acquire (ConduitM () (Entity record) m2 ()))
selectSourceRes [Filter record]
filts [SelectOpt record]
opts
    selectFirst :: forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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))
forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record b) =>
[Filter record]
-> [SelectOpt record] -> ReaderT b m (Maybe (Entity record))
selectFirst [Filter record]
filts [SelectOpt record]
opts
    selectKeysRes :: forall (m1 :: * -> *) (m2 :: * -> *) record.
(MonadIO m1, MonadIO m2,
 PersistRecordBackend record (Compatible b s)) =>
[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 (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 ()))
forall (m1 :: * -> *) (m2 :: * -> *) record.
(MonadIO m1, MonadIO m2, PersistRecordBackend record b) =>
[Filter record]
-> [SelectOpt record]
-> ReaderT b m1 (Acquire (ConduitM () (Key record) m2 ()))
selectKeysRes [Filter record]
filts [SelectOpt record]
opts
    count :: forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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
forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record b) =>
[Filter record] -> ReaderT b m Int
count
    exists :: forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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
forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record b) =>
[Filter record] -> ReaderT b m Bool
exists

instance (HasPersistBackend b, BackendCompatible b s, PersistQueryWrite b) => PersistQueryWrite (Compatible b s) where
    updateWhere :: forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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 ()
forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record b) =>
[Filter record] -> [Update record] -> ReaderT b m ()
updateWhere [Filter record]
filts [Update record]
updates
    deleteWhere :: forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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 ()
forall (m :: * -> *) record.
(MonadIO m, PersistRecordBackend record b) =>
[Filter record] -> ReaderT b m ()
deleteWhere

instance (HasPersistBackend b, BackendCompatible b s, PersistUniqueRead b) => PersistUniqueRead (Compatible b s) where
    getBy :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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))
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Unique record -> ReaderT b m (Maybe (Entity record))
getBy
    existsBy :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
Unique record -> ReaderT (Compatible b s) m Bool
existsBy = (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 (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)
-> (Unique record -> ReaderT b m Bool)
-> Unique record
-> ReaderT (Compatible b s) m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique record -> ReaderT b m Bool
forall backend record (m :: * -> *).
(PersistUniqueRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Unique record -> ReaderT backend m Bool
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Unique record -> ReaderT b m Bool
existsBy

instance (HasPersistBackend b, BackendCompatible b s, PersistStoreWrite b) => PersistStoreWrite (Compatible b s) where
    insert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
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 (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, SafeToInsert record) =>
record -> ReaderT backend m (Key record)
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
record -> ReaderT b m (Key record)
insert
    insert_ :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
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 (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, SafeToInsert record) =>
record -> ReaderT backend m ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
record -> ReaderT b m ()
insert_
    insertMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
[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 (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, SafeToInsert record) =>
[record] -> ReaderT backend m [Key record]
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
[record] -> ReaderT b m [Key record]
insertMany
    insertMany_ :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
[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 (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, SafeToInsert record) =>
[record] -> ReaderT backend m ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
[record] -> ReaderT b m ()
insertMany_
    insertEntityMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
[Entity record] -> ReaderT b m ()
insertEntityMany
    insertKey :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> record -> ReaderT b m ()
insertKey Key record
k
    repsert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> record -> ReaderT b m ()
repsert Key record
k
    repsertMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
[(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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
[(Key record, record)] -> ReaderT b m ()
repsertMany
    replace :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> record -> ReaderT b m ()
replace Key record
k
    delete :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> ReaderT b m ()
delete
    update :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> [Update record] -> ReaderT b m ()
update Key record
k
    updateGet :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Key record -> [Update record] -> ReaderT b m record
updateGet Key record
k

instance (HasPersistBackend b, BackendCompatible b s, PersistUniqueWrite b) => PersistUniqueWrite (Compatible b s) where
    deleteBy :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s)) =>
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 (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 ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b) =>
Unique record -> ReaderT b m ()
deleteBy
    insertUnique :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
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 (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, SafeToInsert record) =>
record -> ReaderT backend m (Maybe (Key record))
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
record -> ReaderT b m (Maybe (Key record))
insertUnique
    upsert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 OnlyOneUniqueKey record, SafeToInsert record) =>
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 (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,
 SafeToInsert record) =>
record -> [Update record] -> ReaderT backend m (Entity record)
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, OnlyOneUniqueKey record,
 SafeToInsert record) =>
record -> [Update record] -> ReaderT b m (Entity record)
upsert record
rec
    upsertBy :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
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 (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, SafeToInsert record) =>
Unique record
-> record -> [Update record] -> ReaderT backend m (Entity record)
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
Unique record
-> record -> [Update record] -> ReaderT b m (Entity record)
upsertBy Unique record
uniq record
rec
    putMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record (Compatible b s),
 SafeToInsert record) =>
[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 (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, SafeToInsert record) =>
[record] -> ReaderT backend m ()
forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record b, SafeToInsert record) =>
[record] -> ReaderT b 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))