Skip to content

Instantly share code, notes, and snippets.

@stuarthalloway
Created March 1, 2021 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuarthalloway/9c0ab9f3e5f82e97ef78b8cbad9ce6b2 to your computer and use it in GitHub Desktop.
Save stuarthalloway/9c0ab9f3e5f82e97ef78b8cbad9ce6b2 to your computer and use it in GitHub Desktop.
(defn pull-many
"Pull pattern from each entity in eids in a single eager query.
Returns a seqable of values sorted by:
sort-k if sort-k non nil
eids order if no sort-k specified
unsorted if sort-k is nil"
([db pattern eids]
(let [e->idx (into {} (map-indexed (fn [idx e] [e idx]) eids))]
(pull-many db pattern #(-> % :db/id e->idx) eids)))
([db pattern sort-k eids]
(let [result
(into
[]
(map first)
(d/q '[:find (pull ?e pattern)
:in $ pattern [?e ...]]
db pattern eids))]
(if sort-k
(sort-by sort-k result)
result))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment