Last active
October 3, 2015 17:01
-
-
Save jvans1/f581f60c2c2a37d1bcb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Database.Persist.Sql | |
import qualified Database.Persist.TH as DPTH | |
--Consider the followering database structure | |
DPTH.share [DPTH.mkPersist DPTH.sqlSettings, DPTH.mkMigrate "migrateAll"] [DPTH.persistLowerCase| | |
User | |
name T.Text | |
UserPost | |
user_id UserId | |
post_id PostId | |
Post | |
author_id UserId | |
content Text | |
Follower | |
user_id UserId | |
follower_id FollowerId | |
|] | |
-- We almost never want to work with just a user here. We usually want to work with a type more like(forgive the double definition of user) | |
data User = User { posts :: [Entity Post], name :: T.Text, followers :: [Entity Follower] } | |
-- We have to manually load these associations to create this type so wouldn't it be nice have this taken care of for us? | |
-- We could create a type class | |
class (PersistEntity a, PersistEntity b) => HasMany a b where | |
data HasMany a b = HasMany a b | |
-- define Join table and Foreign key | |
-- So we could write | |
instance HasMany User Post where | |
-- definition | |
instance HasMany User Follower where | |
-- definition | |
allUsers :: [User] | |
-- But this is expensive. What if we have 5 has many associations? We dont want to always load every association | |
-- What we really want is a datatype like | |
type MaybeLoaded = Maybe | |
data User = User { posts :: MaybeLoaded [Entity Post], name :: T.Text, followers :: MaybeLoaded [Entity Follower] } | |
--and a function that we can provide a list of associations to load | |
allUsers :: PersistEntity b => [HasMany User b] -> [User] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment