Correct fetching of database values
The existing way did not work for local database for some reason. The existing approach tried to fetch all attributes for a given entity. We now specifically fetch the attributes we want for an entity, which is better anyway.
This commit is contained in:
parent
bde07221a6
commit
838e6497d4
1 changed files with 33 additions and 56 deletions
|
@ -71,25 +71,42 @@
|
||||||
(println "Connected to datomic at " uri)
|
(println "Connected to datomic at " uri)
|
||||||
conn))
|
conn))
|
||||||
|
|
||||||
(defn remap-keys [key-maps coll]
|
(def all-grubs-query
|
||||||
(reduce (fn [new-coll [key new-key]] (assoc new-coll new-key (get coll key))) {} key-maps))
|
[:find '?id '?text '?complete
|
||||||
|
:where
|
||||||
|
['?e :grub/id '?id]
|
||||||
|
['?e :grub/text '?text]
|
||||||
|
['?e :grub/completed '?complete]])
|
||||||
|
|
||||||
(defn query-entities [db entity-key]
|
(def all-recipes-query
|
||||||
(->> db
|
[:find '?id '?name '?grubs '?directions
|
||||||
(d/q [:find '?e :where ['?e entity-key]])
|
:where
|
||||||
((fn [[id]] (d/entity db id)))))
|
['?e :recipe/id '?id]
|
||||||
|
['?e :recipe/name '?name]
|
||||||
|
['?e :recipe/grubs '?grubs]
|
||||||
|
['?e :recipe/directions '?directions]])
|
||||||
|
|
||||||
|
(defn grub-as-map [[id text complete]]
|
||||||
|
{:id id :text text :complete complete})
|
||||||
|
|
||||||
|
(defn recipe-as-map [[id name grubs directions]]
|
||||||
|
{:id id :name name :grubs grubs :directions directions})
|
||||||
|
|
||||||
|
(defn get-db-grubs [db]
|
||||||
|
(->> (d/q all-grubs-query db)
|
||||||
|
(map grub-as-map)
|
||||||
|
vec
|
||||||
|
(util/map-by-key :id)))
|
||||||
|
|
||||||
|
(defn get-db-recipes [db]
|
||||||
|
(->> (d/q all-recipes-query db)
|
||||||
|
(map recipe-as-map)
|
||||||
|
vec
|
||||||
|
(util/map-by-key :id)))
|
||||||
|
|
||||||
(defn get-current-db-state [db]
|
(defn get-current-db-state [db]
|
||||||
(let [grubs (->> (query-entities db :grub/id)
|
{:grubs (get-db-grubs db)
|
||||||
(map #(remap-keys {:grub/id :id :grub/text :text :grub/completed :completed} %))
|
:recipes (get-db-recipes db)})
|
||||||
vec
|
|
||||||
(util/map-by-key :id))
|
|
||||||
recipes (->> (query-entities db :recipe/id)
|
|
||||||
(map #(remap-keys {:recipe/id :id :recipe/name :name :recipe/grubs :grubs :recipe/directions :directions} %))
|
|
||||||
vec
|
|
||||||
(util/map-by-key :id))]
|
|
||||||
{:grubs grubs
|
|
||||||
:recipes recipes}))
|
|
||||||
|
|
||||||
(defn get-current-state [conn]
|
(defn get-current-state [conn]
|
||||||
(get-current-db-state (d/db conn)))
|
(get-current-db-state (d/db conn)))
|
||||||
|
@ -112,51 +129,12 @@
|
||||||
:recipe/grubs (:grubs recipe)
|
:recipe/grubs (:grubs recipe)
|
||||||
:recipe/directions (:directions recipe)})])
|
:recipe/directions (:directions recipe)})])
|
||||||
|
|
||||||
(defn update-db! [conn state]
|
|
||||||
(let [grubs-tx (->> state
|
|
||||||
:grubs
|
|
||||||
(vals)
|
|
||||||
(map upsert-grub-tx)
|
|
||||||
(flatten)
|
|
||||||
(vec))
|
|
||||||
recipes-tx (->> state
|
|
||||||
:recipes
|
|
||||||
(vals)
|
|
||||||
(map upsert-recipe-tx)
|
|
||||||
(flatten)
|
|
||||||
(vec))
|
|
||||||
tx (into grubs-tx recipes-tx)]
|
|
||||||
@(d/transact conn tx)))
|
|
||||||
|
|
||||||
(defn disconnect [conn]
|
(defn disconnect [conn]
|
||||||
(d/release conn))
|
(d/release conn))
|
||||||
|
|
||||||
(defn get-history-state [db-conn tag]
|
(defn get-history-state [db-conn tag]
|
||||||
(get-current-state db-conn))
|
(get-current-state db-conn))
|
||||||
|
|
||||||
|
|
||||||
(defn patch-map [state diff]
|
|
||||||
(-> state
|
|
||||||
(#(apply dissoc % (into [] (:- diff))))
|
|
||||||
(#(merge-with merge % (:+ diff)))))
|
|
||||||
|
|
||||||
(defn patch-state [state diff]
|
|
||||||
(->> state
|
|
||||||
(keys)
|
|
||||||
(map (fn [k] [k (patch-map (k state) (k diff))]))
|
|
||||||
(into {})))
|
|
||||||
|
|
||||||
|
|
||||||
(def empty-diff {:grubs {:- #{} :+ nil}
|
|
||||||
:recipes {:- #{} :+ nil}})
|
|
||||||
|
|
||||||
(def added-diff
|
|
||||||
{:grubs {:- #{}
|
|
||||||
:+ {"grub-completed" {:completed true}
|
|
||||||
"grub-updated" {:text "Ketchup"}
|
|
||||||
"grub-added" {:completed false :text "Toothpaste"}}}
|
|
||||||
:recipes {:- #{} :+ nil}})
|
|
||||||
|
|
||||||
(defn diff-tx [diff]
|
(defn diff-tx [diff]
|
||||||
(let [grubs-upsert-tx (->> diff
|
(let [grubs-upsert-tx (->> diff
|
||||||
:grubs
|
:grubs
|
||||||
|
@ -185,5 +163,4 @@
|
||||||
(vec (concat grubs-upsert-tx grubs-retract-tx recipes-upsert-tx recipes-retract-tx))))
|
(vec (concat grubs-upsert-tx grubs-retract-tx recipes-upsert-tx recipes-retract-tx))))
|
||||||
|
|
||||||
(defn patch-state! [conn diff]
|
(defn patch-state! [conn diff]
|
||||||
(pprint (diff-tx diff))
|
|
||||||
@(d/transact conn (diff-tx diff)))
|
@(d/transact conn (diff-tx diff)))
|
||||||
|
|
Loading…
Reference in a new issue