Reputation: 4166
I need to fetch a list of entities from a list of keys, with Persistent (Yesod)
Say I have a Model
and its corresponding ModelId
. I have with me:
keys :: [ModelId]
and I need to get
models :: [Model]
The current best way of doing that seems to be:
models' <- mapM get keys
models <- return $ Data.Maybe.catMaybes models'
Is there a more efficient way of doing this? Does persistent queue up the fetch requests or execute them one by one?
There's the same question at yesod persistent: get list of entities from list of keys but I would like to know whether this is the most efficient way of doing this.
Upvotes: 3
Views: 573
Reputation: 332
If I understood your question well, you can achieve it by trying something like this:
models' <- selectList [ModelId <-. keys] []
By using mapM
you will query the database length keys
times, while selectList
performs a single query.
Upvotes: 3