Database.Persist.Class.PersistQuery

persistent-2.14.6.3: Type-safe, multi-backend data serialization.

Safe Haskell | None
Language | Haskell2010

Database.Persist.Class.PersistQuery

Synopsis

- selectList :: forall record backend (m :: Type -> Type). (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Entity record]
- class (PersistCore backend, PersistStoreRead backend) => PersistQueryRead backend where
  - selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record backend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT backend m1 (Acquire (ConduitM () (Entity record) m2 ()))
  - selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m (Maybe (Entity record))
  - selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m1 (Acquire (ConduitM () (Key record) m2 ()))
  - count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m Int
  - exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m Bool
- class (PersistQueryRead backend, PersistStoreWrite backend) => PersistQueryWrite backend where
  - updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> [Update record] -> ReaderT backend m ()
  - deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m ()
- selectSource :: forall record backend (m :: Type -> Type). (PersistQueryRead backend, MonadResource m, PersistRecordBackend record backend, MonadReader backend m) => [Filter record] -> [SelectOpt record] -> ConduitM () (Entity record) m ()
- selectKeys :: forall record backend (m :: Type -> Type). (PersistQueryRead backend, MonadResource m, PersistRecordBackend record backend, MonadReader backend m) => [Filter record] -> [SelectOpt record] -> ConduitM () (Key record) m ()
- selectKeysList :: forall record backend (m :: Type -> Type). (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Key record]

# Documentation

selectList :: forall record backend (m :: Type -> Type). (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Entity record] #

Returns a `[`Entity` record]` corresponding to the filters and options provided.

Filters are constructed using the operators defined in Database.Persist (and re-exported from Database.Persist.Sql). Let's look at some examples:

    usersWithAgeOver40 :: SqlPersistT IO [Entity User]
    usersWithAgeOver40 =
        selectList [UserAge >=. 40] []

If you provide multiple values in the list, the conditions are `AND`ed together.

    usersWithAgeBetween30And50 :: SqlPersistT IO [Entity User]
    usersWithAgeBetween30And50 =
         selectList
             [ UserAge >=. 30
             , UserAge <=. 50
             ]
             []

The second list contains the `SelectOpt` for a record. We can select the first ten records with `LimitTo`

    firstTenUsers =
        selectList [] [LimitTo 10]

And we can select the second ten users with `OffsetBy`.

    secondTenUsers =
        selectList [] [LimitTo 10, OffsetBy 10]

Warning that LIMIT/OFFSET is bad for pagination!

