Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve datascript explorer #138

Merged
merged 2 commits into from
Jun 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 72 additions & 19 deletions src/cljs/athens/devcards/db_boxes.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[cljs.core.async :refer [<!]]
[cljsjs.react]
[cljsjs.react.dom]
[clojure.string :as str]
[datascript.core :as d]
[devcards.core :as devcards :refer [defcard defcard-rg]]
[garden.core :refer [css]]
Expand Down Expand Up @@ -136,7 +137,33 @@

(defcard-rg Modify-Devcards
"Increase width to 90% for table"
[:style (css [:.com-rigsomelight-devcards-container {:width "90%"}])])
[:style (css [:.com-rigsomelight-devcards-container {:width "90%"}]
[:.com-rigsomelight-devcards_rendered-card {:display "flex";
:flex-direction "column-reverse"}])]);



(defn attr-unique?
[attr]
(contains? (get db/schema attr) :db/unique))


(defn attr-many?
[attr]
(= (get-in db/schema [attr :db/cardinality])
:db.cardinality/many))


(defn attr-ref?
[attr]
(= (get-in db/schema [attr :db/valueType])
:db.type/ref))


(defn attr-reverse?
[attr]
(when (keyword? attr)
(str/starts-with? (name attr) "_")))


(defn headings
Expand Down Expand Up @@ -167,6 +194,41 @@
(map row))))


(defn reverse-refs-for-attr
[attr eid]
(d/q '[:find [?parent ...]
:in $ ?attr ?eid
:where [?parent :block/children ?eid]]
@db/dsdb attr eid))


(defn reverse-attr
[attr]
(keyword (str (namespace attr) "/_" (name attr))))


(defn wrap-with-db-id
[eid]
{:db/id eid})


(defn reverse-refs
[eid]
(let [ref-attrs (->> db/schema
keys
(filter attr-ref?))]
(into {}
(for [attr ref-attrs]
[(reverse-attr attr)
(map wrap-with-db-id (reverse-refs-for-attr attr eid))]))))


(defn reverse-rows
[{:keys [:db/id]}]
(when id
(reverse-refs id)))


(defn map-rows
[m]
(let [row (fn [[k v]]
Expand All @@ -177,7 +239,8 @@
:attr k
:heading "val"
:idx k}])]
(map row m)))
(concat (map row m)
(map row (reverse-rows m)))))


; still not very clean
Expand Down Expand Up @@ -222,23 +285,6 @@
:maps (maps-rows data)))


(defn attr-unique?
[attr]
(contains? (get db/schema attr) :db/unique))


(defn attr-many?
[attr]
(= (get-in db/schema [attr :db/cardinality])
:db.cardinality/many))


(defn attr-ref?
[attr]
(= (get-in db/schema [attr :db/valueType])
:db.type/ref))


(defn pull-entity-str
([id]
(str "(d/pull @athens/db '[*] " id ")"))
Expand Down Expand Up @@ -268,6 +314,13 @@
:attr :db/id
:id (:db/id v)})])]

(attr-reverse? attr)
[:ul (for [v value]
^{:key v}
[:li (cell {:value v
:attr :db/id
:id (:db/id v)})])]

(attr-many? attr)
[:ul (for [v value]
^{:key v}
Expand Down