Kevin Ludwig
2017-05-31 07:17:40 UTC
I've got a data model for movies (since I was following the tutorial on
datomic.com!) and I decided to add a little bit to it. In particular I put
an :isComponent on the movie for rating that looks like this:
{:db/id #db/id[:db.part/db]
:db/ident :movie/title
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "the title of the movie"}
{:db/id #db/id[:db.part/db]
:db/ident :movie/release-year
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db/doc "original theatrical release year"}
{:db/id #db/id[:db.part/db]
:db/ident :rating/value
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "one of NR, G, PG, PG13, R, NC17"}
{:db/id #db/id[:db.part/db]
:db/ident :rating/source
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "the rating body, e.g. MPAA"}
{:db/id #db/id[:db.part/db]
:db/ident :rating/advisories
:db/valueType :db.type/string
:db/cardinality :db.cardinality/many
:db/doc "rating advisories, e.g. adult language, ..."}
; I use these helpers throughout:
(declare conn)
(defn get-conn []
(let [uri (config/get-key :db :uri)]
(defonce conn (d/connect uri))
conn))
(defn get-db []
(let [conn (get-conn)]
(d/db conn)))
; Then I added a document like this:
(defn create []
(let [cxn (get-conn)
datom {:db/id (d/tempid :db.part/user) :movie/title "Some Movie"
:movie/release-year 2017 :movie/rating {:rating/value "R"}}
tx @(d/transact cxn [datom tx-datom])]
(d/resolve-tempid (get-db) (:tempids tx) (:db/id datom))))
; Then I updated the rating and title like this:
(defn update [id]
(let [cxn (get-conn)
datom {:db/id id :title "Some Movie 2" :movie/rating
{:rating/value "PG"}}
tx @(d/transact cxn [datom tx-datom])]
(d/pull (get-db) '[*] id)))
; Then I pulled history on :movie/title:
(defn movie-title-history [id]
(let [hdb (d/history (get-db))
attr :movie/title
query '[:find ?e ?a ?v ?t ?op
:in $ ?e ?a
:where [?e ?a ?v ?t ?op]]]
(d/q query hdb id attr)))
; and then on :rating/value
(defn movie-rating-value-history [id]
(let [hdb (d/history (get-db))
attr :rating/value
query '[:find ?e ?a ?v ?t ?op
:in $ ?e ?a
:where [?e _ ?ref]
[?ref ?a ?v ?t ?op]]]
(d/q query hdb id attr)))
And I was surprised to find that my history on title had 3 entries (2 adds
and 1 retract), whereas my history on :rating/value has only 2 entries (2
adds, and no retract). Is this a bug or am I doing something wrong? For
reference I'm using version datomic-pro-0.9.5561.
datomic.com!) and I decided to add a little bit to it. In particular I put
an :isComponent on the movie for rating that looks like this:
{:db/id #db/id[:db.part/db]
:db/ident :movie/title
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "the title of the movie"}
{:db/id #db/id[:db.part/db]
:db/ident :movie/release-year
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db/doc "original theatrical release year"}
{:db/id #db/id[:db.part/db]
:db/ident :rating/value
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "one of NR, G, PG, PG13, R, NC17"}
{:db/id #db/id[:db.part/db]
:db/ident :rating/source
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "the rating body, e.g. MPAA"}
{:db/id #db/id[:db.part/db]
:db/ident :rating/advisories
:db/valueType :db.type/string
:db/cardinality :db.cardinality/many
:db/doc "rating advisories, e.g. adult language, ..."}
; I use these helpers throughout:
(declare conn)
(defn get-conn []
(let [uri (config/get-key :db :uri)]
(defonce conn (d/connect uri))
conn))
(defn get-db []
(let [conn (get-conn)]
(d/db conn)))
; Then I added a document like this:
(defn create []
(let [cxn (get-conn)
datom {:db/id (d/tempid :db.part/user) :movie/title "Some Movie"
:movie/release-year 2017 :movie/rating {:rating/value "R"}}
tx @(d/transact cxn [datom tx-datom])]
(d/resolve-tempid (get-db) (:tempids tx) (:db/id datom))))
; Then I updated the rating and title like this:
(defn update [id]
(let [cxn (get-conn)
datom {:db/id id :title "Some Movie 2" :movie/rating
{:rating/value "PG"}}
tx @(d/transact cxn [datom tx-datom])]
(d/pull (get-db) '[*] id)))
; Then I pulled history on :movie/title:
(defn movie-title-history [id]
(let [hdb (d/history (get-db))
attr :movie/title
query '[:find ?e ?a ?v ?t ?op
:in $ ?e ?a
:where [?e ?a ?v ?t ?op]]]
(d/q query hdb id attr)))
; and then on :rating/value
(defn movie-rating-value-history [id]
(let [hdb (d/history (get-db))
attr :rating/value
query '[:find ?e ?a ?v ?t ?op
:in $ ?e ?a
:where [?e _ ?ref]
[?ref ?a ?v ?t ?op]]]
(d/q query hdb id attr)))
And I was surprised to find that my history on title had 3 entries (2 adds
and 1 retract), whereas my history on :rating/value has only 2 entries (2
adds, and no retract). Is this a bug or am I doing something wrong? For
reference I'm using version datomic-pro-0.9.5561.
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.