Fork me on GitHub
#datomic
<
2022-02-01
>
timo12:02:50

How do I do that?

(d/q '[:find ?e ?contract-end 
       :in $ ?date
       :where
       [?e :contract/end ?contract-end]
       [(< ?date ?contract-end)]]
     conn
     (java.util.Date.)))
I am trying to get only entities that have a date that is later than the one passed. But I am getting ClassCastException 😞

pyry12:02:47

Just evaluating (< (Date.) (Date.)) in the repl gives a ClassCastException, so no wonder if Datomic does as well.

pyry12:02:52

Should be able to do something like [(.before ?date ?contract-end)] though.

timo12:02:05

right, thanks. That seems more correct.

timo12:02:14

it's just giving me still CCE... :thinking_face:

timo12:02:55

but probably the problem is elsewhere... the entity value is an inst though

favila13:02:16

Datomic supports comparison on dates.

👍 2
favila13:02:10

Is conn a connection or db?

timo13:02:22

(def older (Date.))
(d/q '[:find ?contract-end => #{[#inst "2022-02-01T12:56:01.393-00:00"]}
       :in $ ?date
       :where
       [?e :contract/end ?contract-end]
       [(> ?date ?contract-end)]]
       [[1 :contract/end (Date.)]
        [2 :contract/end older]]
     (Date.)))
This works, that means there is somewhere a non-inst in the query 🍌

pyry13:02:40

Right, sorry about that false lead.

timo13:02:05

no worries

favila13:02:10

@U4GEXTNGZ I think your class cast may be about trying to cast a connection to a db, nothing about dates. Your db var is named conn which is fishy

favila13:02:28

What is the actual exception?

timo13:02:38

no sorry about that... I am using d/db to get the conn... I am used to use datahike and there usually it's a connection that is used to query.

timo13:02:37

there was actually a (get-else in the query that I did not recognize as the problem here

timo13:02:10

it was giving me a string when there is no contract-end there

souenzzo13:02:02

> it was giving me a string when there is no contract-end there This is wired. when it don't have contract/end, it should simply don't match

souenzzo13:02:13

(require '[datomic.client.api :as d])
(let [conn (-> {:server-type :dev-local
                :system      "hello"}
             d/client
             (doto (d/delete-database {:db-name "hello"})
                   (d/create-database {:db-name "hello"}))
             (d/connect {:db-name "hello"})
             (doto (d/transact {:tx-data [{:db/ident       :contract/id
                                           :db/valueType   :db.type/string
                                           :db/cardinality :db.cardinality/one}
                                          {:db/ident       :contract/end
                                           :db/valueType   :db.type/instant
                                           :db/cardinality :db.cardinality/one}]})))
      {:keys [db-after]} (d/transact conn
                           {:tx-data [{:contract/id "1"}
                                      {:contract/id  "2"
                                       :contract/end #inst"2000"}
                                      {:contract/id  "3"
                                       :contract/end #inst"3000"}]})]
  (d/q '[:find ?e ?contract-end
         :in $ ?date
         :where
         [?e :contract/end ?contract-end]
         [(< ?date ?contract-end)]]
    db-after
    (java.util.Date.)))
=> [[83562883711053 #inst"3000-01-01T00:00:00.000-00:00"]]

timo13:02:44

see

(d/q '[:find ?contract-end => #{[#inst "2022-02-01T12:56:01.393-00:00"]}
       :in $ ?date
       :where
       [?e :contract/end ?contract-end]
       [(> ?date ?contract-end)]]
       [[1 :contract/end (Date.)]
        [2 :contract/end older]
        [3 :contract/end "-"]]
     (java.util.Date.)))
results in
; eval (current-form): (d/q '[:find ?contract-end :in $ ?date :where [?e :...
; (err) Execution error (ClassCastException) at (REPL:1).
; (err) null

timo13:02:25

yeah, sorry. there is a get-else that tripped me off. it inserted a string where there should be an inst.

souenzzo13:02:53

to use datomic.api (peer api) you can change to

conn (-> "datomic:"
       (doto d/delete-database
             d/create-database)
       d/connect)
need to remove the {:tx-data map too

souenzzo13:02:38

You are creating an invalid database: [3 :contract/end "-"]