Search code examples
haskellyesodhaskell-persistent

PersistValues for MongoDB Keys in Yesod/Persistent 2


I have some code that was doing the following in persistent < 2:

(either (\_ -> 0) id).fromPersistValue.unKey.entityKey $ myEntity

How can I achieve the same functionality using persistent > 2 as keys are now defined by individual Backends and as such there is no unKey function? Is there still away to achieve these "Int representations" for MongoDB keys in newer versions of Persistent?


Solution

  • It seems there is a keyToOid function that does approximately what you want in Database.Persist.MongoDB. From the Persistent docs, I gather the change was part of a move towards abstracting keys from backends:

    class (PersistEntity record, PersistEntityBackend record ~ backend, PersistStore backend) => ToBackendKey backend record where

    ToBackendKey converts a PersistEntity Key into a BackendKey This can be used by each backend to convert between a Key and a plain Haskell type.