The type of record can usually be infered from the types of the provided filters and select options. In the previous two examples, though, you'll notice that the select options are polymorphic, applying to any record type. In order to help type inference in such situations, or simply as an enhancement to readability, you might find type application useful, illustrated below.

    {-# LANGUAGE TypeApplications #-}
    ...

    firstTenUsers =
        selectList User [] [LimitTo 10]

    secondTenUsers =
        selectList User [] [LimitTo 10, OffsetBy 10]

With `Asc` and `Desc`, we can provide the field we want to sort on. We can provide multiple sort orders - later ones are used to sort records that are equal on the first field.

    newestUsers =
        selectList [] [Desc UserCreatedAt, LimitTo 10]

    oldestUsers =
        selectList [] [Asc UserCreatedAt, LimitTo 10]

class (PersistCore backend, PersistStoreRead backend) => PersistQueryRead backend where #

Backends supporting conditional read operations.

Minimal complete definition

selectSourceRes, selectKeysRes, count, exists

Methods

selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record backend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT backend m1 (Acquire (ConduitM () (Entity record) m2 ())) #

Get all records matching the given criterion in the specified order. Returns also the identifiers.

NOTE: This function returns an `Acquire` and a `ConduitM`, which implies that it streams from the database. It does not. Please use `selectList` to simplify the code. If you want streaming behavior, consider `persistent-pagination` which efficiently chunks a query into ranges, or investigate a backend-specific streaming solution.

selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m (Maybe (Entity record)) #

Get just the first record for the criterion.

selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m1 (Acquire (ConduitM () (Key record) m2 ())) #

Get the `Key`s of all records matching the given criterion.

count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m Int #

The total number of records fulfilling the given criterion.

exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m Bool #

Check if there is at least one record fulfilling the given criterion.

Since: 2.11

#### Instances

Instances details

PersistQueryRead SqlReadBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record SqlReadBackend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT SqlReadBackend m1 (Acquire (ConduitM () (Entity record) m2 ())) # selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlReadBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlReadBackend m (Maybe (Entity record)) # selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record SqlReadBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlReadBackend m1 (Acquire (ConduitM () (Key record) m2 ())) # count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlReadBackend) => [Filter record] -> ReaderT SqlReadBackend m Int # exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlReadBackend) => [Filter record] -> ReaderT SqlReadBackend m Bool #
PersistQueryRead SqlWriteBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record SqlWriteBackend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT SqlWriteBackend m1 (Acquire (ConduitM () (Entity record) m2 ())) # selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlWriteBackend m (Maybe (Entity record)) # selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record SqlWriteBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlWriteBackend m1 (Acquire (ConduitM () (Key record) m2 ())) # count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Filter record] -> ReaderT SqlWriteBackend m Int # exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Filter record] -> ReaderT SqlWriteBackend m Bool #
PersistQueryRead SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record SqlBackend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m1 (Acquire (ConduitM () (Entity record) m2 ())) # selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m (Maybe (Entity record)) # selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m1 (Acquire (ConduitM () (Key record) m2 ())) # count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m Int # exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m Bool #
(HasPersistBackend b, BackendCompatible b s, PersistQueryRead b) => PersistQueryRead (Compatible b s) # |
Instance details Defined in Database.Persist.Compatible.Types Methods selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record (Compatible b s), MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT (Compatible b s) m1 (Acquire (ConduitM () (Entity record) m2 ())) # selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (Compatible b s)) => [Filter record] -> [SelectOpt record] -> ReaderT (Compatible b s) m (Maybe (Entity record)) # selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record (Compatible b s)) => [Filter record] -> [SelectOpt record] -> ReaderT (Compatible b s) m1 (Acquire (ConduitM () (Key record) m2 ())) # count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (Compatible b s)) => [Filter record] -> ReaderT (Compatible b s) m Int # exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (Compatible b s)) => [Filter record] -> ReaderT (Compatible b s) m Bool #

class (PersistQueryRead backend, PersistStoreWrite backend) => PersistQueryWrite backend where #

Backends supporting conditional write operations

Methods

updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> [Update record] -> ReaderT backend m () #

Update individual fields on any record matching the given criterion.

deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m () #

Delete all records matching the given criterion.

#### Instances

Instances details

PersistQueryWrite SqlWriteBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Filter record] -> [Update record] -> ReaderT SqlWriteBackend m () # deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Filter record] -> ReaderT SqlWriteBackend m () #
PersistQueryWrite SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> [Update record] -> ReaderT SqlBackend m () # deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m () #
(HasPersistBackend b, BackendCompatible b s, PersistQueryWrite b) => PersistQueryWrite (Compatible b s) # |
Instance details Defined in Database.Persist.Compatible.Types Methods updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (Compatible b s)) => [Filter record] -> [Update record] -> ReaderT (Compatible b s) m () # deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (Compatible b s)) => [Filter record] -> ReaderT (Compatible b s) m () #

selectSource :: forall record backend (m :: Type -> Type). (PersistQueryRead backend, MonadResource m, PersistRecordBackend record backend, MonadReader backend m) => [Filter record] -> [SelectOpt record] -> ConduitM () (Entity record) m () #

Get all records matching the given criterion in the specified order. Returns also the identifiers.

WARNING: This function returns a `ConduitM`, which suggests that it streams the results. It does not stream results on most backends. If you need streaming, see `persistent-pagination` for a means of chunking results based on indexed ranges.

selectKeys :: forall record backend (m :: Type -> Type). (PersistQueryRead backend, MonadResource m, PersistRecordBackend record backend, MonadReader backend m) => [Filter record] -> [SelectOpt record] -> ConduitM () (Key record) m () #

Get the `Key`s of all records matching the given criterion.

For an example, see `selectList`.

selectKeysList :: forall record backend (m :: Type -> Type). (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Key record] #

Call `selectKeys` but return the result as a list.