I'm new to both Haskell and Yesod, and am trying to build a simple web application that can answer queries from an external API. I have built a parser (using Parsec), that gets me the ID of an entity I want to load as a regular Int value.
However, I for the life of me can't figure out how to turn this Int into something that get
will accept (i. e. a Key
(?)). All the examples in the documentation only get the id from previous inserts, or from url dispatch.
Any help would be greatly appreciated, since I seem to be stuck... :)
Even if the answer can already be found in the comments, I would like to give a complete example.
Assuming we have a
Person
Model, the following function returns a record for the persion with the given ID (if it exists):The
import
is needed to let us construct the persist-version of an integer.fromIntegral
converts any integer to the expected typeInt64
.Update: Since Yesod 1.2
PersistValue
lives in the moduleDatabase.Persist.Types
, before 1.2 it wasDatabase.Persist.Store
(API Documentation).Update 2: Since Persistent 2.0.2 there are two build-in functions to convert from/to database keys:
toSqlKey
andfromSqlKey
(API Documentation, see answer by hhefesto for an example).PersistInt64
is here:Database.Persist.Types
.Previously
PersistInt64
was here:Database.Persist.Store
.Just an example of how to use toSqlKey (Persistent 2.0.2)