Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save keesterbrugge/c45868bee7d7ba76fb4503a72e7baf5b to your computer and use it in GitHub Desktop.
Save keesterbrugge/c45868bee7d7ba76fb4503a72e7baf5b to your computer and use it in GitHub Desktop.
datomic entity resolution exploration
(def db-uri "datomic:mem://minimal-example")
(d/create-database db-uri)
(def conn (d/connect db-uri))
(def schema
[ {:db/ident :day
:db/unique :db.unique/identity
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}
{:db/ident :metric/day
:db/unique :db.unique/identity
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :rev
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one} ])
@(d/transact conn schema)
;; till here setup.
;; 1 does not work: create automatically the metric entity and the day entity.
@(d/transact conn [{:metric/day {:day 3} :rev 1.1}])
;; 2 does work: first create day entity and then auto create metric entity.
@(d/transact conn [{:day 3} ])
@(d/transact conn [{:metric/day [:day 3] :rev 1.1}])
(d/q '[:find
(pull ?e [*])
:where
(or [?e :rev]
[?e :day])]
(d/db conn))
;; 3 also works: first create metric entity and then auto create day entity.
@(d/transact conn [{ :rev 1.1}])
(def eid (d/q '[:find
?e .
:where
[?e :rev]]
(d/db conn)))
@(d/transact conn [{:db/id eid :metric/day {:day 3}}])
(d/q '[:find
(pull ?e [*])
:where
(or [?e :rev]
[?e :day])]
(d/db conn))
(d/release conn)
;; conclusion: I have to be mindful of how I transact data into datomic cause it's not as smart as I hoped it would be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment