0
votes

I am having issues with types matching up in persistent. I have a module called Storage.Mongo like so:

type FieldMap = Map.Map T.Text T.Text

let mongoSettings = (mkPersistSettings (ConT ''MongoBackend)) {mpsGeneric = False}
    in share [mkPersist mongoSettings] [persistLowerCase|
    Notice
        rawData FieldMap
        deriving Show
    |]

-- | This is the default database pool
defaultPool = createMongoDBPool
            "system_of_record"
            "localhost"
            (PortNumber 33107)
            (Just (MongoAuth "reader" "password"))
            10
            10
            30

-- | Save notice to database
saveNotices x = do pool <- defaultPool
                   runMongoDBPoolDef save pool
                   where save = mapM_ (insert.Notice) x

I am trying to pass the saveNotices command a field map, which it will convert to a Notice entity and save to a MongoDb database. To wit:

main = do files <- getArgs
          mapM_ parseNotice files
          where parseNotice f = do x <- parseFromFile fboFile f
                                   case x of
                                    Left err -> print err
                                    Right notices -> mapM_ saveNotices notices

The parseNotice function returns a list of Maps:

notice = do noticeType <- openingTag
            fields <- manyTill (try complexField <|> simpleField) (try closingTag)
            return $ (Map.fromList(concat ([("NOTICETYPE", noticeType)]:fields)))

fboFile = many notice

I am not sure where the problem is. I believe I should let the compiler the know the type of

mapM_ (insert.Notice) x

is, but I am not sure what the type should be

This is the error I am getting

Couldn't match type 'PersistEntityBackend Notice' with 'MongoBackend' Expected type: PersistEntityBackend Notice Actual type: PersistMonadBackend (Action m) In the first argument of '(.)', namely 'insert' In the first argument of 'mapM_', namely '(insert . Notice)' In the expression: mapM_ (insert . Notice) x

1
It helps to call out the line that errored in your code with a comment and include the exact error message encountered. - Guvante
I don't believe I have the permission to edit the question, but the error I am getting is: Couldn't match type 'PersistEntityBackend Notice' with 'MongoBackend' Expected type: PersistEntityBackend Notice Actual type: PersistMonadBackend (Action m) In the first argument of '(.)', namely 'insert' In the first argument of 'mapM_', namely '(insert . Notice)' In the expression: mapM_ (insert . Notice) x - Arnob
There is an edit link right below the tags for the question. You can always (to my knowledge) edit your own questions/answers. I went ahead and added it for you. - Guvante

1 Answers

1
votes

It is failing trying to apply insert . Notice (space added for emphasis)

insert :: MonadIO' m => Collection -> Document -> Action m Value
type Collection = Text

It looks like insert expects a name as its first argument. The following might work.

mapM_ (insert "Notice" $ Notice) x