kwrooijen / gungnir

A fully featured, data-driven database library for Clojure.

Home Page:https://kwrooijen.github.io/gungnir/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preloading

kwrooijen opened this issue · comments

Currently in order to load a relation, you need to deref the relation atom of a record. This results in a new query being executed. It would be nice to be able to preload relations, querying them with a join instead of separate queries.

A solution could be to add a new function gungnir.query/preload, which will add a :gungnir/preload '(,,,) key to the HoneySQL map. We then need to modify the query before executing it.

(-> (q/preload :user/posts)
    (q/find-by! :user/email "user@test.com"))

Another thing we need to think about is how do we adjust the result of the relation? Normally we swap! the relation atom, but in this case we'd have to do that beforehand. Maybe a function to update preloads?

(-> (q/preload :user/posts)
    (q/preload-update :user/posts q/limit 5)
    (q/preload-update :user/posts q/sort-by :post/score :desc)
    (q/find-by! :user/email "user@test.com"))