Fork me on GitHub
#datomic
<
2021-11-19
>
Benjamin13:11:23

Is there a idiomatic way for generating tmpids? gensym or randomUUID come to mind - whatever I realized for my use case I already have a unique string at hand

thumbnail14:11:50

For us it depends on the use case. Generally the tempids are crafted by hand, and we use lookup refs for collections (I.e dynamic stuff). Gensym otherwise. UUIDs can be a good tool too especially if you have an unique-identity field for them either way

catjam 1
Benjamin15:11:44

[?order :shop/line-items ??]

;; How do I get the order back when I have a line item?

;; I want to do what it says here
;; 

;; like this but I have cardinality/many

[?release :release/artists ?artist]

Joe Lane15:11:16

@benjamin.schwerdtner

(def order-from-line-item
 '[:find ?order
   :in $ ?line-item
   :where
   [?order :shop/line-items ?line-item]])

(def the-db (d/db the-conn))
(def line-item-id "abc123")
(d/q {:query order-from-line-item :args [the-db line-item-id]})

👍 1
catjam 1
Benjamin17:11:36

What is the most succinct way to assert a unique entity? I thought I can do something like

(d/transact
   conn
   {:tx-data
    [{:db/id [:unique/id "bestid5"]}]})
the effect of the above is I think not what I want. When I pull
(d/pull
   (d/db conn)
   '[*]
   [:unique/id "bestid5"])
=> #:db{:id nil}

Joe Lane17:11:37

You're close, (d/transact conn {:tx-data [{:unique/id "bestid5"}]})

👀 1
🎉 1
Benjamin17:11:38

Now I'm trying this to assert and reference the unique in the same tx

(d/transact
 conn
 {:tx-data
  [{:airstats.pipeline/id "bestid4"}    ;a unqiue
   {:airstats.job/pipeline              ;ref to unique
    [:airstats.pipeline/id "bestid4"]}
   {:airstats.job/pipeline              ;I'll have multiple of those
    [:airstats.pipeline/id "bestid4"]}]})
=>
"Unable to resolve entity: [:airstats.pipeline/id \"bestid4\"] in datom [-9223301668109598094 :airstats.job/pipeline [:airstats.pipeline/id \"bestid4\"]]",
Do I need to put a tmpid?

Joe Lane17:11:22

Yep, you need a tempid

(d/transact
 conn
 {:tx-data
  [{:airstats.pipeline/id "bestid4"
    :db/id "tempid-for-bestid4"}    ;a unqiue
   {:airstats.job/pipeline              ;ref to unique
    "tempid-for-bestid4"}
   {:airstats.pipeline/id "bestid5"
    :db/id "tempid-for-bestid5"}
   {:airstats.job/pipeline              ;I'll have multiple of those
    "tempid-for-bestid5"}]})
I'm not sure how your :airstats.job/pipeline relates to :airstats.pipeline/id but the above will create two different "airstats.job"s, each job references one "airstats.pipeline"

Benjamin17:11:22

:airstats.job/pipeline is supposed to be a ref to an entity with :airstats.pipeline/id and multiple jobs can ref 1 pipeline. Thanks will check. Maybe I should rethink the schema and have jobs as a reference component on the pipeline entity

Joe Lane17:11:10

My only generic advice for you is to try and avoid thinking in rectangles if you can. Datomic doesn't operate in structs of entities. Instead, like Clojure, it operates as sets of attributes.

partywombat 1
Joe Lane17:11:48

(TBC, I'm not saying what you were asking about was necessarily bad or particularly rectangular)

Benjamin17:11:07

haha I'll meditate on that!

👍 1
kenny18:11:45

Could duplicate :where clauses impact query performance in any way?