Skip to content

Instantly share code, notes, and snippets.

@spieden
Created June 25, 2019 22:27
Show Gist options
  • Save spieden/1f559ab5508d44b837647ab894e4795a to your computer and use it in GitHub Desktop.
Save spieden/1f559ab5508d44b837647ab894e4795a to your computer and use it in GitHub Desktop.
(ns ncgl.datum-human-readable
(:require [ncgl.db.fixtures :as db-fxt]
[datomic.api :as d]))
(defn fixture []
(let [conn (db-fxt/make-test-conn)]
@(d/transact conn
[{:db/id "FOO"
:gene/hgnc-id "FOO"}
{:db/id "panel-id"
:panel/id "panel-id"
:panel/genes ["FOO"]}])))
(defn readable-tx-data [{:keys [db-after tx-data]}]
(let [unique-id-attrs (into #{}
(map first)
(d/q '{:find [?e]
:where [[?e :db/unique :db.unique/identity]]}
db-after))
ref-attr? (into #{}
(map first)
(d/q '{:find [?e]
:where [[?e :db/valueType :db.type/ref]]}
db-after))
tx-eids (into #{}
(map :e)
tx-data)
tx-ref-eids (into #{}
(comp (filter (comp ref-attr? :a))
(map :v))
tx-data)
eid->id-refs (->> (d/q '{:find [?e ?id-attr-ident ?id-val]
:in [$ [?e ...] [?id-attr ...]]
:where [[?e ?id-attr ?id-val]
[?id-attr :db/ident ?id-attr-ident]]}
db-after
(into tx-eids
tx-ref-eids)
unique-id-attrs)
(group-by first)
(into {}
(map (fn [[e refs]]
[e (into []
(map (fn [[_ id-attr-ident id-val]]
[id-attr-ident id-val]))
refs)]))))]
(mapv (fn [{:keys [e a v]}]
[(eid->id-refs e e)
(d/ident db-after a)
(if (ref-attr? a)
(eid->id-refs v v)
v)])
tx-data)))
(defn sandbox []
(readable-tx-data (fixture)))
(comment
[[13194139534345 :db/txInstant #inst"2019-06-25T22:26:47.861-00:00"]
[[[:gene/hgnc-id "FOO"]] :gene/hgnc-id "FOO"]
[[[:panel/id "panel-id"]] :panel/id "panel-id"]
[[[:panel/id "panel-id"]] :panel/genes [[:gene/hgnc-id "FOO"]]]]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment