Skip to content

Instantly share code, notes, and snippets.

@jvans1
Last active October 3, 2015 17:01
Show Gist options
  • Save jvans1/f581f60c2c2a37d1bcb1 to your computer and use it in GitHub Desktop.
Save jvans1/f581f60c2c2a37d1bcb1 to your computer and use it in GitHub Desktop.
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