This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-23
Channels
- # announcements (7)
- # babashka (40)
- # babashka-sci-dev (74)
- # beginners (74)
- # calva (31)
- # cider (11)
- # clj-kondo (22)
- # cljs-dev (1)
- # cljsrn (1)
- # clojure (70)
- # clojure-brasil (3)
- # clojure-dev (12)
- # clojure-europe (39)
- # clojure-nl (2)
- # clojure-norway (15)
- # clojure-uk (9)
- # clojurescript (69)
- # community-development (2)
- # conjure (1)
- # core-async (3)
- # cursive (1)
- # data-science (1)
- # datalevin (13)
- # datomic (17)
- # emacs (42)
- # events (1)
- # fulcro (16)
- # graphql (9)
- # helix (1)
- # holy-lambda (14)
- # honeysql (2)
- # hugsql (3)
- # hyperfiddle (5)
- # kaocha (10)
- # lsp (41)
- # luminus (5)
- # malli (7)
- # meander (3)
- # membrane (47)
- # off-topic (23)
- # podcasts-discuss (2)
- # polylith (34)
- # rdf (4)
- # re-frame (2)
- # releases (2)
- # remote-jobs (1)
- # ring (16)
- # shadow-cljs (111)
- # spacemacs (6)
- # test-check (2)
- # tools-deps (19)
Can anyone explain why there are differences in the :find
specifications for on-prem and cloud?
Why doesn't cloud have find-coll [?e ...]
or find-scalar ?e .
?
Is there something wrong with them? Are they discouraged even for on-prem? Is there a technical explanation?
The only one I can think of is maybe too much confusion from people thinking the find-destructure either short-circuits or makes only-one guarantees (it doesn’t do either)
I just submitted a support request. The Zendesk email replied with: > In order to expedite the resolution of this support request, please provide the information described in this Knowledge Base article: https://cognitect.zendesk.com/entries/96723066-Information-to-provide-with-a-support-request. That link 404s
I have this query:
(defn sent
"get user sent threads"
[user & [{:keys [page]}]]
(let [page (or page 0)
db-conn (db/_d)
to-drop (* messages-per-page page)]
(->> (d/q
'[:find ?e (max ?createdAt)
:in $ ?user
:where
[?e :thread/users ?user]
[?m :message/thread ?e]
[?m :ent/created_at ?createdAt]
(not [?user :user/deleted_thread ?e])
[?user :user/read ?e]] db-conn
(:db/id user))
(sort-by #(inst-ms (last %)))
reverse
(drop to-drop)
(take messages-per-page)
(map #(db/by-id (first %) db-conn)))))
And it works:
(take 2 (map keys (sent {:db/id 17592186045460} {:page 0})))
;; => ((:ent/created_at :thread/users :thread/subject :user/read) (:ent/created_at :thread/users :thread/subject))
But I also want to get all the :message/thread matches, which will be an array, so I add an ?m
in :find
(defn sent
"get user sent threads"
[user & [{:keys [page]}]]
(let [page (or page 0)
db-conn (db/_d)
to-drop (* messages-per-page page)]
(->> (d/q
'[:find ?e ?m (max ?createdAt)
:in $ ?user
:where
[?e :thread/users ?user]
[?m :message/thread ?e]
[?m :ent/created_at ?createdAt]
(not [?user :user/deleted_thread ?e])
[?user :user/read ?e]] db-conn
(:db/id user))
(sort-by #(inst-ms (last %)))
reverse
(drop to-drop)
(take messages-per-page)
(map #(db/by-id (first %) db-conn)))))
But evaling this still doesn’t show the :message/thread key:
(take 2 (map keys (sent {:db/id 17592186045460} {:page 0})))
;; => ((:ent/created_at :thread/users :thread/subject :user/read) (:ent/created_at :thread/users :thread/subject))
Why is it not shown?In sent
you have: :find ?e (max ?createdAt) ?m
And then: (sort-by #(inst-ms (last %)))
"Last" is an entity id (long)
@U4VT24ZM3 I updated the question
OK...
So when you run a query. You get back a set of all matches. There are no "arrays" or "collections" in the query...
Instead of:
[entity-id [message1 message2 message3] created-at]
You get:
[entity-id message1 created-at]
[entity-id message2 created-at]
[entity-id message3 created-at]
You can use a pull in your find and pull the messages for a thread:
:find ?e (pull ?e [:message/_thread]) (max ?createdAt)
That would give you something like:
[entity-id {:message/_thread [message1 message2 message3]} created-at]
But I think it would be better to separate that... 1. Query for Threads 2. sort, filter, whatever them 3. Pull additional stuff like the specific messages later.
I still get the inst-ms error with:
(defn sent
"get user sent threads"
[user & [{:keys [page]}]]
(let [page (or page 0)
db-conn (db/_d)
to-drop (* messages-per-page page)]
(->> (d/q
'[:find ?e (pull ?e [:message/_thread]) (max ?createdAt)
:in $ ?user
:where
[?e :thread/users ?user]
[?m :message/thread ?e]
[?m :ent/created_at ?createdAt]
(not [?user :user/deleted_thread ?e])
[?user :user/read ?e]] db-conn
(:db/id user))
(sort-by #(inst-ms (last %)))
reverse
(drop to-drop)
(take messages-per-page)
#_(map #(db/by-id (first %) db-conn)))))
What is: "the inst-ms error" ?
Maybe look into what d/q
actually returns?
I don’t understand this, so this:
(defn sent
"get user sent threads"
[user & [{:keys [page]}]]
(let [page (or page 0)
db-conn (db/_d)
to-drop (* messages-per-page page)]
(->> (d/q
'[:find ?e (pull ?e [:message/_thread]) (max ?createdAt)
:in $ ?user
:where
[?e :thread/users ?user]
[?m :message/thread ?e]
[?m :ent/created_at ?createdAt]
(not [?user :user/deleted_thread ?e])
[?user :user/read ?e]] db-conn
(:db/id user))))))
returns:
[[#:message{:_thread [#:db{:id 17592186048681} #:db{:id 17592186048686}]} 17592186048679] ...]
But when I remove the pull:
(defn sent
"get user sent threads"
[user & [{:keys [page]}]]
(let [page (or page 0)
db-conn (db/_d)
to-drop (* messages-per-page page)]
(->> (d/q
'[:find ?e (max ?createdAt)
:in $ ?user
:where
[?e :thread/users ?user]
[?m :message/thread ?e]
[?m :ent/created_at ?createdAt]
(not [?user :user/deleted_thread ?e])
[?user :user/read ?e]] db-conn
(:db/id user))))))
it returns:
[[17592186048679 #inst "2022-03-17T14:18:58.112-00:00"] ...]
Why is it not returning
[[17592186048679 #:message{:_thread [#:db{:id 17592186048681} #:db{:id 17592186048686}]} #inst "2022-03-17T14:18:58.112-00:00"] ...]
as expected in the first case?
Is this a bug in datomic?Are you sure you executed the right code? Your first example looks of... Try this:
(defn sent
"get user sent threads"
[user & [{:keys [page]}]]
(let [page (or page 0)
db-conn (db/_d)
to-drop (* messages-per-page page)]
(->>
(d/q
'[:find ?e (max ?createdAt)
:in $ ?user
:where
[?e :thread/users ?user]
(not [?user :user/deleted_thread ?e])
[?user :user/read ?e]
[?m :message/thread ?e]
[?m :ent/created_at ?createdAt]]
db-conn
(:db/id user))
(sort-by second)
(map first)
(drop to-drop)
(take messages-per-page)
(d/pull-many db-conn ['* {:message/_thread ['*]}]))))
Like I said: Try to separate finding the threads from pulling the messages.
@U01F1TM2FD5 IIRC I ran into this long ago. The explanation back then was that there’s a limitation (bug) in Datomic that you can’t reference an entity more than once in a :find
expression, or at least in certain situations. The recommendation back then was to convert
:find ?e (pull ?e [:message/_thread]) (max ?createdAt)
into
:find (pull ?e [:db/id :message/_thread]) (max ?createdAt)
That will get you results like this:
[[{:db/id 17592186048679 :message/_thread [#:db{:id 17592186048681} #:db{:id 17592186048686}]} #inst "2022-03-17T14:18:58.112-00:00"] ...]
And you’ll be able to get the entity id out of the first element.