diff --git a/src/clj/grub/db.clj b/src/clj/grub/db.clj index cc58b68..d18dfd6 100644 --- a/src/clj/grub/db.clj +++ b/src/clj/grub/db.clj @@ -5,31 +5,33 @@ (def schema-tx (read-string (slurp "database_schema.edn"))) -(defn create-db [uri] - (d/create-database uri) - (let [conn (d/connect uri)] - @(d/transact conn schema-tx))) +(defn add-schema-to-db [uri] + (d/transact (d/connect uri) schema-tx) ) + +(defn create-db-unless-exists [uri] + (let [db-created? (d/create-database uri)] + (when db-created? @(add-schema-to-db uri)))) (defn connect [uri] - (create-db uri) + (create-db-unless-exists uri) (let [conn (d/connect uri)] (println "Connected to datomic at " uri) conn)) -(defn map-keys [key-maps coll] +(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 #(map-keys {:grub/id :id :grub/text :text :grub/completed :completed} %) + map-grub-keys #(remap-keys {:grub/id :id :grub/text :text :grub/completed :completed} %) grubs (->> grub-ids (map (comp map-grub-keys get-entity)) vec (util/map-by-key :id)) recipe-ids (d/q '[:find ?r :where [?r :recipe/id]] (d/db conn)) - map-recipe-keys #(map-keys {:recipe/id :id :recipe/name :name :recipe/grubs :grubs :recipe/directions :directions} %) + 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)) vec