Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module exports the most commonly used functions and datatypes.
See http://github.com/lykahb/groundhog/blob/master/examples/.
Synopsis
- class (Monad m, MonadIO m, MonadFail m, ConnectionManager (Conn m), PersistBackendConn (Conn m)) => PersistBackend m where
- type Conn m
- getConnection :: m (Conn m)
- class (DbDescriptor conn, ConnectionManager conn) => PersistBackendConn conn where
- insert :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m (AutoKey v)
- insert_ :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m ()
- insertBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u)), PersistBackend m, Conn m ~ conn) => u (UniqueMarker v) -> v -> m (Either (AutoKey v) (AutoKey v))
- insertByAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m (Either (AutoKey v) (AutoKey v))
- replace :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific), PersistBackend m, Conn m ~ conn) => Key v BackendSpecific -> v -> m ()
- replaceBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u)), PersistBackend m, Conn m ~ conn) => u (UniqueMarker v) -> v -> m ()
- select :: (PersistEntity v, EntityConstr v c, HasSelectOptions opts conn (RestrictionHolder v c), PersistBackend m, Conn m ~ conn) => opts -> m [v]
- selectAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => m [(AutoKey v, v)]
- get :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific), PersistBackend m, Conn m ~ conn) => Key v BackendSpecific -> m (Maybe v)
- getBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u)), PersistBackend m, Conn m ~ conn) => Key v (Unique u) -> m (Maybe v)
- update :: (PersistEntity v, EntityConstr v c, PersistBackend m, Conn m ~ conn) => [Update conn (RestrictionHolder v c)] -> Cond conn (RestrictionHolder v c) -> m ()
- delete :: (PersistEntity v, EntityConstr v c, PersistBackend m, Conn m ~ conn) => Cond conn (RestrictionHolder v c) -> m ()
- deleteBy :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific), PersistBackend m, Conn m ~ conn) => Key v BackendSpecific -> m ()
- deleteAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m ()
- count :: (PersistEntity v, EntityConstr v c, PersistBackend m, Conn m ~ conn) => Cond conn (RestrictionHolder v c) -> m Int
- countAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m Int
- project :: (PersistEntity v, EntityConstr v c, Projection' p conn (RestrictionHolder v c) a, HasSelectOptions opts conn (RestrictionHolder v c), PersistBackend m, Conn m ~ conn) => p -> opts -> m [a]
- migrate :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> Migration m
- executeRaw :: (PersistBackend m, Conn m ~ conn) => Bool -> String -> [PersistValue] -> m ()
- queryRaw :: (PersistBackend m, Conn m ~ conn) => Bool -> String -> [PersistValue] -> m (RowStream [PersistValue])
- insertList :: (PersistField a, PersistBackend m, Conn m ~ conn) => [a] -> m Int64
- getList :: (PersistField a, PersistBackend m, Conn m ~ conn) => Int64 -> m [a]
- data family Key v :: Type -> Type
- type family DefaultKey v
- type family AutoKey v
- data Unique (u :: (Type -> Type) -> Type)
- data UniqueMarker v a
- data BackendSpecific
- extractUnique :: (IsUniqueKey uKey, uKey ~ Key v u) => v -> uKey
- data Cond db r
- = And (Cond db r) (Cond db r)
- | Or (Cond db r) (Cond db r)
- | Not (Cond db r)
- | Compare ExprRelation (UntypedExpr db r) (UntypedExpr db r)
- | CondRaw (QueryRaw db r)
- | CondEmpty
- data Order db r
- = forall a f.Projection' f db r a => Asc f
- | forall a f.Projection' f db r a => Desc f
- data family Selector v :: Type -> Type
- data AutoKeyField v (c :: (Type -> Type) -> Type) where
- AutoKeyField :: AutoKeyField v c
- (~>) :: (EntityConstr v c, FieldLike f a, DbDescriptor db, Projection' f db (RestrictionHolder v c) a, Embedded a) => f -> Selector a a' -> SubField db v c a'
- limitTo :: (HasSelectOptions a db r, HasLimit a ~ HFalse) => a -> Int -> SelectOptions db r HTrue (HasOffset a) (HasOrder a) (HasDistinct a)
- offsetBy :: (HasSelectOptions a db r, HasOffset a ~ HFalse) => a -> Int -> SelectOptions db r (HasLimit a) HTrue (HasOrder a) (HasDistinct a)
- orderBy :: (HasSelectOptions a db r, HasOrder a ~ HFalse) => a -> [Order db r] -> SelectOptions db r (HasLimit a) (HasOffset a) HTrue (HasDistinct a)
- (=.) :: (Assignable f a', ProjectionDb f db, ProjectionRestriction f r, Expression db r b, Unifiable f b) => f -> b -> Update db r
- (&&.) :: Cond db r -> Cond db r -> Cond db r
- (||.) :: Cond db r -> Cond db r -> Cond db r
- (==.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r
- (/=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r
- (<.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r
- (<=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r
- (>.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r
- (>=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r
- isFieldNothing :: (Expression db r f, Projection f (Maybe a), PrimitivePersistField (Maybe a), Unifiable f (Maybe a)) => f -> Cond db r
- liftExpr :: ExpressionOf db r a a' => a -> Expr db r a'
- toArith :: ExpressionOf db r a a' => a -> Expr db r a'
- createMigration :: Monad m => Migration m -> m NamedMigrations
- executeMigration :: (PersistBackend m, MonadIO m) => NamedMigrations -> m ()
- executeMigrationUnsafe :: (PersistBackend m, MonadIO m) => NamedMigrations -> m ()
- runMigration :: (PersistBackend m, MonadIO m) => Migration m -> m ()
- runMigrationUnsafe :: (PersistBackend m, MonadIO m) => Migration m -> m ()
- printMigration :: MonadIO m => NamedMigrations -> m ()
Core datatypes and functions
class (Monad m, MonadIO m, MonadFail m, ConnectionManager (Conn m), PersistBackendConn (Conn m)) => PersistBackend m where Source #
This class helps to shorten the type signatures of user monadic code. If your monad has several connections, e.g., for main and audit databases, create run*Db function runAuditDb :: Action conn a -> m a
getConnection :: m (Conn m) Source #
Instances
(Monad m, MonadIO m, MonadFail m, PersistBackendConn conn) => PersistBackend (ReaderT conn m) Source # | |
class (DbDescriptor conn, ConnectionManager conn) => PersistBackendConn conn where Source #
insert, insert_, insertBy, insertByAll, replace, replaceBy, select, selectStream, selectAll, selectAllStream, get, getBy, update, delete, deleteBy, deleteAll, count, countAll, project, projectStream, migrate, executeRaw, queryRaw, insertList, getList
insert :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m (AutoKey v) Source #
Insert a new record to a database and return its autogenerated key or ()
insert_ :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m () Source #
Insert a new record to a database. For some backends it may be faster than insert
.
insertBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u)), PersistBackend m, Conn m ~ conn) => u (UniqueMarker v) -> v -> m (Either (AutoKey v) (AutoKey v)) Source #
Try to insert a record and return Right newkey. If there is a constraint violation for the given constraint, Left oldkey is returned , where oldkey is an identifier of the record with the matching values.
insertByAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m (Either (AutoKey v) (AutoKey v)) Source #
Try to insert a record and return Right newkey. If there is a constraint violation for any constraint, Left oldkey is returned , where oldkey is an identifier of the record with the matching values. Note that if several constraints are violated, a key of an arbitrary matching record is returned.
replace :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific), PersistBackend m, Conn m ~ conn) => Key v BackendSpecific -> v -> m () Source #
Replace a record with the given autogenerated key. Result is undefined if the record does not exist.
replaceBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u)), PersistBackend m, Conn m ~ conn) => u (UniqueMarker v) -> v -> m () Source #
Replace a record. The unique key marker defines what unique key of the entity is used.
select :: (PersistEntity v, EntityConstr v c, HasSelectOptions opts conn (RestrictionHolder v c), PersistBackend m, Conn m ~ conn) => opts -> m [v] Source #
Return a list of the records satisfying the condition. Example: select $ (FirstField ==. "abc" &&. SecondField >. "def") `orderBy` [Asc ThirdField] `limitTo` 100
selectAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => m [(AutoKey v, v)] Source #
Return a list of all records. Order is undefined. It can be useful for datatypes with multiple constructors.
get :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific), PersistBackend m, Conn m ~ conn) => Key v BackendSpecific -> m (Maybe v) Source #
Fetch an entity from a database
getBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u)), PersistBackend m, Conn m ~ conn) => Key v (Unique u) -> m (Maybe v) Source #
Fetch an entity from a database by its unique key
update :: (PersistEntity v, EntityConstr v c, PersistBackend m, Conn m ~ conn) => [Update conn (RestrictionHolder v c)] -> Cond conn (RestrictionHolder v c) -> m () Source #
Update the records satisfying the condition. Example: update [FirstField =. "abc"] $ FirstField ==. "def"
delete :: (PersistEntity v, EntityConstr v c, PersistBackend m, Conn m ~ conn) => Cond conn (RestrictionHolder v c) -> m () Source #
Remove the records satisfying the condition
deleteBy :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific), PersistBackend m, Conn m ~ conn) => Key v BackendSpecific -> m () Source #
Remove the record with given key. No-op if the record does not exist
deleteAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m () Source #
Remove all records. The entity parameter is used only for type inference.
count :: (PersistEntity v, EntityConstr v c, PersistBackend m, Conn m ~ conn) => Cond conn (RestrictionHolder v c) -> m Int Source #
Count total number of records satisfying the condition
countAll :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> m Int Source #
Count total number of records with all constructors. The entity parameter is used only for type inference
project :: (PersistEntity v, EntityConstr v c, Projection' p conn (RestrictionHolder v c) a, HasSelectOptions opts conn (RestrictionHolder v c), PersistBackend m, Conn m ~ conn) => p -> opts -> m [a] Source #
Fetch projection of some fields. Example: project (SecondField, ThirdField) $ (FirstField ==. "abc" &&. SecondField >. "def") `orderBy` [Asc ThirdField] `offsetBy` 100
migrate :: (PersistEntity v, PersistBackend m, Conn m ~ conn) => v -> Migration m Source #
Check database schema and create migrations for the entity and the entities it contains
:: (PersistBackend m, Conn m ~ conn) | |
=> Bool | keep in cache |
-> String | query |
-> [PersistValue] | positional parameters |
-> m () |
Execute raw query
:: (PersistBackend m, Conn m ~ conn) | |
=> Bool | keep in cache |
-> String | query |
-> [PersistValue] | positional parameters |
-> m (RowStream [PersistValue]) |
Execute raw query with results
insertList :: (PersistField a, PersistBackend m, Conn m ~ conn) => [a] -> m Int64 Source #
getList :: (PersistField a, PersistBackend m, Conn m ~ conn) => Int64 -> m [a] Source #
data family Key v :: Type -> Type Source #
A unique identifier of a value stored in a database. This may be a primary key, a constraint or unique indices. The second parameter is the key description.
type family DefaultKey v Source #
This type is the default key for the entity.
type family AutoKey v Source #
This type is the default autoincremented key for the entity. If entity does not have such key, AutoKey v = ().
data UniqueMarker v a Source #
A phantom datatype to make instance head different u (UniqueMarker v)
Instances
data BackendSpecific Source #
Key marked with this type can have value for any backend
extractUnique :: (IsUniqueKey uKey, uKey ~ Key v u) => v -> uKey Source #
Creates value of unique key using the data extracted from the passed value
Represents condition for a query.
And (Cond db r) (Cond db r) | |
Or (Cond db r) (Cond db r) | |
Not (Cond db r) | |
Compare ExprRelation (UntypedExpr db r) (UntypedExpr db r) | |
CondRaw (QueryRaw db r) | |
CondEmpty |
Instances
Defines sort order of a result-set
forall a f.Projection' f db r a => Asc f | |
forall a f.Projection' f db r a => Desc f |
data family Selector v :: Type -> Type Source #
Instances
data Selector (a', b') constr Source # | |
Defined in Database.Groundhog.Instances data Selector (a', b') constr where
| |
data Selector (a', b', c') constr Source # | |
Defined in Database.Groundhog.Instances data Selector (a', b', c') constr where
| |
data Selector (a', b', c', d') constr Source # | |
Defined in Database.Groundhog.Instances data Selector (a', b', c', d') constr where
| |
data Selector (a', b', c', d', e') constr Source # | |
Defined in Database.Groundhog.Instances data Selector (a', b', c', d', e') constr where
|
data AutoKeyField v (c :: (Type -> Type) -> Type) where Source #
It can be used in expressions like a regular field.
For example, delete (AutoKeyField ==. k)
or delete (AutoKeyField ==. k ||. SomeField ==. "DUPLICATE")
AutoKeyField :: AutoKeyField v c |
Instances
(~>) :: (EntityConstr v c, FieldLike f a, DbDescriptor db, Projection' f db (RestrictionHolder v c) a, Embedded a) => f -> Selector a a' -> SubField db v c a' infixl 5 Source #
Accesses fields of the embedded datatypes. For example, SomeField ==. ("abc", "def") ||. SomeField ~> Tuple2_0Selector ==. "def"
limitTo :: (HasSelectOptions a db r, HasLimit a ~ HFalse) => a -> Int -> SelectOptions db r HTrue (HasOffset a) (HasOrder a) (HasDistinct a) Source #
offsetBy :: (HasSelectOptions a db r, HasOffset a ~ HFalse) => a -> Int -> SelectOptions db r (HasLimit a) HTrue (HasOrder a) (HasDistinct a) Source #
orderBy :: (HasSelectOptions a db r, HasOrder a ~ HFalse) => a -> [Order db r] -> SelectOptions db r (HasLimit a) (HasOffset a) HTrue (HasDistinct a) Source #
Expressions
(=.) :: (Assignable f a', ProjectionDb f db, ProjectionRestriction f r, Expression db r b, Unifiable f b) => f -> b -> Update db r infixr 3 Source #
Update field
(==.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r infix 4 Source #
(/=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r infix 4 Source #
(<.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r infix 4 Source #
(<=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r infix 4 Source #
(>.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r infix 4 Source #
(>=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r infix 4 Source #
isFieldNothing :: (Expression db r f, Projection f (Maybe a), PrimitivePersistField (Maybe a), Unifiable f (Maybe a)) => f -> Cond db r Source #
This function more limited than (==.), but has better type inference.
If you want to compare your value to Nothing with (==.)
operator, you have to write the types explicitly myExpr ==. (Nothing :: Maybe Int)
.
TODO: restrict db r
liftExpr :: ExpressionOf db r a a' => a -> Expr db r a' Source #
Converts value to Expr
. It can help to pass values of different types into functions which expect arguments of the same type, like (+).
toArith :: ExpressionOf db r a a' => a -> Expr db r a' Source #
Deprecated: Please use liftExpr instead
It is kept for compatibility with older Groundhog versions and can be replaced with "liftExpr".
Migration
createMigration :: Monad m => Migration m -> m NamedMigrations Source #
Produce the migrations but not execute them. Fails when an unsafe migration occurs.
executeMigration :: (PersistBackend m, MonadIO m) => NamedMigrations -> m () Source #
Execute the migrations with printing to stderr. Fails when an unsafe migration occurs.
executeMigrationUnsafe :: (PersistBackend m, MonadIO m) => NamedMigrations -> m () Source #
Execute migrations. Executes the unsafe migrations without warnings and prints them to stderr
runMigration :: (PersistBackend m, MonadIO m) => Migration m -> m () Source #
Creates migrations and executes them with printing to stderr. Fails when an unsafe migration occurs. > runMigration m = createMigration m >>= executeMigration
runMigrationUnsafe :: (PersistBackend m, MonadIO m) => Migration m -> m () Source #
Creates migrations and executes them with printing to stderr. Executes the unsafe migrations without warnings > runMigrationUnsafe m = createMigration m >>= executeMigrationUnsafe
printMigration :: MonadIO m => NamedMigrations -> m () Source #
Pretty print the migrations