Minor cleanup of db code
This commit is contained in:
parent
efbc29b0ad
commit
90e512d59d
1 changed files with 10 additions and 8 deletions
|
@ -5,31 +5,33 @@
|
||||||
|
|
||||||
(def schema-tx (read-string (slurp "database_schema.edn")))
|
(def schema-tx (read-string (slurp "database_schema.edn")))
|
||||||
|
|
||||||
(defn create-db [uri]
|
(defn add-schema-to-db [uri]
|
||||||
(d/create-database uri)
|
(d/transact (d/connect uri) schema-tx) )
|
||||||
(let [conn (d/connect uri)]
|
|
||||||
@(d/transact conn 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]
|
(defn connect [uri]
|
||||||
(create-db uri)
|
(create-db-unless-exists uri)
|
||||||
(let [conn (d/connect uri)]
|
(let [conn (d/connect uri)]
|
||||||
(println "Connected to datomic at " uri)
|
(println "Connected to datomic at " uri)
|
||||||
conn))
|
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))
|
(reduce (fn [new-coll [key new-key]] (assoc new-coll new-key (get coll key))) {} key-maps))
|
||||||
|
|
||||||
(defn get-current-state [conn]
|
(defn get-current-state [conn]
|
||||||
(let [db (d/db conn)
|
(let [db (d/db conn)
|
||||||
get-entity (fn [[id]] (d/touch (d/entity db id)))
|
get-entity (fn [[id]] (d/touch (d/entity db id)))
|
||||||
grub-ids (d/q '[:find ?g :where [?g :grub/id]] (d/db conn))
|
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
|
grubs (->> grub-ids
|
||||||
(map (comp map-grub-keys get-entity))
|
(map (comp map-grub-keys get-entity))
|
||||||
vec
|
vec
|
||||||
(util/map-by-key :id))
|
(util/map-by-key :id))
|
||||||
recipe-ids (d/q '[:find ?r :where [?r :recipe/id]] (d/db conn))
|
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
|
recipes (->> recipe-ids
|
||||||
(map (comp map-recipe-keys get-entity))
|
(map (comp map-recipe-keys get-entity))
|
||||||
vec
|
vec
|
||||||
|
|
Loading…
Reference in a new issue