Liking cljdoc? Tell your friends :D

toucan2.tools.hydrate


batched-hydrateclj

(batched-hydrate model k rows)

can-hydrate-with-strategy?clj

(can-hydrate-with-strategy? model strategy k)

fk-keys-for-automagic-hydrationclj

(fk-keys-for-automagic-hydration original-model dest-key hydrated-model)

hydrateclj

(hydrate results k & ks)

Hydrate a single object or sequence of objects.

Automagic Batched Hydration (via [[toucan2.hydrate/model-for-automagic-hydration]])

[[toucan2.hydrate/hydrate]] attempts to do a batched hydration where possible. If the key being hydrated is defined as one of some table's [[toucan2.hydrate/model-for-automagic-hydration]], hydrate will do a batched toucan2.select/select if a corresponding key (by default, the same key suffixed by -id) is found in the objects being batch hydrated. The corresponding key can be customized by implementing [[toucan2.hydrate/fk-keys-for-automagic-hydration]].

(hydrate [{:user_id 100}, {:user_id 101}] :user)

Since :user is a hydration key for :models/User, a single toucan2.select/select will used to fetch Users:

(db/select :models/User :id [:in #{100 101}])

The corresponding Users are then added under the key :user.

Function-Based Batched Hydration (via [[toucan2.hydrate/batched-hydrate]] methods)

If the key can't be hydrated auto-magically with the appropriate [[toucan2.hydrate/model-for-automagic-hydration]], [[toucan2.hydrate/hydrate]] will attempt to do batched hydration if it can find a matching method for [[toucan2.hydrate/batched-hydrate]]. If a matching function is found, it is called with a collection of objects, e.g.

(m/defmethod hydrate/batched-hydrate [:default :fields] [_model _k rows] (let [id->fields (get-some-fields rows)] (for [row rows] (assoc row :fields (get id->fields (:id row))))))

Simple Hydration (via [[toucan2.hydrate/simple-hydrate]] methods)

If the key is not eligible for batched hydration, [[toucan2.hydrate/hydrate]] will look for a matching [[toucan2.hydrate/simple-hydrate]] method. simple-hydrate is called with a single row.

(m/defmethod simple-hydrate [:default :dashboard] [_model _k {:keys [dashboard-id], :as row}] (assoc row :dashboard (select/select-one :models/Dashboard :toucan/pk dashboard-id)))

Hydrating Multiple Keys

You can hydrate several keys at one time:

(hydrate {...} :a :b) -> {:a 1, :b 2}

Nested Hydration

You can do recursive hydration by listing keys inside a vector:

(hydrate {...} [:a :b]) -> {:a {:b 1}}

The first key in a vector will be hydrated normally, and any subsequent keys will be hydrated inside the corresponding values for that key.

(hydrate {...} [:a [:b :c] :e]) -> {:a {:b {:c 1} :e 2}}

Hydrate a single object or sequence of objects.

#### Automagic Batched Hydration (via [[toucan2.hydrate/model-for-automagic-hydration]])

[[toucan2.hydrate/hydrate]] attempts to do a *batched hydration* where possible. If the key being hydrated is
defined as one of some table's [[toucan2.hydrate/model-for-automagic-hydration]], `hydrate` will do a batched
[[toucan2.select/select]] if a corresponding key (by default, the same key suffixed by `-id`) is found in the
objects being batch hydrated. The corresponding key can be customized by
implementing [[toucan2.hydrate/fk-keys-for-automagic-hydration]].

  (hydrate [{:user_id 100}, {:user_id 101}] :user)

Since `:user` is a hydration key for `:models/User`, a single [[toucan2.select/select]] will used to fetch Users:

  (db/select :models/User :id [:in #{100 101}])

The corresponding Users are then added under the key `:user`.

#### Function-Based Batched Hydration (via [[toucan2.hydrate/batched-hydrate]] methods)

If the key can't be hydrated auto-magically with the appropriate [[toucan2.hydrate/model-for-automagic-hydration]],
[[toucan2.hydrate/hydrate]] will attempt to do batched hydration if it can find a matching method
for [[toucan2.hydrate/batched-hydrate]]. If a matching function is found, it is called with a collection of
objects, e.g.

  (m/defmethod hydrate/batched-hydrate [:default :fields]
    [_model _k rows]
    (let [id->fields (get-some-fields rows)]
      (for [row rows]
        (assoc row :fields (get id->fields (:id row))))))

#### Simple Hydration (via [[toucan2.hydrate/simple-hydrate]] methods)

If the key is *not* eligible for batched hydration, [[toucan2.hydrate/hydrate]] will look for a matching
[[toucan2.hydrate/simple-hydrate]] method. `simple-hydrate` is called with a single row.

  (m/defmethod simple-hydrate [:default :dashboard]
    [_model _k {:keys [dashboard-id], :as row}]
    (assoc row :dashboard (select/select-one :models/Dashboard :toucan/pk dashboard-id)))

#### Hydrating Multiple Keys

You can hydrate several keys at one time:

  (hydrate {...} :a :b)
    -> {:a 1, :b 2}

#### Nested Hydration

You can do recursive hydration by listing keys inside a vector:

  (hydrate {...} [:a :b])
    -> {:a {:b 1}}

The first key in a vector will be hydrated normally, and any subsequent keys will be hydrated *inside* the
corresponding values for that key.

  (hydrate {...}
           [:a [:b :c] :e])
    -> {:a {:b {:c 1} :e 2}}
raw docstring

hydrate-with-strategyclj

(hydrate-with-strategy model strategy k rows)

hydration-strategyclj

(hydration-strategy model k)

model-for-automagic-hydrationclj

(model-for-automagic-hydration original-model k)

The model that should be used to automagically hydrate the key k in instances of original-model.

(model-for-automagic-hydration :some-table :user) :-> :myapp.models/user

The model that should be used to automagically hydrate the key `k` in instances of `original-model`.

(model-for-automagic-hydration :some-table :user) :-> :myapp.models/user
raw docstring

simple-hydrateclj

(simple-hydrate model k row)

Implementations should return a version of map row with the key k added.

Implementations should return a version of map `row` with the key `k` added.
raw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close