IHP Api Reference
Copyright(c) digitally induced GmbH 2020
Safe HaskellNone

IHP.Fetch

Description

This modules builds on top of QueryBuilder and provides functions to fetch a query builder.

For more complex sql queries, use sqlQuery.

Synopsis

Documentation

findManyBy :: forall {k} {value} {name :: Symbol} {table :: Symbol} {queryBuilderProvider} {joinRegister :: k} {model}. (ToField value, HasField name (GetModelByTableName table) value, EqOrIsOperator value, HasQueryBuilder queryBuilderProvider joinRegister, Fetchable (queryBuilderProvider table) model, Table model, Table (GetModelByTableName table), FromRow model, ?modelContext :: ModelContext, KnownSymbol table, KnownSymbol name) => Proxy name -> value -> queryBuilderProvider table -> IO (FetchResult (queryBuilderProvider table) model) Source #

findMaybeBy :: forall {k} {value} {name :: Symbol} {table :: Symbol} {queryBuilderProvider} {joinRegister :: k} {model}. (ToField value, HasField name (GetModelByTableName table) value, EqOrIsOperator value, HasQueryBuilder queryBuilderProvider joinRegister, Fetchable (queryBuilderProvider table) model, Table model, Table (GetModelByTableName table), FromRow model, ?modelContext :: ModelContext, KnownSymbol table, KnownSymbol name) => Proxy name -> value -> queryBuilderProvider table -> IO (Maybe model) Source #

findBy :: forall {k} {value} {name :: Symbol} {table :: Symbol} {queryBuilderProvider} {joinRegister :: k} {model}. (ToField value, HasField name (GetModelByTableName table) value, EqOrIsOperator value, HasQueryBuilder queryBuilderProvider joinRegister, Fetchable (queryBuilderProvider table) model, Table model, Table (GetModelByTableName table), FromRow model, ?modelContext :: ModelContext, KnownSymbol table, KnownSymbol name) => Proxy name -> value -> queryBuilderProvider table -> IO model Source #

newtype In a #

Constructors

In a 

Instances

Instances details
Functor In 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

fmap :: (a -> b) -> In a -> In b #

(<$) :: a -> In b -> In a #

