diff options
author | stuebinm | 2022-06-15 02:34:11 +0200 |
---|---|---|
committer | stuebinm | 2022-06-15 02:34:11 +0200 |
commit | 077910f9c0560328949ca7f2e2ab639236f3c523 (patch) | |
tree | f718415af8f6a2d6388aeb88fc328a9faf50d74d /lib/PersistOrphans.hs | |
parent | c37d12f82eea235295c50014acd41c094b426f14 (diff) |
add some persist stuff
this doesn't yet actually use the database, but it's getting close to
3am and I should probably go to bed or something
Diffstat (limited to 'lib/PersistOrphans.hs')
-rw-r--r-- | lib/PersistOrphans.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/PersistOrphans.hs b/lib/PersistOrphans.hs new file mode 100644 index 0000000..68e9738 --- /dev/null +++ b/lib/PersistOrphans.hs @@ -0,0 +1,34 @@ +-- | This module contains instances for the Data.UUID UUID type +-- to be mapped to postgresql's custom builtin uuid type. +-- +-- Unfortunately, this breaks compatability with other SQL databases +-- (though uuids aren't really supported by most anyways) +module PersistOrphans where + + +import Data.Either.Combinators (maybeToRight) +import qualified Data.Text as T +import Data.UUID (UUID) +import Data.UUID as UUID +import Data.UUID.V4 +import Database.Persist (PersistField (..), + PersistValue (PersistLiteralEscaped), + SqlType (SqlOther)) +import Database.Persist.Sql (PersistFieldSql (..), SqlBackend, + migrate, runMigration) +import Web.PathPieces (PathPiece (..)) + + +instance PersistField UUID where + toPersistValue = PersistLiteralEscaped . UUID.toASCIIBytes + fromPersistValue (PersistLiteralEscaped buf) = + maybeToRight "not a uuid (cannot decode)" $ UUID.fromASCIIBytes buf + fromPersistValue v = Left $ "not a uuid (wrong type in database): " <> T.pack (show v) + -- postgres is type-safe, so this should /hopefully/ never happen +instance PersistFieldSql UUID where + sqlType = const $ SqlOther "uuid" +instance PathPiece UUID where + fromPathPiece = UUID.fromText + toPathPiece = UUID.toText + + |