Fork me on GitHub
#datascript
<
2024-03-28
>
seepel23:03:58

I have a question, I read the datascript 2 blog post and I keep wondering about this > Well, it surprised me as well that in 3 years of full-time web app development with DataScript I haven’t used a single query. The place where I use queries is when I want to grab an eid that is on the other side of a :db.unique/identity attribute like this. I have a linked list of messages, and a main chat object that points to the last message in that history. I have the chat entity and I want to get the last message entity so I do this.

(defn last-chat-message [db chat-eid]
  (d/q '[:find ?message .
         :in $ ?chat
         :where
         [?chat :chat/last-message ?message]]
       db chat-eid))
How would I do this with the entity api?

Niki14:03:16

(-> (d/datoms db :eavt chat-eid :chat/last-message)
  first
  :e)

Niki14:03:26

Basically query would do the same under the hood

Niki14:03:00

But because of overhead on parsing/executing and being generic it takes more time doing the query

Niki14:03:16

Maybe the fact that I know how it works affects my preference

🎯 2
seepel16:03:34

Thanks! I have done it that way too, but both ways felt kind of wrong to me.

Niki18:03:54

Looks like I added find-datom that replaces d/datoms + first

Niki18:03:06

Still feels clumsy though

seepel18:03:59

Ahhh, good idea. Something like this?

(defn find-datom [db idx & args]
  (first (apply d/datoms db idx args)))

(defn find-ent [db idx & args]
  (:e (apply find-datom db idx args)))

(defn find-attr [db idx & args]
  (:a (apply find-datom db idx args)))

(defn find-value [db idx & args]
  (:v (apply find-datom db idx args)))

(defn find-tx [db idx & args]
  (:tx (apply find-datom db idx args)))

Niki18:03:48

Yeah, and find-datom is already in