Read a => Read (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

readsPrec :: Int -> ReadS (In a)

readList :: ReadS [In a]

readPrec :: ReadPrec (In a)

readListPrec :: ReadPrec [In a]

Show a => Show (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

showsPrec :: Int -> In a -> ShowS #

show :: In a -> String

showList :: [In a] -> ShowS #

Eq a => Eq (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

(==) :: In a -> In a -> Bool #

(/=) :: In a -> In a -> Bool #

Ord a => Ord (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

compare :: In a -> In a -> Ordering #

(<) :: In a -> In a -> Bool #

(<=) :: In a -> In a -> Bool #

(>) :: In a -> In a -> Bool #

(>=) :: In a -> In a -> Bool #

max :: In a -> In a -> In a #

min :: In a -> In a -> In a #

ToField a => ToField (In [a]) 
Instance details

Defined in Database.PostgreSQL.Simple.ToField

Methods

toField :: In [a] -> Action

genericFetchId :: forall (table :: Symbol) model. (Table model, KnownSymbol table, FromRow model, ?modelContext :: ModelContext, FilterPrimaryKey table, model ~ GetModelByTableName table, GetTableName model ~ table) => Id' table -> IO [model] Source #

genericfetchIdOneOrNothing :: forall (table :: Symbol) model. (Table model, KnownSymbol table, FromRow model, ?modelContext :: ModelContext, FilterPrimaryKey table, model ~ GetModelByTableName table, GetTableName model ~ table) => Id' table -> IO (Maybe model) Source #

genericFetchIdOne :: forall (table :: Symbol) model. (Table model, KnownSymbol table, FromRow model, ?modelContext :: ModelContext, FilterPrimaryKey table, model ~ GetModelByTableName table, GetTableName model ~ table) => Id' table -> IO model Source #

class Fetchable fetchable model | fetchable -> model where Source #

Associated Types

type FetchResult fetchable model Source #

Methods

fetch :: fetchable -> IO (FetchResult fetchable model) Source #

fetchOneOrNothing :: fetchable -> IO (Maybe model) Source #

fetchOne :: fetchable -> IO model Source #

Instances

Instances details
(model ~ GetModelById (Id' table), GetTableName model ~ table, FilterPrimaryKey table) => Fetchable (Id' table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (Id' table) model 
Instance details

Defined in IHP.Fetch

type FetchResult (Id' table) model = model

Methods

fetch :: Id' table -> IO (FetchResult (Id' table) model) Source #

fetchOneOrNothing :: Id' table -> IO (Maybe model) Source #

fetchOne :: Id' table -> IO model Source #

(model ~ GetModelByTableName table, KnownSymbol table) => Fetchable (NoJoinQueryBuilderWrapper table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (NoJoinQueryBuilderWrapper table) model 
Instance details

Defined in IHP.Fetch

type FetchResult (NoJoinQueryBuilderWrapper table) model = [model]
(model ~ GetModelByTableName table, KnownSymbol table) => Fetchable (QueryBuilder table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (QueryBuilder table) model 
Instance details

Defined in IHP.Fetch

type FetchResult (QueryBuilder table) model = [model]

Methods

fetch :: QueryBuilder table -> IO (FetchResult (QueryBuilder table) model) Source #

fetchOneOrNothing :: QueryBuilder table -> IO (Maybe model) Source #

fetchOne :: QueryBuilder table -> IO model Source #

(model ~ GetModelById (Id' table), GetTableName model ~ table, FilterPrimaryKey table) => Fetchable (Maybe (Id' table)) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (Maybe (Id' table)) model 
Instance details

Defined in IHP.Fetch

type FetchResult (Maybe (Id' table)) model = [model]

Methods

fetch :: Maybe (Id' table) -> IO (FetchResult (Maybe (Id' table)) model) Source #

fetchOneOrNothing :: Maybe (Id' table) -> IO (Maybe model) Source #

fetchOne :: Maybe (Id' table) -> IO model Source #

(model ~ GetModelById (Id' table), GetModelByTableName table ~ model, GetTableName model ~ table) => Fetchable [Id' table] model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult [Id' table] model 
Instance details

Defined in IHP.Fetch

type FetchResult [Id' table] model = [model]

Methods

fetch :: [Id' table] -> IO (FetchResult [Id' table] model) Source #

fetchOneOrNothing :: [Id' table] -> IO (Maybe model) Source #

fetchOne :: [Id' table] -> IO model Source #

(model ~ GetModelByTableName table, KnownSymbol table) => Fetchable (JoinQueryBuilderWrapper r table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (JoinQueryBuilderWrapper r table) model 
Instance details

Defined in IHP.Fetch

type FetchResult (JoinQueryBuilderWrapper r table) model = [model]
(model ~ GetModelByTableName table, KnownSymbol table, FromField value, KnownSymbol foreignTable, foreignModel ~ GetModelByTableName foreignTable, KnownSymbol columnName, HasField columnName foreignModel value, HasQueryBuilder (LabeledQueryBuilderWrapper foreignTable columnName value) NoJoins) => Fetchable (LabeledQueryBuilderWrapper foreignTable columnName value table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (LabeledQueryBuilderWrapper foreignTable columnName value table) model 
Instance details

Defined in IHP.Fetch

type FetchResult (LabeledQueryBuilderWrapper foreignTable columnName value table) model = [LabeledData value model]

Methods

fetch :: LabeledQueryBuilderWrapper foreignTable columnName value table -> IO (FetchResult (LabeledQueryBuilderWrapper foreignTable columnName value table) model) Source #

fetchOneOrNothing :: LabeledQueryBuilderWrapper foreignTable columnName value table -> IO (Maybe model) Source #

fetchOne :: LabeledQueryBuilderWrapper foreignTable columnName value table -> IO model Source #

genericFetchIds :: forall (table :: Symbol) model. (Table model, KnownSymbol table, FromRow model, ?modelContext :: ModelContext, model ~ GetModelByTableName table, GetTableName model ~ table) => [Id model] -> IO [model] Source #

genericfetchIdsOneOrNothing :: forall (table :: Symbol) model. (Table model, KnownSymbol table, FromRow model, ?modelContext :: ModelContext, model ~ GetModelByTableName table, GetTableName model ~ table) => [Id model] -> IO (Maybe model) Source #

genericFetchIdsOne :: forall (table :: Symbol) model. (Table model, KnownSymbol table, FromRow model, ?modelContext :: ModelContext, model ~ GetModelByTableName table, GetTableName model ~ table) => [Id model] -> IO model Source #

fetchCount :: forall {k} (table :: Symbol) queryBuilderProvider (joinRegister :: k). (?modelContext :: ModelContext, KnownSymbol table, HasQueryBuilder queryBuilderProvider joinRegister) => queryBuilderProvider table -> IO Int Source #

Returns the count of records selected by the query builder.

Example: Counting all users.

allUsersCount <- query @User |> fetchCount -- SELECT COUNT(*) FROM users

Example: Counting all active projects

    activeProjectsCount <- query @Project
        |> filterWhere (#isActive, True)
        |> fetchCount
    -- SELECT COUNT(*) FROM projects WHERE is_active = true

fetchExists :: forall {k} (table :: Symbol) queryBuilderProvider (joinRegister :: k). (?modelContext :: ModelContext, KnownSymbol table, HasQueryBuilder queryBuilderProvider joinRegister) => queryBuilderProvider table -> IO Bool Source #

Checks whether the query has any results.

Returns True when there is at least one row matching the conditions of the query. Returns False otherwise.

Example: Checking whether there are unread messages

    hasUnreadMessages <- query @Message
        |> filterWhere (#isUnread, True)
        |> fetchExists
    -- SELECT EXISTS (SELECT * FROM messages WHERE is_unread = true)

fetchSQLQuery :: (FromRow model, ?modelContext :: ModelContext) => SQLQuery -> IO [model] Source #

fetchLatest :: forall {k} (table :: Symbol) queryBuilderProvider (joinRegister :: k) model. (?modelContext :: ModelContext, model ~ GetModelByTableName table, KnownSymbol table, HasQueryBuilder queryBuilderProvider joinRegister, HasField "createdAt" model UTCTime, Fetchable (queryBuilderProvider table) model, Table model, FromRow model) => queryBuilderProvider table -> IO (Maybe model) Source #

Returns the latest record or Nothing

Example:

latestUser <-
    query @User
        |> fetchLatest

fetchLatest is mainly a shortcut for code like this:

latestUser <-
    query @User
        |> orderByDesc #createdAt
        |> fetchOneOrNothing

fetchLatestBy :: forall {k} (table :: Symbol) (createdAt :: Symbol) queryBuilderProvider (joinRegister :: k) model. (?modelContext :: ModelContext, KnownSymbol createdAt, model ~ GetModelByTableName table, KnownSymbol table, HasQueryBuilder queryBuilderProvider joinRegister, HasField createdAt model UTCTime, Fetchable (queryBuilderProvider table) model, Table model, FromRow model) => Proxy createdAt -> queryBuilderProvider table -> IO (Maybe model) Source #

Provided a field name, it returns the latest record or Nothing

See fetchLatest if you're looking for the latest record by the createdAt timestamp.

Example:

latestTrialUser <-
    query @User
        |> fetchLatestBy #trialStartedAt

fetchLatestBy is mainly a shortcut for code like this:

latestUser <-
    query @User
        |> orderByDesc #trialStartedAt
        |> fetchOneOrNothing