From cec0060984516c4e2eba3d92b5de38b4f5313c00 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Mon, 10 Aug 2015 06:45:09 +0300 Subject: [PATCH] db - wip --- src/clj/grub/db.clj | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/clj/grub/db.clj b/src/clj/grub/db.clj index 80863dc..661c705 100644 --- a/src/clj/grub/db.clj +++ b/src/clj/grub/db.clj @@ -74,24 +74,26 @@ (defn remap-keys [key-maps coll] (reduce (fn [new-coll [key new-key]] (assoc new-coll new-key (get coll key))) {} key-maps)) -(defn get-current-state [conn] - (let [db (d/db conn) - get-entity (fn [[id]] (d/touch (d/entity db id))) - grub-ids (d/q '[:find ?g :where [?g :grub/id]] (d/db conn)) - map-grub-keys #(remap-keys {:grub/id :id :grub/text :text :grub/completed :completed} %) - grubs (->> grub-ids - (map (comp map-grub-keys get-entity)) +(defn query-entities [db entity-key] + (->> db + (d/q [:find '?e :where ['?e entity-key]]) + ((fn [[id]] (d/entity db id))))) + +(defn get-current-db-state [db] + (let [grubs (->> (query-entities db :grub/id) + (map #(remap-keys {:grub/id :id :grub/text :text :grub/completed :completed} %)) vec (util/map-by-key :id)) - recipe-ids (d/q '[:find ?r :where [?r :recipe/id]] (d/db conn)) - map-recipe-keys #(remap-keys {:recipe/id :id :recipe/name :name :recipe/grubs :grubs :recipe/directions :directions} %) - recipes (->> recipe-ids - (map (comp map-recipe-keys get-entity)) + 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] + (get-current-db-state (d/db conn))) + (defn remove-keys-with-nil-vals [mapcoll] (->> mapcoll (remove (fn [[k v]] (nil? v)))