Safe Haskell | None |
---|
Type classes for PostgreSQL-backed data models.
- class FromParams p where
- fromParams :: [Param] -> Maybe p
- type Param = (ByteString, ByteString)
- class (FromRow p, ToRow p, ToField (PrimaryKey p), FromField (PrimaryKey p)) => PostgreSQLModel p where
- type PrimaryKey p :: *
- primaryKey :: p -> Maybe (PrimaryKey p)
- tableName :: p -> TableName p
- columns :: TableName p -> [String]
- primaryKeyName :: TableName p -> String
- columns_ :: TableName p -> [String]
- orderBy :: TableName p -> Maybe String
- insert :: p -> Connection -> IO (PrimaryKey p)
- upsert :: p -> Connection -> IO (PrimaryKey p)
- find :: TableName p -> PrimaryKey p -> Connection -> IO (Maybe p)
- findFirst :: ToField f => TableName p -> String -> f -> Connection -> IO (Maybe p)
- findAll :: TableName p -> Connection -> IO [p]
- findAllBy :: ToField f => TableName p -> String -> f -> Connection -> IO [p]
- class (PostgreSQLModel parent, PostgreSQLModel child) => HasMany parent child where
- foreignKey :: TableName parent -> TableName child -> String
- childrenOf :: parent -> TableName child -> Connection -> IO [child]
- childOf :: parent -> TableName child -> PrimaryKey child -> Connection -> IO (Maybe child)
- childOfBy :: ToField v => parent -> TableName child -> String -> v -> Connection -> IO (Maybe child)
- childrenOfBy :: ToField f => parent -> TableName child -> String -> f -> Connection -> IO [child]
- insertFor :: parent -> child -> Connection -> IO (PrimaryKey child)
- newtype TableName p = TableName String
- fromTableName :: TableName p -> String
- fromString :: IsString a => String -> a
- class IsString a where
- fromString :: String -> a
Documentation
class FromParams p whereSource
fromParams :: [Param] -> Maybe pSource
Converts an HTML form into a type
(FromParams a, FromParams b) => FromParams (a, b) |
type Param = (ByteString, ByteString)
Post parameter name and value.
class (FromRow p, ToRow p, ToField (PrimaryKey p), FromField (PrimaryKey p)) => PostgreSQLModel p whereSource
Basis type for PostgreSQL-backed data-models. Instances must, at minimum,
implement primaryKey
, tableName
and columns
. /Note: the column ordering
must match that used in the type's implementation of FromRow
and ToRow
/
type PrimaryKey p :: *Source
primaryKey :: p -> Maybe (PrimaryKey p)Source
Given a model, returns the value of it's primary key. In many cases, this will simply be an alias to a record accessor.
tableName :: p -> TableName pSource
Returns the TableName
of the model. Instances should have a top-level value
for the TableName
that is always returned. For example:
employees :: TableName Employee employees = TableName "employees" instance PostgreSQLModel Employee where ... tableName _ = employess
columns :: TableName p -> [String]Source
primaryKeyName :: TableName p -> StringSource
Name of primary key column (default: "id").
columns_ :: TableName p -> [String]Source
Column names with primary-key name prepended.
orderBy :: TableName p -> Maybe StringSource
insert :: p -> Connection -> IO (PrimaryKey p)Source
Inserts the model into the database. It relies on the primary key being autogenerated by the database.
upsert :: p -> Connection -> IO (PrimaryKey p)Source
Create or update the model (uses the primary key to determine if the model already exists in the database)
find :: TableName p -> PrimaryKey p -> Connection -> IO (Maybe p)Source
Retrieves the single model corresponding to the given primary key
:: ToField f | |
=> TableName p | |
-> String | Search column name |
-> f | Search value |
-> Connection | |
-> IO (Maybe p) |
Finds the first model in the database based on the column-value contstraint.
findAll :: TableName p -> Connection -> IO [p]Source
Retrieves all models in the table
:: ToField f | |
=> TableName p | |
-> String | Search column name |
-> f | Search value |
-> Connection | |
-> IO [p] |
Retrieves all models in the table subject to the column-value constraint.
class (PostgreSQLModel parent, PostgreSQLModel child) => HasMany parent child whereSource
Defines a "has-many" relationship between two models, where the parent
model may be associated with zero or more of the child
model. Specifically,
the child
table has a foreign key column pointing to the parent model.
foreignKey :: TableName parent -> TableName child -> StringSource
childrenOf :: parent -> TableName child -> Connection -> IO [child]Source
childOf :: parent -> TableName child -> PrimaryKey child -> Connection -> IO (Maybe child)Source
childOfBy :: ToField v => parent -> TableName child -> String -> v -> Connection -> IO (Maybe child)Source
childrenOfBy :: ToField f => parent -> TableName child -> String -> f -> Connection -> IO [child]Source
insertFor :: parent -> child -> Connection -> IO (PrimaryKey child)Source
Wrapper type representing PostgreSQL table names
fromTableName :: TableName p -> StringSource
Unwraps a TableName
fromString :: IsString a => String -> a
class IsString a where
Class for string-like datastructures; used by the overloaded string extension (-foverloaded-strings in GHC).
fromString :: String -> a