Skip to content

Allow eager loading associations w/ a renamed fk#1

Merged
kgann merged 2 commits intokgann:masterfrom
trptcolin:transformed-fks
Aug 3, 2015
Merged

Allow eager loading associations w/ a renamed fk#1
kgann merged 2 commits intokgann:masterfrom
trptcolin:transformed-fks

Conversation

@trptcolin
Copy link
Contributor

Because Korma's :transforms/:prepares features allow users to rename fields, we can't necessarily assume that the foreign key passed to the database query is the same as the one in the result set. This removes that assumption from the eager-loading (include) stitching, by transforming the foreign key in the same way as records are transformed.

I do realize that this test case's particular kebab/snake-case dance introduces a lot of complexity for what ends up being a pretty low benefit, but it happens, and takes some work to rip out. So as a general tool on top of Korma, which allows this kind of thing, I think it's great if Cumin can handle this use case.

Because Korma's :transforms/:prepares features allow users to rename
fields, we can't necessarily assume that the foreign key passed to the
database query is the same as the one in the result set. This removes
that assumption from the eager-loading stitcher by transforming the
foreign key in the same way as records are transformed.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I'm worried a transformation may expect some data to be present and throw an exception when mimicking a record with {fk nil}.

How do you feel about removing the transforms, stitching the records together, and then applying the transformation to the stitched result set?

(defn- ^:no-doc apply-transforms
  [results f k]
  (mapv (fn [row]
          (if (contains? row k)
            (update-in row [k] #(mapv f %))
            row))
        results))

(defn ^:no-doc include* [ent mapping body-fn rows]
  (let [[pk fk] (first (dissoc mapping :as))
        as-k (get mapping :as (keyword (:table ent)))
        pks (distinct (remove nil? (map pk rows)))
        sub-q (-> ent (dissoc :transforms) (select*) (body-fn))
        results (when (seq pks)
                  (-> sub-q
                      (cond-> (not-any? #{:* :korma.core/* fk} (:fields sub-q))
                        (update-in [:fields] conj fk))
                      (where {fk [in pks]})
                      (select)))]
    (if (seq results)
      (-> rows
          (stitch results as-k [pk fk])
          (apply-transforms (apply comp (:transforms ent)) as-k))
      rows)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go for it - this works totally great for me. The {fk nil} was totally a hack, and I like this better.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make the changes, I'll happily merge this PR - the tests are great. Thanks for spotting this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool deal, will do, thanks!

This way we don't assume all transforms are able to handle a nil value,
missing keys, etc. the way that transforming `{fk nil}` would require.

h/t @kgann for the PR review & modified code.
@trptcolin
Copy link
Contributor Author

Updated. By the way, thanks for this library - I'm super-impressed at how concisely you've added these features, and I'm looking forward to rolling it out for some huge performance wins.

@kgann
Copy link
Owner

kgann commented Aug 3, 2015

Awesome! I'm glad you're getting some use out of it. Keep the improvements comin'!

kgann added a commit that referenced this pull request Aug 3, 2015
Allow eager loading associations w/ a renamed fk
@kgann kgann merged commit 2d9f428 into kgann:master Aug 3, 2015
@trptcolin trptcolin deleted the transformed-fks branch August 3, 2015 18:18
@kgann
Copy link
Owner

kgann commented Aug 3, 2015

I released 0.2.1 - thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants